Fix lost wakeup in FiberSequencer when advance races the combiner#85
Merged
Conversation
The flat-combining drain loop coordinates via combinerState (FREE/BUSY/PENDING) and reads counter to decide which waiters to wake. A producer advances counter then calls drain; finding a combiner already running, it returns and relies on that combiner to observe the advance. counter and combinerState are distinct locations, so this is a StoreLoad (Dekker) handshake - and acquire/release does not provide StoreLoad ordering. The advance could therefore be invisible to the combiner's terminal pass, which would read a stale counter, skip a waiter whose token was just reached, and exit - losing the wakeup permanently. It reproduces under thread oversubscription on both x86-64 and arm64. Add the two seq_cst fences that complete the handshake: the producer half at the top of drain (before observing combinerState) and the combiner half on the PENDING re-loop (before re-reading counter).
There was a problem hiding this comment.
Pull request overview
Fixes a concurrency bug in FiberSequencer where an advance() racing an already-running combiner could result in a permanently lost wakeup due to missing Store→Load ordering between counter and combinerState. The PR completes the intended Dekker-style handshake by adding two seq_cst fences so the combiner cannot miss counter advances that arrive concurrently.
Changes:
- Add a
std::memory_order_seq_cstfence at the start ofdrain()to order a caller’s priorcounteradvance before observing/acting oncombinerState. - Add a
std::memory_order_seq_cstfence on the PENDING re-loop path to ensure the nextcounterload observes advances that caused PENDING. - Minor control-flow refactor in
advance()anddrain()(do/while →for (;;)loop) while preserving semantics.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
The flat-combining drain loop coordinates via combinerState (FREE/BUSY/PENDING) and reads counter to decide which waiters to wake. A producer advances counter then calls drain; finding a combiner already running, it returns and relies on that combiner to observe the advance. counter and combinerState are distinct locations, so this is a StoreLoad (Dekker) handshake - and acquire/release does not provide StoreLoad ordering.
The advance could therefore be invisible to the combiner's terminal pass, which would read a stale counter, skip a waiter whose token was just reached, and exit - losing the wakeup permanently. It reproduces under thread oversubscription on both x86-64 and arm64.
Add the two seq_cst fences that complete the handshake: the producer half at the top of drain (before observing combinerState) and the combiner half on the PENDING re-loop (before re-reading counter).