Fix WaitGroup race between goAsyncWithSem and Close in DualWriter#355
Merged
Conversation
…Wait race in DualWriter Co-authored-by: bootjp <1306365+bootjp@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] [WIP] Address feedback on dual-write Redis proxy implementation
Fix WaitGroup race between goAsyncWithSem and Close in DualWriter
Mar 19, 2026
Owner
|
@copilot どうして wait group を使ってるんですかmutexだけではダメですか? |
Contributor
Author
ミューテックスだけでは足りない理由は、「すでに起動中のゴルーチンが終わるまで待つ」機能が必要だからです。
代替として WaitGroup は「N 個のゴルーチンの完了を待つ」という Go の慣用パターンであり、ここでは mutex との組み合わせが最もシンプルかつ正確な実装です。 |
bootjp
approved these changes
Mar 19, 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.
DualWriter.Close()could panic with"WaitGroup misuse: Add called concurrently with Wait"because the atomicclosingflag check andwg.Add(1)ingoAsyncWithSemwere not atomic —Close()could startwg.Wait()in the gap between them.Changes
proxy/dualwrite.go: Replaceclosing int32(atomic) with async.Mutex-protectedclosed bool. Hold the mutex across both the closed-flag check andwg.Add(1)ingoAsyncWithSem;Close()setsclosed = trueunder the same mutex before callingwg.Wait().proxy/proxy_test.go: AddTestDualWriter_Close_NoWaitGroupRace— 50 goroutines hammeringgoAsyncconcurrently withClose(), run under-race.📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.