From 1ad049b7b60b2c1704b8bb5b94ee4f95d7540b3a Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sun, 3 Apr 2011 13:03:18 -0400 Subject: [PATCH] fix race condition in rsyscall handler the problem: there is a (single-instruction) race condition window between a thread flagging itself dead and decrementing itself from the thread count. if it receives the rsyscall signal at this exact moment, the rsyscall caller will never succeed in signalling enough flags to succeed, and will deadlock forever. in previous versions of musl, the about-to-terminate thread masked all signals prior to decrementing the thread count, but this cost a whole syscall just to account for extremely rare races. the solution is a huge hack: rather than blocking in the signal handler if the thread is dead, modify the signal mask of the saved context and return in order to prevent further signal handling by the dead thread. this allows the dead thread to continue decrementing the thread count (if it had not yet done so) and exiting, even while the live part of the program blocks for rsyscall. --- src/thread/pthread_create.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/thread/pthread_create.c b/src/thread/pthread_create.c index 9df4f712..344b39f8 100644 --- a/src/thread/pthread_create.c +++ b/src/thread/pthread_create.c @@ -85,7 +85,7 @@ static void rsyscall_handler(int sig, siginfo_t *si, void *ctx) /* Threads which have already decremented themselves from the * thread count must not increment rs.cnt or otherwise act. */ if (self->dead) { - __wait(&rs.hold, 0, 1, 1); + sigaddset(&((ucontext_t *)ctx)->uc_sigmask, SIGSYSCALL); return; } -- 2.25.1