[PATCH] Avoided updating of parent PID value after daemonization

Valentin V. Bartenev vbart at wbsrv.ru
Fri Jul 12 15:28:54 UTC 2024


# HG changeset patch
# User Valentin Bartenev <vbart at wbsrv.ru>
# Date 1720793182 -10800
#      Fri Jul 12 17:06:22 2024 +0300
# Node ID d9b53b7e164560a44c9228dfd2c882b619004ed2
# Parent  072ca4906154effb64cbf8209dfe86742ff074d7
Avoided updating of parent PID value after daemonization.

The "ngx_parent" value is only used to detect conditions where the daemon is in
a binary upgrade procedure and the old master process hasn't exited yet.

To do this, it's checked for equality with ngx_getppid().  As a result, if it's
set to the parent process during daemonization, the binary upgrade signal is
ignored with an error: "[crit] the changing binary signal is ignored: you
should shutdown or terminate before either old or new binary's process" until
the parent process is terminated, which may not happen immediately after forking
under load on a slow system.

On the other hand, it should be absolutely safe to upgrade the binary before
the process left after daemonization has finally exited.

Notably, this problem has been observed sporadically in the "binary_upgrade.t"
test, as it sends a USR2 signal immediately after starting the daemon and
checking its pid file.

diff --git a/src/os/unix/ngx_daemon.c b/src/os/unix/ngx_daemon.c
--- a/src/os/unix/ngx_daemon.c
+++ b/src/os/unix/ngx_daemon.c
@@ -26,7 +26,6 @@ ngx_daemon(ngx_log_t *log)
         exit(0);
     }
 
-    ngx_parent = ngx_pid;
     ngx_pid = ngx_getpid();
 
     if (setsid() == -1) {


More information about the nginx-devel mailing list