go
runtime_error
ai_generated
true
panic: sync: WaitGroup is reused before previous Wait has returned
ID: go/waitgroup-misused-copy
80%Fix Rate
90%Confidence
0Evidence
2024-05-21First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
| 1.23 | active | — | — | — |
Root Cause
wg.Add called again while another goroutine is still in wg.Wait. Typically happens with a loop that reuses the same WaitGroup across iterations without resetting.
generic中文
在另一个 goroutine 仍处于 wg.Wait 时再次调用 wg.Add。通常发生在循环中跨迭代复用同一个 WaitGroup 而未重置。
Workarounds
-
95% success
for _, batch := range batches { var wg sync.WaitGroup for _, x := range batch { wg.Add(1) go func(x int) { defer wg.Done(); work(x) }(x) } wg.Wait() } -
93% success
for _, batch := range batches { g := new(errgroup.Group) for _, x := range batch { x := x; g.Go(func() error { return work(x) }) } if err := g.Wait(); err != nil { return err } }
Dead Ends
Common approaches that don't work:
-
85% fail
Reassigning while Wait is in flight races on the struct; still panics or corrupts state.
-
80% fail
Sleep does not synchronize with Wait's completion; race remains.