go
runtime_error
ai_generated
true
panic: sync: WaitGroup is reused before previous Wait has returned (racy close)
ID: go/goroutine-racy-close-waitgroup
80%Fix Rate
84%Confidence
0Evidence
2026-05-22First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.21 | active | — | — | — |
Root Cause
Calling WaitGroup.Wait() and then Add() concurrently from multiple goroutines.
generic中文
从多个协程并发调用WaitGroup.Wait()然后Add()。
Workarounds
-
95% success Use separate WaitGroup for each batch
for batch := 0; batch < 3; batch++ { var wg sync.WaitGroup for i := 0; i < 5; i++ { wg.Add(1) go func() { defer wg.Done(); /* work */ }() } wg.Wait() } -
85% success Use channel-based synchronization
ch := make(chan struct{}) for i := 0; i < 5; i++ { go func() { /* work */; ch <- struct{}{} }() } for i := 0; i < 5; i++ { <-ch }
Dead Ends
Common approaches that don't work:
-
Using recover() to catch panic
100% fail
Panic is unrecoverable; program crashes.
-
Adding time.Sleep between Wait and Add
90% fail
Race condition still exists; timing-dependent.