go
runtime_error
ai_generated
true
恐慌:sync: WaitGroup 在之前 Wait 返回前被重用
panic: sync: WaitGroup is reused before previous Wait has returned
ID: go/panic-sync-waitgroup-reused-before-wait
80%修复率
87%置信度
0证据数
2024-08-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.21 | active | — | — | — |
根因分析
在 Wait() 开始但尚未返回时调用了 WaitGroup.Add(),违反了 Add 不能与 Wait 并发的规则。
English
WaitGroup.Add() is called after Wait() has started but before it returns, violating the rule that Add must not be called concurrently with Wait.
解决方案
-
90% 成功率
Ensure all Add calls happen before any Wait call. Use a single WaitGroup per logical group and don't reuse until Wait returns.
-
92% 成功率
Use errgroup.Group which manages lifecycle safely: g, ctx := errgroup.WithContext(ctx) for ... { g.Go(...) } return g.Wait()
无效尝试
常见但无效的做法:
-
90% 失败
Sleep does not guarantee Wait has returned; race still possible.
-
50% 失败
If batches overlap, the same issue can occur; requires careful lifecycle management.