Skip to content

Extract fork-safe mutex into ForkSafeMutex - #1035

Open
jnunemaker wants to merge 1 commit into
mainfrom
fix-forked-mutex-reset
Open

Extract fork-safe mutex into ForkSafeMutex#1035
jnunemaker wants to merge 1 commit into
mainfrom
fix-forked-mutex-reset

Conversation

@jnunemaker

Copy link
Copy Markdown
Collaborator

The Memory adapter and Poller each duplicated the same fork-detection dance: track the PID and, on first use in a forked child, unlock the inherited-but-stale mutex before locking it — which risks a ThreadError since the mutex may be held by a thread that no longer exists after fork.

This extracts a shared Flipper::ForkSafeMutex that atomically swaps in a fresh mutex on first use in a new process (via an AtomicReference + compare-and-set) instead of unlocking a stale one. Both Memory and Poller now delegate to it, and Poller#start uses the reset's return value to clear @shutdown_requested on fork. Added specs for the mutex, the memory adapter, and the poller cover the post-fork swap and the reset race.

🤖 Generated with Claude Code

The Memory adapter and Poller each carried their own copy of the
fork-detection dance: track the PID, and on first use in a forked child,
unlock the inherited-but-stale mutex before locking. Unlocking a mutex
held by a thread that no longer exists after fork risks ThreadError.

Replace both with a shared ForkSafeMutex that atomically swaps in a fresh
mutex on first use in a new process instead of unlocking a stale one.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant