-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix(pool): data race on Conn.onClose during init vs Close #3966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
saddamr3e
wants to merge
1
commit into
redis:master
Choose a base branch
from
saddamr3e:conn-onclose-race
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+66
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package pool | ||
|
|
||
| import ( | ||
| "context" | ||
| "net" | ||
| "sync" | ||
| "testing" | ||
| ) | ||
|
|
||
| // TestConnOnCloseRaceWithInitConn is a regression test for a data race on | ||
| // Conn.onClose between a connection being (re)initialized and a concurrent | ||
| // Close. | ||
| // | ||
| // baseClient.initConn installs the close callback via SetOnClose (e.g. the | ||
| // StreamingCredentialsProvider unsubscribe). initConn runs inside | ||
| // SetNetConnAndInitConn while the connection is in the INITIALIZING state. | ||
| // Conn.Close transitions to CLOSED from any state, then reads and nils | ||
| // onClose without synchronization. A pool shutdown (or connection removal) | ||
| // that closes a connection whose init is still in flight therefore races the | ||
| // setter. | ||
| func TestConnOnCloseRaceWithInitConn(t *testing.T) { | ||
| iterations := 5000 | ||
| if testing.Short() { | ||
| iterations = 1000 | ||
| } | ||
|
|
||
| for i := 0; i < iterations; i++ { | ||
| c1, c2 := net.Pipe() | ||
|
|
||
| cn := NewConn(c1) | ||
| cn.SetInitConnFunc(func(ctx context.Context, c *Conn) error { | ||
| // Mirror baseClient.initConn: install the close hook, then mark | ||
| // the connection idle so it is ready for use. | ||
| c.SetOnClose(func() error { return nil }) | ||
| c.GetStateMachine().Transition(StateIdle) | ||
| return nil | ||
| }) | ||
|
|
||
| var wg sync.WaitGroup | ||
| wg.Add(2) | ||
| go func() { | ||
| defer wg.Done() | ||
| _ = cn.SetNetConnAndInitConn(context.Background(), c1) | ||
| }() | ||
| go func() { | ||
| defer wg.Done() | ||
| _ = cn.Close() | ||
| }() | ||
| wg.Wait() | ||
|
|
||
| c2.Close() | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
Closewins the race and completesSwap(nil)before an in-flightinitConnreaches this store,SetOnCloseinstalls the streaming-provider unsubscribe callback on an already-closed connection, so no laterClosewill invoke it. This leaves the provider subscribed to a dead connection—the exact shutdown/reinitialization interleaving this change targets—while the new regression test checks only for a data-race report and never verifies callback delivery. Preserve a terminal closed sentinel in the atomic slot or otherwise synchronize installation with the CLOSED transition so a late callback runs immediately.AGENTS.md reference: AGENTS.md:L172-L180
Useful? React with 👍 / 👎.