BUG/MEDIUM: threads: handle signal queue only in thread 0
Signals were handled in all threads which caused some signals to be lost
from time to time. To avoid complicated lock system (threads+signals),
we prefer handling the signals in one thread avoiding concurrent access.
The side effect of this bug was that some process were not leaving from
time to time during a reload.
This patch must be backported in 1.8.
diff --git a/src/haproxy.c b/src/haproxy.c
index 766758f..96b8abc 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -2402,8 +2402,10 @@
/* Process a few tasks */
process_runnable_tasks();
- /* check if we caught some signals and process them */
- signal_process_queue();
+ /* check if we caught some signals and process them in the
+ first thread */
+ if (tid == 0)
+ signal_process_queue();
/* Check if we can expire some tasks */
next = wake_expired_tasks();
@@ -2418,7 +2420,7 @@
activity[tid].wake_cache++;
else if (active_tasks_mask & tid_bit)
activity[tid].wake_tasks++;
- else if (signal_queue_len)
+ else if (signal_queue_len && tid == 0)
activity[tid].wake_signal++;
else
exp = next;
@@ -3008,6 +3010,7 @@
unsigned int *tids = calloc(global.nbthread, sizeof(unsigned int));
pthread_t *threads = calloc(global.nbthread, sizeof(pthread_t));
int i;
+ sigset_t blocked_sig, old_sig;
THREAD_SYNC_INIT((1UL << global.nbthread) - 1);
@@ -3015,6 +3018,15 @@
for (i = 0; i < global.nbthread; i++)
tids[i] = i;
+ /* ensure the signals will be blocked in every thread */
+ sigfillset(&blocked_sig);
+ sigdelset(&blocked_sig, SIGPROF);
+ sigdelset(&blocked_sig, SIGBUS);
+ sigdelset(&blocked_sig, SIGFPE);
+ sigdelset(&blocked_sig, SIGILL);
+ sigdelset(&blocked_sig, SIGSEGV);
+ pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
+
/* Create nbthread-1 thread. The first thread is the current process */
threads[0] = pthread_self();
for (i = 1; i < global.nbthread; i++)
@@ -3048,6 +3060,9 @@
}
#endif /* !USE_CPU_AFFINITY */
+ /* when multithreading we need to let only the thread 0 handle the signals */
+ pthread_sigmask(SIG_SETMASK, &old_sig, NULL);
+
/* Finally, start the poll loop for the first thread */
run_thread_poll_loop(&tids[0]);