patch: call modules' exit_process in reverse order

shanlei at asiainfo.com shanlei at asiainfo.com
Sun Apr 24 08:33:04 UTC 2022


# HG changeset patch
# User stdanley <shanlei at asiainfo.com>
# Date 1650788278 -28800
#      Sun Apr 24 16:17:58 2022 +0800
# Node ID 522acbe88486d027383075c8208edd6fcc0a3aa6
# Parent  a736a7a613ea6e182ff86fbadcb98bb0f8891c0b
patch: call modules' exit_process in reverse order to solve module 
dependency.
We once have developed a module which depends on ngx_thread_pool module. 
but nginx hungs when began to shutdown. trace shows that ngx_thread_pool 
cant finish "exit_process" because our module is using a thread, our 
module must
call "exit_process" before ngx_thread_pool.
so we present this patch to solve modules' dependency issue.

diff -r a736a7a613ea -r 522acbe88486 src/os/unix/ngx_process_cycle.c
--- a/src/os/unix/ngx_process_cycle.c   Tue Feb 08 17:35:27 2022 +0300
+++ b/src/os/unix/ngx_process_cycle.c   Sun Apr 24 16:17:58 2022 +0800
@@ -300,10 +300,13 @@
          ngx_process_events_and_timers(cycle);

          if (ngx_terminate || ngx_quit) {
-
-            for (i = 0; cycle->modules[i]; i++) {
-                if (cycle->modules[i]->exit_process) {
-                    cycle->modules[i]->exit_process(cycle);
+            ngx_int_t j, k = 0;
+            //for (i = 0; cycle->modules[i]; i++) {
+            for (j = 0; cycle->modules[j]; j++, k++)
+                ;
+            for (j = k - 1; j >= 0; j--) {
+                if (cycle->modules[j]->exit_process) {
+                    cycle->modules[j]->exit_process(cycle);
                  }
              }

@@ -950,10 +953,14 @@
  {
      ngx_uint_t         i;
      ngx_connection_t  *c;
+    ngx_int_t         j, k=0;

-    for (i = 0; cycle->modules[i]; i++) {
-        if (cycle->modules[i]->exit_process) {
-            cycle->modules[i]->exit_process(cycle);
+    //for (i = 0; cycle->modules[i]; i++) {
+    for (j = 0; cycle->modules[j]; j++, k++)
+        ;
+    for (j = k - 1; j >= 0; j--) {
+        if (cycle->modules[j]->exit_process) {
+            cycle->modules[j]->exit_process(cycle);
          }
      }



More information about the nginx-devel mailing list