drpcmanager: fix data race between newStream and Close on WaitGroup#77
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a concurrency bug in drpcmanager.Manager where a sync.WaitGroup increment in newStream could race with wg.Wait() during Manager.Close(). It does this by making “stream registration + wg.Add” atomic with respect to termination/close.
Changes:
- Move
wg.Add(1)intoactiveStreams.Addunder theactiveStreamsmutex, and pass&m.wgfromManager.newStream. - Add a regression test that concurrently calls
Manager.Close()andManager.NewClientStream()under contention. - Update
activeStreamstests to match the updatedAddsignature.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| drpcmanager/manager.go | Passes the manager WaitGroup into stream registration so wg.Add is coordinated with the closed check. |
| drpcmanager/active_streams.go | Updates Add to optionally increment a WaitGroup under the same lock used for the closed check. |
| drpcmanager/manager_test.go | Adds a concurrent Close vs NewClientStream regression test. |
| drpcmanager/active_streams_test.go | Updates callers to the new activeStreams.Add(..., wg) signature. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Move `wg.Add(1)` inside `activeStreams.Add` under its mutex so the increment and the closed check are atomic. This prevents `wg.Add` racing with `wg.Wait` in `Manager.Close` when terminate closes streams before `newStream` finishes registering.
shubhamdhama
approved these changes
Jul 17, 2026
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.
Move
wg.Add(1)insideactiveStreams.Addunder its mutex so theincrement and the closed check are atomic. This prevents
wg.Addracing with
wg.WaitinManager.Closewhen terminate closes streamsbefore
newStreamfinishes registering.