From d8fd2af8826c2d33b23c4ef64b88ecd57910e330 Mon Sep 17 00:00:00 2001 From: Christopher Faulet Date: Wed, 20 Jun 2018 16:22:03 +0200 Subject: [PATCH] BUG/MEDIUM: threads: Use the sync point to check active jobs and exit 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 | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/haproxy.c b/src/haproxy.c index 1c0455c56a..5906f79892 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -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++; } } -- 2.39.5