Skip to content

Commit

Permalink
Idempotent binding close
Browse files Browse the repository at this point in the history
  • Loading branch information
nateberkopec committed Sep 3, 2019
1 parent 375a751 commit ceda63d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
11 changes: 3 additions & 8 deletions lib/puma/binder.rb
Expand Up @@ -44,19 +44,14 @@ def initialize(events)

attr_reader :listeners, :ios

# TODO: Removing listeners should clean up @envs too
def env(sock)
@envs.fetch(sock, @proto_env)
end

def close
@ios.each { |i| i.close }
@unix_paths.each do |i|
# Errno::ENOENT is intermittently raised
begin
File.unlink i
rescue Errno::ENOENT
end
end
@ios.each { |i| i.close && @ios.delete(i) }
@unix_paths.each { |i| File.unlink(i) && @unix_paths.delete(i) }
end

def import_from_env
Expand Down
1 change: 1 addition & 0 deletions lib/puma/cluster.rb
Expand Up @@ -381,6 +381,7 @@ def setup_signals
log "Early termination of worker"
exit! 0
else
# TODO: replace with binder.close?
@launcher.close_binder_listeners

stop_workers
Expand Down
12 changes: 6 additions & 6 deletions lib/puma/server.rb
Expand Up @@ -74,7 +74,6 @@ def initialize(app, events=Events.stdio, options={})
@first_data_timeout = options.fetch(:first_data_timeout, FIRST_DATA_TIMEOUT)

@binder = Binder.new(events)
@own_binder = true

@leak_stack_on_error = true

Expand All @@ -97,7 +96,6 @@ def initialize(app, events=Events.stdio, options={})

def inherit_binder(bind)
@binder = bind
@own_binder = false
end

def tcp_mode!
Expand Down Expand Up @@ -271,6 +269,8 @@ def handle_servers_lopez_mode
rescue IOError
# no biggy
end

@binder.close if @status != :restart
end

@events.fire :state, :done
Expand Down Expand Up @@ -417,8 +417,8 @@ def handle_servers
end

@events.fire :state, @status

graceful_shutdown if @status == :stop || @status == :restart

if queue_requests
@reactor.clear!
@reactor.shutdown
Expand All @@ -430,9 +430,7 @@ def handle_servers
@check.close
@notify.close

if @status != :restart and @own_binder
@binder.close
end
@binder.close if @status != :restart
end

@events.fire :state, :done
Expand Down Expand Up @@ -952,6 +950,8 @@ def graceful_shutdown
@events.debug "Drained #{count} additional connections."
end

@binder.close if @status != :restart

if @thread_pool
if timeout = @options[:force_shutdown_after]
@thread_pool.shutdown timeout.to_i
Expand Down
4 changes: 2 additions & 2 deletions test/test_integration.rb
Expand Up @@ -193,12 +193,12 @@ def test_phased_restart_via_pumactl
done = true
end
end
# Stop

assert File.exist? @bind_path
ccli = Puma::ControlCLI.new ["-S", @state_path, "stop"], sout
ccli.run

assert_kind_of Thread, t.join, "server didn't stop"
assert File.exist? @bind_path
end

def test_kill_unknown_via_pumactl
Expand Down

0 comments on commit ceda63d

Please sign in to comment.