go
runtime_error
ai_generated
true
panic: sync: WaitGroup is reused before previous Wait has returned
ID: go/goroutine-waitgroup-wait-in-loop
80%Fix Rate
84%Confidence
0Evidence
2024-12-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.20 | active | — | — | — |
Root Cause
Calling Add() on a WaitGroup while a previous Wait() is still in progress, causing race.
generic中文
在之前的Wait()仍在进行时对WaitGroup调用Add(),导致竞态条件。
Workarounds
-
95% success Create a new 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 channels to synchronize instead of reusing WaitGroup
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 in this context; program crashes.
-
Adding more Done() calls to unblock Wait
90% fail
Counter imbalance causes negative counter panic.