fix(handoff): register the kept list's fork handlers before a split takes its lock

This commit is contained in:
Kurt Himebauch committed 2026-12-01 12:51:10 -05:00
1 parent 840ac0bad6
commit 3a49714231
1 file changed
+8 -2
+8 -2
View File
@@ -56,12 +56,17 @@ static void kept_fork_prepare(void) { pthread_mutex_lock(&g_kept_lock); }
static void kept_fork_done(void) { pthread_mutex_unlock(&g_kept_lock); }
static void kept_register_fork(void) { pthread_atfork(kept_fork_prepare, kept_fork_done, kept_fork_done); }
// Before anything takes g_kept_lock: a fork() in another thread then waits for it to be let go.
static void kept_register_once(void) {
static pthread_once_t once = PTHREAD_ONCE_INIT;
pthread_once(&once, kept_register_fork);
}
// Every signal blocked meanwhile, so that no handler on this thread reads the list half changed (it
// would wait for the change forever) or changes it too; a fork() waits for the change to end, so a
// child never starts with one under way.
static void kept_change_begin(void) {
static pthread_once_t once = PTHREAD_ONCE_INIT;
pthread_once(&once, kept_register_fork);
kept_register_once();
uint64_t all = ~0ull;
call_orig_syscall(SYS_rt_sigprocmask, SIG_SETMASK, (unsigned long)&all, (unsigned long)&t_kept_mask, 8, 0, 0);
pthread_mutex_lock(&g_kept_lock);
@@ -101,6 +106,7 @@ static int kept_threads(void) {
// a thread with a table of its own starts later share that table but not its copy: not followed.
static int kept_split(int (*split)(void)) {
if (t_kept_split) return split();
kept_register_once();
uint64_t all = ~0ull, mask = 0;
call_orig_syscall(SYS_rt_sigprocmask, SIG_SETMASK, (unsigned long)&all, (unsigned long)&mask, 8, 0, 0);
pthread_mutex_lock(&g_kept_lock);