go
runtime_error
ai_generated
true
panic: sync: WaitGroup 在上一次 Wait 返回前被复用
panic: sync: WaitGroup is reused before previous Wait has returned
ID: go/panic-sync-waitgroup-is-reused-before-previous-wait-has-re
80%修复率
89%置信度
0证据数
2024-09-19首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.20 | active | — | — | — |
| 1.23 | active | — | — | — |
根因分析
在上一次 wg.Wait() 尚未返回时又调用 wg.Add(),这违反了 WaitGroup 的使用约定。
English
wg.Add() is called again while a previous wg.Wait() is still in progress, which is explicitly disallowed by the WaitGroup contract.
解决方案
-
93% 成功率
Create a fresh WaitGroup per batch: do not reuse the same wg across phases. var wg sync.WaitGroup once per logical group of goroutines.
-
90% 成功率
Use golang.org/x/sync/errgroup where lifecycle is bounded by Wait() returning, and never call Go() after Wait() has been invoked.
无效尝试
常见但无效的做法:
-
85% 失败
The mutex serializes the calls but does not change the illegal reuse pattern; the panic still fires.
-
70% 失败
A naive channel close races with still-running goroutines and may close early, producing send-on-closed panics.