Skip to content
This repository has been archived by the owner on Aug 5, 2021. It is now read-only.

Commit

Permalink
trap death signals in the worker sooner
Browse files Browse the repository at this point in the history
This helps close a race condition preventing shutdown if
loading the application (preload_app=false) takes a long
time and the user decides to kil workers instead.
  • Loading branch information
Eric Wong committed Aug 2, 2011
1 parent 6d56d7a commit 406b8b0
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/unicorn/http_server.rb
Expand Up @@ -535,12 +535,17 @@ def process_client(client)
handle_error(client, e)
end

EXIT_SIGS = [ :QUIT, :TERM, :INT ]
WORKER_QUEUE_SIGS = QUEUE_SIGS - EXIT_SIGS

# gets rid of stuff the worker has no business keeping track of
# to free some resources and drops all sig handlers.
# traps for USR1, USR2, and HUP may be set in the after_fork Proc
# by the user.
def init_worker_process(worker)
QUEUE_SIGS.each { |sig| trap(sig, nil) }
# we'll re-trap :QUIT later for graceful shutdown iff we accept clients
EXIT_SIGS.each { |sig| trap(sig) { exit!(0) } }
WORKER_QUEUE_SIGS.each { |sig| trap(sig, nil) }
trap(:CHLD, 'DEFAULT')
SIG_QUEUE.clear
proc_name "worker[#{worker.nr}]"
Expand Down Expand Up @@ -578,7 +583,6 @@ def worker_loop(worker)
# closing anything we IO.select on will raise EBADF
trap(:USR1) { nr = -65536; SELF_PIPE[0].close rescue nil }
trap(:QUIT) { worker = nil; LISTENERS.each { |s| s.close rescue nil }.clear }
[:TERM, :INT].each { |sig| trap(sig) { exit!(0) } } # instant shutdown
logger.info "worker=#{worker.nr} ready"

begin
Expand Down

0 comments on commit 406b8b0

Please sign in to comment.