[patch]worker process: make sure to quit when master quits

李林哲 llz at antiy.cn
Thu Oct 13 01:23:09 UTC 2016


# HG changeset patch
# User lilinzhe <llz at antiy.cn>
# Date 1476321680 -28800
# Node ID d13daa14b1df9e04d7bf339c1669740edf19de0e
# Parent  7cdf69d012f02a80c94d930b29c71847e203e4d6
worker process: make sure to quit when master quits

when theres prctl(PR_SET_PDEATHSIG). make worker to use it.

on linux system OOM, it might send SIGKILL to master, and make master quits. some daemon program might try to
restart nginx. but when worker exists. the restart will fail for port in use problem. this commit fixs this problem.

diff -r 7cdf69d012f0 -r d13daa14b1df auto/os/linux
--- a/auto/os/linux	Tue Oct 11 18:03:01 2016 +0300
+++ b/auto/os/linux	Thu Oct 13 09:21:20 2016 +0800
@@ -157,6 +157,18 @@
 . auto/feature
 
 
+# prctl(PR_SET_PDEATHSIG)
+
+ngx_feature="prctl(PR_SET_PDEATHSIG)"
+ngx_feature_name="NGX_HAVE_PR_SET_PDEATHSIG"
+ngx_feature_run=yes
+ngx_feature_incs="#include <sys/prctl.h>
+                  #include <signal.h>"
+ngx_feature_path=
+ngx_feature_libs=
+ngx_feature_test="if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) == -1) return 1"
+. auto/feature
+
 # sched_setaffinity()
 
 ngx_feature="sched_setaffinity()"
diff -r 7cdf69d012f0 -r d13daa14b1df src/os/unix/ngx_process_cycle.c
--- a/src/os/unix/ngx_process_cycle.c	Tue Oct 11 18:03:01 2016 +0300
+++ b/src/os/unix/ngx_process_cycle.c	Thu Oct 13 09:21:20 2016 +0800
@@ -869,6 +869,15 @@
 
 #endif
 
+#if(NGX_HAVE_PR_SET_PDEATHSIG)
+    /*
+     * when system OOM and send SIGKILL to master. make slave receive SIGTERM to quit.
+     */
+    if(prctl(PR_SET_PDEATHSIG, SIGTERM) == -1){
+        ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, "prctl PR_SET_PDEATHSIG failed");
+    }
+#endif
+
     if (ccf->working_directory.len) {
         if (chdir((char *) ccf->working_directory.data) == -1) {
             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,


More information about the nginx-devel mailing list