Skip to content

Commit

Permalink
Return proper exit code for TERM signal
Browse files Browse the repository at this point in the history
Attempt at returning the proper exit code (128+15) when TERM signal
is sent to the server, for both single and clustered mode.

The changes are achieved by restoring signal from within the trap
and accordingly killing the process using TERM event.

Added plus, stopping single mode gracefully now.
  • Loading branch information
Shayon Mukherjee committed Aug 5, 2017
1 parent 0d7a8bb commit f43ceea
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/puma/cluster.rb
Expand Up @@ -376,6 +376,9 @@ def setup_signals
exit! 0
else
stop

Signal.trap("SIGTERM", "DEFAULT")
Process.kill("TERM", Process.pid)
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/puma/launcher.rb
Expand Up @@ -392,7 +392,10 @@ def setup_signals

begin
Signal.trap "SIGTERM" do
stop
graceful_stop

Signal.trap("SIGTERM", "DEFAULT")
Process.kill("TERM", Process.pid)
end
rescue Exception
log "*** SIGTERM not implemented, signal based gracefully stopping unavailable!"
Expand Down
22 changes: 22 additions & 0 deletions test/test_integration.rb
Expand Up @@ -218,4 +218,26 @@ def test_restart_restores_environment
assert_includes new_reply, "Hello RAND"
refute_equal initial_reply, new_reply
end

def test_term_signal_exit_code_in_single_mode
server("-q test/rackup/hello.ru")

Process.kill(:TERM, @server.pid)

_, status = Process.wait2(@server.pid)
assert_equal 15, status

@server = nil
end

def test_term_signal_exit_code_in_clustered_mode
server("-q -w 2 test/rackup/hello.ru")

Process.kill(:TERM, @server.pid)

_, status = Process.wait2(@server.pid)
assert_equal 15, status

@server = nil
end
end

0 comments on commit f43ceea

Please sign in to comment.