Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More elaborate exception handling. #2700

Merged
merged 2 commits into from Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/puma/server.rb
Expand Up @@ -356,7 +356,10 @@ def handle_servers
pool << client
end
end
rescue Object => e
rescue IOError, Errno::EBADF
# In the case that any of the sockets are unexpectedly close.
raise
rescue StandardError => e
@events.unknown_error e, nil, "Listen loop"
end
end
Expand Down
8 changes: 6 additions & 2 deletions test/helpers/integration.rb
Expand Up @@ -50,7 +50,7 @@ def silent_and_checked_system_command(*args)
assert(system(*args, out: File::NULL, err: File::NULL))
end

def cli_server(argv, unix: false, config: nil)
def cli_server(argv, unix: false, config: nil, merge_err: false)
if config
config_file = Tempfile.new(%w(config .rb))
config_file.write config
Expand All @@ -64,7 +64,11 @@ def cli_server(argv, unix: false, config: nil)
@tcp_port = UniquePort.call
cmd = "#{BASE} #{puma_path} #{config} -b tcp://#{HOST}:#{@tcp_port} #{argv}"
end
@server = IO.popen(cmd, "r")
if merge_err
@server = IO.popen(cmd, "r", :err=>[:child, :out])
else
@server = IO.popen(cmd, "r")
end
wait_for_server_to_boot
@pid = @server.pid
@server
Expand Down
6 changes: 6 additions & 0 deletions test/rackup/close_listeners.ru
@@ -0,0 +1,6 @@
require 'objspace'

run lambda { |env|
ios = ObjectSpace.each_object(::TCPServer).to_a.tap { |a| a.each(&:close) }
[200, [], ["#{ios.inspect}\n"]]
}
25 changes: 25 additions & 0 deletions test/test_integration_single.rb
Expand Up @@ -178,4 +178,29 @@ def test_application_logs_are_flushed_on_write
@server.close unless @server.closed?
@server = nil
end

# listener is closed 'externally' while Puma is in the IO.select statement
def test_closed_listener
skip_unless_signal_exist? :TERM

cli_server "test/rackup/close_listeners.ru", merge_err: true
read_body connect

begin
Timeout.timeout(5) do
begin
Process.kill :SIGTERM, @pid
rescue Errno::ESRCH
end
begin
Process.wait2 @pid
rescue Errno::ECHILD
end
end
rescue Timeout::Error
Process.kill :SIGKILL, @pid
assert false, "Process froze"
end
assert true
end
end