fix(pool): prevent FIFO ordering violation in notifyWaiters by tracking state locally#3777
Merged
Merged
Conversation
ofekshenawa
approved these changes
Apr 15, 2026
ofekshenawa
left a comment
Collaborator
There was a problem hiding this comment.
Thanks @0x48core , LGTM!
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
ConnStateMachine.notifyWaiters()that breaks FIFO execution ordering of queued waitersProblem
notifyWaiters()re-reads the atomic state (sm.GetState()) on every iteration of its outer loop. When a woken goroutine runs fast enough to callTransition(StateIdle)before the loop iterates again, the function sees the new state and wakes the next waiter within the same mutex hold. This cascades — potentially notifying all queued waiters in a single call. Since they all become runnable simultaneously, the Go scheduler executes them in arbitrary order, violating the FIFO guarantee.Example: With waiters queued as
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], the observed execution order was[0, 1, 9, 2, 3, 4, 5, 6, 7, 8].This caused flaky failures in TestConnStateMachine_FIFOOrdering across multiple CI matrix entries (8.0.x stable, 8.2.x 1.25.x, 8.4.x stable).
Fix
Read the state once at entry, and after a successful CAS, update a local state variable to
w.targetStateinstead of re-reading the atomic. This ensuresnotifyWaitersonly considers transitions made within its own call, not concurrent transitions from woken goroutines. The CAS failure path still re-reads the atomic (correctly — an external change requires the real value).Note
Medium Risk
Touches concurrency/state-transition logic in
notifyWaiters, where subtle ordering or CAS behavior changes could impact pool fairness or cause missed wakeups under load.Overview
Fixes a race in
ConnStateMachine.notifyWaitersthat could wake multiple queued waiters in a single mutex hold and let the scheduler run them out of FIFO order.notifyWaitersnow snapshots state once per call and updates a localcurrentStateonly on successful CAS transitions, only re-reading the atomic state on CAS failure so concurrent transitions by woken goroutines don’t cascade additional wakeups.Reviewed by Cursor Bugbot for commit 4381942. Bugbot is set up for automated code reviews on this repo. Configure here.