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 8db6ee5
Show file tree
Hide file tree
Showing 3 changed files with 37 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
30 changes: 30 additions & 0 deletions test/test_integration.rb
Expand Up @@ -218,4 +218,34 @@ 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
pid = fork do
base = (defined?(Bundler) ? "bundle exec puma" : "#{Gem.ruby} -Ilib bin/puma")
exec "#{base} -b tcp://127.0.0.1:#{@tcp_port} -q test/rackup/hello.ru"
end

sleep 10

Process.kill(:TERM, pid)

_, status = Process.wait2(pid)

assert_equal 15, status
end

def test_term_signal_exit_code_in_clustered_mode
pid = fork do
base = (defined?(Bundler) ? "bundle exec puma" : "#{Gem.ruby} -Ilib bin/puma")
exec "#{base} -b tcp://127.0.0.1:#{@tcp_port} -q -w 2 test/rackup/hello.ru"
end

sleep 10

Process.kill(:TERM, pid)

_, status = Process.wait2(pid)

assert_equal 15, status
end
end

0 comments on commit 8db6ee5

Please sign in to comment.