From 3a49714231f9cf30a3657eaae05e63ea8431d8f9 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Tue, 1 Dec 2026 12:51:10 -0500 Subject: [PATCH] fix(handoff): register the kept list's fork handlers before a split takes its lock --- tools/nativeguest/hook/kept.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tools/nativeguest/hook/kept.c b/tools/nativeguest/hook/kept.c index ab35a83b59..a25b3813d4 100644 --- a/tools/nativeguest/hook/kept.c +++ b/tools/nativeguest/hook/kept.c @@ -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);