From f7afdd910b09fd334585e62fc118d097be716350 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 1 Jul 2022 17:19:14 +0200 Subject: [PATCH] MINOR: debug: mark oneself harmless while waiting for threads to finish The debug_handler() function waits for other threads to join, but does not mark itself as harmless, so if at the same time another thread tries to isolate, this may deadlock. In practice this does not happen as the signal is received during epoll_wait() hence under harmless mode, but it can possibly arrive under other conditions. In order to improve this, while waiting for other threads to join, we're now marking the current thread as harmless, as it's doing nothing but waiting for the other ones. This way another harmless waiter will be able to proceed. It's valid to do this since we're not doing anything else in this loop. One improvement could be to also check for the thread being idle and marking it idle in addition to harmless, so that it can even release a full isolation requester. But that really doesn't look worth it. --- src/debug.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/debug.c b/src/debug.c index 3abcc0f00b..39a6e02313 100644 --- a/src/debug.c +++ b/src/debug.c @@ -1340,9 +1340,15 @@ void debug_handler(int sig, siginfo_t *si, void *arg) */ /* wait for all previous threads to finish first */ + if (!harmless) + thread_harmless_now(); + while (threads_to_dump & (tid_bit - 1)) ha_thread_relax(); + if (!harmless) + thread_harmless_end(); + /* dump if needed */ if (threads_to_dump & tid_bit) { if (thread_dump_buffer) @@ -1361,8 +1367,15 @@ void debug_handler(int sig, siginfo_t *si, void *arg) * present again. This way the threads_to_dump variable never passes to * zero until all visitors have stopped waiting. */ + if (!harmless) + thread_harmless_now(); + while (!(threads_to_dump & tid_bit)) ha_thread_relax(); + + if (!harmless) + thread_harmless_end(); + HA_ATOMIC_AND(&threads_to_dump, ~tid_bit); /* mark the current thread as stuck to detect it upon next invocation -- 2.39.5