[PATCH] initialize signal handlers before writing pid file

Iñaki Baz Castillo ibc at aliax.net
Wed Jan 20 03:45:33 EST 2010

El Miércoles, 20 de Enero de 2010, Eric Wong escribió:
> This prevents trigger-happy init scripts from reading the pid
> file (and thus sending signals) to a not-fully initialized
> master process to handle them.
> This does NOT fix anything if other processes are sending
> signals prematurely without relying on the presence of the pid
> file.  It's not possible to prevent all cases of this in one
> process, even in a purely C application, so we won't bother
> trying.
> We continue to always defer signal handling to the main loop
> anyways, and signals sent to the master process will be
> deferred/ignored until Unicorn::HttpServer#join is run.

Hi Eric, I've already tested it. It works great :)

To check it I use the following init script "reload" action which hope could 
be useful for others (it requires loading "/lib/lsb/init-functions"):

  # Return
  #   0 if daemon has been reloaded
  #   1 if daemon was not running
  #   1 if daemon could not be reloaded
  log_daemon_msg "Safe reloading $DESC"

  oldpid=$(pidofproc -p $PIDFILE)
  if [ "$oldpid" ] ; then
    kill -s USR2 $oldpid
    log_action_msg "signal USR2 sent to $NAME master process (pid $oldpid) to
                    fork a new master and workers processes"

    log_action_msg "waiting for the new process to successfully start..."
    printf "."
    sleep 1
    while [ "$(pidofproc -p $PIDFILE.oldbin)" ]; do
      printf "."
      sleep 1

    newpid=$(pidofproc -p $PIDFILE)
    # If the PID has definitively changed than the new master is running.
    if [ "$oldpid" != "$newpid" ] ; then
      log_success_msg "server correctly reloaded, new master process has pid
      log_end_msg 0
    # If not, the new master process couldn't start due to the new
    # configuration or code error.
      log_failure_msg "error running a new master process, $NAME was not
                       reloaded (check error log)"
      log_end_msg 1
    log_failure_msg "$NAME is not running"
    log_end_msg 1

And I test the following loop to check it:

  while [ 0 ] ; do /etc/init.d/myserver reload ; done

Iñaki Baz Castillo <ibc at aliax.net>

More information about the mongrel-unicorn mailing list