]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MEDIUM: threads: Use the sync point to check active jobs and exit
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 20 Jun 2018 14:22:03 +0000 (16:22 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 22 Jun 2018 08:16:26 +0000 (10:16 +0200)
When HAProxy is shutting down, it exits the polling loop when there is no jobs
anymore (jobs == 0). When there is no thread, it works pretty well, but when
HAProxy is started with several threads, a thread can decide to exit because
jobs variable reached 0 while another one is processing a task (e.g. a
health-check). At this stage, the running thread could decide to request a
synchronization. But because at least one of them has already gone, the others
will wait infinitly in the sync point and the process will never die.

To fix the bug, when the first thread (and only this one) detects there is no
active jobs anymore, it requests a synchronization. And in the sync point, all
threads will check if jobs variable reached 0 to exit the polling loop.

This patch must be backported in 1.8.

src/haproxy.c

index 1c0455c56a06a2a238b3b92c2efdc585e069cbf2..5906f798923129af8c5a42b09525e36d6deb06ae 100644 (file)
@@ -2372,10 +2372,12 @@ void mworker_pipe_register()
        fd_want_recv(mworker_pipe[0]);
 }
 
-static void sync_poll_loop()
+static int sync_poll_loop()
 {
+       int stop = 0;
+
        if (THREAD_NO_SYNC())
-               return;
+               return stop;
 
        THREAD_ENTER_SYNC();
 
@@ -2389,7 +2391,9 @@ static void sync_poll_loop()
 
        /* *** } */
   exit:
+       stop = (jobs == 0); /* stop when there's nothing left to do */
        THREAD_EXIT_SYNC();
+       return stop;
 }
 
 /* Runs the polling loop */
@@ -2410,9 +2414,10 @@ static void run_poll_loop()
                /* Check if we can expire some tasks */
                next = wake_expired_tasks();
 
-               /* stop when there's nothing left to do */
-               if (jobs == 0)
-                       break;
+               /* the first thread requests a synchronization to exit when
+                * there is no active jobs anymore */
+               if (tid == 0 && jobs == 0)
+                       THREAD_WANT_SYNC();
 
                /* expire immediately if events are pending */
                exp = now_ms;
@@ -2431,7 +2436,9 @@ static void run_poll_loop()
 
 
                /* Synchronize all polling loops */
-               sync_poll_loop();
+               if (sync_poll_loop())
+                       break;
+
                activity[tid].loops++;
        }
 }