Fix nsync_waiter_wipe_ zeroing mutexes held by surviving thread after fork#1487
Open
igravious wants to merge 1 commit into
Open
Fix nsync_waiter_wipe_ zeroing mutexes held by surviving thread after fork#1487igravious wants to merge 1 commit into
igravious wants to merge 1 commit into
Conversation
… fork nsync_waiter_wipe_() (introduced in 98c5847) unconditionally zeroes every mutex pointed to by wipe_mu after fork(). This is incorrect when the forking (surviving) thread holds the mutex: the child inherits the lock but nsync_mu_unlock() sees word=0 and panics with "attempt to nsync_mu_unlock() an nsync_mu not held in write mode". After fork(), only the forking thread survives, so any held mutex must be held by the survivor. Check MU_WLOCK and MU_RLOCK_FIELD before zeroing: if set, preserve lock state but strip stale waiter metadata (MU_WAITING, MU_WRITER_WAITING, etc.) since those waiters are dead. Apply the same logic to wipe_cv. Reproducer: hold a pthread_mutex, have another thread block on it (creating a wipe_mu waiter), fork(), then unlock in the child. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Co-Authored-By: Kimi <noreply@moonshot.cn>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
nsync_waiter_wipe_()(introduced in 98c5847) unconditionally zeroes every mutex pointed to bywipe_muafterfork(). This is incorrect when the forking (surviving) thread holds the mutex — the child inherits the lock butnsync_mu_unlock()seesword=0and panics:Root cause
wipe_mu = Mfork()nsync_waiter_wipe_()zeroes M's word (from Thread B's waiter)(0 - MU_WLOCK)underflows → panicFix
Before zeroing a mutex in
nsync_waiter_wipe_(), check if it's currently held (MU_WLOCKorMU_RLOCK_FIELDbits set). After fork, only the forking thread survives, so any held mutex must be held by it. For held mutexes: preserve lock state but strip stale waiter metadata (MU_WAITING,MU_WRITER_WAITING, etc.) since those waiters are dead. Same logic applied towipe_cv.Test plan
test/libc/thread/nsync_fork_wipe_test.c— reproduces the exact crash without the fix, passes with ittest_ractor.rbInitial Crypto (RSA/ECDSA) support #1414, Ip6 getifaddrs #1415) which hit this bug via Ractor threads +fork()insideRB_VM_LOCKING()