go
runtime_error
ai_generated
true
恐慌:同步:WaitGroup计数器为负
panic: sync: negative WaitGroup counter
ID: go/waitgroup-negative-counter
80%修复率
92%置信度
0证据数
2024-03-01首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.22 | active | — | — | — |
根因分析
调用Done()的次数多于Add(),导致WaitGroup计数器变为负数。
English
Calling Done() more times than Add(), causing the WaitGroup counter to go negative.
解决方案
-
95% 成功率
Ensure Add is called before starting goroutines and Done is called exactly once per goroutine.
-
90% 成功率
Use a defer statement right after Add to guarantee Done is called.
无效尝试
常见但无效的做法:
-
100% 失败
The panic will crash the program; ignoring is not possible.
-
80% 失败
This masks the bug and may cause Wait() to return prematurely.
-
70% 失败
If Add is not called, Done will still decrement and may go negative.