-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Fix race conditions in the connection pool code. #18447
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
Closed
Closed
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 |
|---|---|---|
|
|
@@ -50,11 +50,17 @@ type waitlist[C Connection] struct { | |
| // The returned connection may _not_ have the requested Setting. This function can | ||
| // also return a `nil` connection even if our context has expired, if the pool has | ||
| // forced an expiration of all waiters in the waitlist. | ||
| func (wl *waitlist[C]) waitForConn(ctx context.Context, setting *Setting) (*Pooled[C], error) { | ||
| func (wl *waitlist[C]) waitForConn(ctx context.Context, setting *Setting, isClosed func() bool) (*Pooled[C], error) { | ||
| elem := wl.nodes.Get().(*list.Element[waiter[C]]) | ||
| elem.Value = waiter[C]{setting: setting, conn: nil, ctx: ctx} | ||
|
|
||
| wl.mu.Lock() | ||
| if isClosed() { | ||
| // if the pool is closed, we can't wait for a connection, so return an error | ||
| wl.nodes.Put(elem) | ||
|
Comment on lines
+58
to
+60
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can the pool be re-opened between these two calls? |
||
| wl.mu.Unlock() | ||
| return nil, ErrConnPoolClosed | ||
| } | ||
| // add ourselves as a waiter at the end of the waitlist | ||
| wl.list.PushBackValue(elem) | ||
| wl.mu.Unlock() | ||
|
|
||
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
Oops, something went wrong.
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.
I wonder, would an alternative approach work to avoid having to lock? What if we still optimistically put it into the stack, but then check after we've completed that if we have since been closed and then remove it again?
So you only pay the price during closing mostly and then having to do any locking or other synchronization?
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.
I'm definitely open to alternative approaches. For your suggestion, don't we have to take a lock on
capacityMuto correctly decide whether the pool is closed or not?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.
We can think about optimistic concurrency approach with epoch validation.
ConnPool can have
epoch atomic.Uint64. We invalidated with increment whenever there is a change likeCloseorCapacity.In
tryReturnConnwe read the epoch value and then read the capacity. Before returning the connection back to the stack we check the epoch again If it is changed we continue otherwise return to the stack.After returning we check again and if there is a change we try to get the connection back, if we receive the connection continue again.
We do not expect capacity to change that frequently.
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.
I am working on an additional change that uses a pool generation number to allow
Close()to return instantly and basically break the link between checked out connections and the pool.I think that could be used for the optimistic concurrency check. I'll try this out and see if I can make this work.