From a02f9241aef8a2e8814c745e84457ca0eabe5e8d Mon Sep 17 00:00:00 2001 From: Calvin Xiao Date: Fri, 23 Apr 2021 22:31:58 +0800 Subject: [PATCH] Handle fatal error that has no backtrace (#2607) * handle low level error that has no backtrace * add force_shutdown_after in test case * skip test on windows for low level error * remove extra space * remove two extra lines * rename test method to lowlevel_error --- lib/puma/server.rb | 3 ++- test/test_puma_server.rb | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/puma/server.rb b/lib/puma/server.rb index b826c8d7..1f1ffc21 100644 --- a/lib/puma/server.rb +++ b/lib/puma/server.rb @@ -528,7 +528,8 @@ module Puma end if @leak_stack_on_error - [status, {}, ["Puma caught this error: #{e.message} (#{e.class})\n#{e.backtrace.join("\n")}"]] + backtrace = e.backtrace.nil? ? '' : e.backtrace.join("\n") + [status, {}, ["Puma caught this error: #{e.message} (#{e.class})\n#{backtrace}"]] else [status, {}, ["An unhandled lowlevel error occurred. The application logs may have details.\n"]] end diff --git a/test/test_puma_server.rb b/test/test_puma_server.rb index 226e8516..13e84c4b 100644 --- a/test/test_puma_server.rb +++ b/test/test_puma_server.rb @@ -285,6 +285,25 @@ EOF assert_match(/{}\n$/, data) end + def test_lowlevel_error_message + skip_if :windows + @server = Puma::Server.new @app, @events, {:force_shutdown_after => 2} + + server_run app: ->(env) do + require 'json' + + # will raise fatal: machine stack overflow in critical region + obj = {} + obj['cycle'] = obj + ::JSON.dump(obj) + end + + data = send_http_and_read "GET / HTTP/1.0\r\n\r\n" + + assert_match(/HTTP\/1.0 500 Internal Server Error/, data) + assert (data.size > 0), "Expected response message to be not empty" + end + def test_force_shutdown_error_default @server = Puma::Server.new @app, @events, {:force_shutdown_after => 2}