Skip to content

Commit

Permalink
Handle fatal error that has no backtrace (#2607)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
calvinxiao committed Apr 23, 2021
1 parent a717b27 commit a02f924
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/puma/server.rb
Expand Up @@ -528,7 +528,8 @@ def lowlevel_error(e, env, status=500)
end

if @leak_stack_on_error
[status, {}, ["Puma caught this error: #{e.message} (#{e.class})\n#{e.backtrace.join("\n")}"]]
backtrace = e.backtrace.nil? ? '<no backtrace available>' : 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
Expand Down
19 changes: 19 additions & 0 deletions test/test_puma_server.rb
Expand Up @@ -285,6 +285,25 @@ def test_force_shutdown_custom_error_message
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}

Expand Down

0 comments on commit a02f924

Please sign in to comment.