Skip to content

Commit

Permalink
Return proper exit code for TERM signal (#1337)
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
shayonj authored and nateberkopec committed Aug 16, 2017
1 parent 8903eea commit 137a80d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/puma/cluster.rb
Expand Up @@ -375,7 +375,10 @@ def setup_signals
log "Early termination of worker"
exit! 0
else
stop_workers
stop

raise SignalException, "SIGTERM"
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/puma/launcher.rb
Expand Up @@ -392,7 +392,9 @@ def setup_signals

begin
Signal.trap "SIGTERM" do
stop
graceful_stop

raise SignalException, "SIGTERM"
end
rescue Exception
log "*** SIGTERM not implemented, signal based gracefully stopping unavailable!"
Expand Down
33 changes: 33 additions & 0 deletions test/test_integration.rb
Expand Up @@ -53,6 +53,21 @@ def server(argv)
@server
end

def start_forked_server(argv)
pid = fork do
exec "#{Gem.ruby} -I lib/ bin/puma -b tcp://127.0.0.1:#{@tcp_port} #{argv}"
end

sleep 5
pid
end

def stop_forked_server(pid)
Process.kill(:TERM, pid)
sleep 1
Process.wait2(pid)
end

def restart_server_and_listen(argv)
skip_on_appveyor
server(argv)
Expand Down Expand Up @@ -218,4 +233,22 @@ 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
skip if Puma.jruby? || Puma.windows?

pid = start_forked_server("test/rackup/hello.ru")
_, status = stop_forked_server(pid)

assert_equal 15, status
end

def test_term_signal_exit_code_in_clustered_mode
skip if Puma.jruby? || Puma.windows?

pid = start_forked_server("-w 2 test/rackup/hello.ru")
_, status = stop_forked_server(pid)

assert_equal 15, status
end
end

0 comments on commit 137a80d

Please sign in to comment.