go
runtime_error
ai_generated
true
panic: sync: negative WaitGroup counter
ID: go/waitgroup-add-positive-delta
80%Fix Rate
88%Confidence
0Evidence
2024-02-14First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.20 | active | — | — | — |
| 1.22 | active | — | — | — |
Root Cause
wg.Done() is called more times than wg.Add(). Usually a missing Add before launching a goroutine, a double defer wg.Done(), or Done called in a loop path that runs more than the added count.
generic中文
wg.Done() 的调用次数多于 wg.Add()。通常是因为启动 goroutine 前漏了 Add、重复 defer wg.Done(),或 Done 在循环路径中执行次数超过 Add 的数量。
Workarounds
-
96% success
wg.Add(1) go func() { defer wg.Done() work() }() -
90% success
func launch(wg *sync.WaitGroup, f func()) { wg.Add(1) go func() { defer wg.Done(); f() }() }
Dead Ends
Common approaches that don't work:
-
70% fail
Masks the real double-Done bug and can cause Wait to hang forever if the count never reaches zero.
-
90% fail
Wait then blocks forever because the counter never decrements.