go
runtime_error
ai_generated
true
panic: sync: WaitGroup 在之前的 Wait 返回前被重用
panic: sync: WaitGroup is reused before previous Wait has returned
ID: go/waitgroup-reuse-before-wait
80%修复率
86%置信度
0证据数
2024-12-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0+ | active | — | — | — |
根因分析
在上一次 wg.Wait 返回之前又调用了 wg.Add,说明 WaitGroup 被并发重用。
English
wg.Add is called again before a prior wg.Wait has returned, indicating the WaitGroup is being reused concurrently.
解决方案
-
94% 成功率
for _, batch := range batches { var wg sync.WaitGroup for _, item := range batch { wg.Add(1) go func(i Item) { defer wg.Done(); work(i) }(item) } wg.Wait() } -
90% 成功率
g := new(errgroup.Group) for _, item := range batch { g.Go(func() error { return work(item) }) } g.Wait()
无效尝试
常见但无效的做法:
-
80% 失败
Sleep cannot guarantee goroutine completion and reintroduces races.
-
75% 失败
Reassigning wg while goroutines hold a reference to the old one causes Done to target the wrong counter.