Skip to content

Commit 4f46a35

Browse files
committed
BUG/MINOR: haproxy/threads: close a possible race in soft-stop detection
Commit 4b3f27b ("BUG/MINOR: haproxy/threads: try to make all threads leave together") improved the soft-stop synchronization but it left a small race open because it looks at tasks_run_queue, which can drop to zero then back to one while another thread picks the task from the run queue to insert it into the tasklet_list. The risk is very low but not null. In addition the condition didn't consider the possible presence of signals in the queue. This patch moves the stopping detection just after the "wake" calculation which already takes care of the various queues' sizes and signals. It avoids needlessly duplicating these tests. The bug was discovered during a code review but will probably never be observed. This fix may be backported to 2.1 and 2.0 along with the commit above.
1 parent ce6fc25 commit 4f46a35

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/haproxy.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2806,14 +2806,6 @@ void run_poll_loop()
28062806
if (tid == 0)
28072807
signal_process_queue();
28082808

2809-
if (stopping && tasks_run_queue == 0)
2810-
_HA_ATOMIC_OR(&stopping_thread_mask, tid_bit);
2811-
2812-
/* stop when there's nothing left to do */
2813-
if ((jobs - unstoppable_jobs) == 0 && tasks_run_queue == 0 &&
2814-
(stopping_thread_mask & all_threads_mask) == all_threads_mask)
2815-
break;
2816-
28172809
/* also stop if we failed to cleanly stop all tasks */
28182810
if (killed > 1)
28192811
break;
@@ -2834,6 +2826,16 @@ void run_poll_loop()
28342826
wake = 0;
28352827
}
28362828

2829+
if (!wake) {
2830+
if (stopping)
2831+
_HA_ATOMIC_OR(&stopping_thread_mask, tid_bit);
2832+
2833+
/* stop when there's nothing left to do */
2834+
if ((jobs - unstoppable_jobs) == 0 &&
2835+
(stopping_thread_mask & all_threads_mask) == all_threads_mask)
2836+
break;
2837+
}
2838+
28372839
/* If we have to sleep, measure how long */
28382840
next = wake ? TICK_ETERNITY : next_timer_expiry();
28392841

0 commit comments

Comments
 (0)