go
runtime_error
ai_generated
true
致命错误:所有协程都在休眠 - 死锁!(WaitGroup)
fatal error: all goroutines are asleep - deadlock! (WaitGroup)
ID: go/goroutine-leak-on-waitgroup
80%修复率
83%置信度
0证据数
2024-08-18首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
根因分析
WaitGroup的计数器未归零,主goroutine调用Wait,但子goroutine因阻塞而无法完成。
English
WaitGroup的计数器未归零,主goroutine调用Wait,但子goroutine因阻塞而无法完成。
解决方案
-
85% 成功率
使用select和done通道通知退出
-
90% 成功率
ctx, cancel := context.WithCancel(context.Background()); go func(){ select { case <-ctx.Done(): return } }()
无效尝试
常见但无效的做法:
-
60% 失败
超时后goroutine仍在运行,程序可能退出但资源泄漏。
-
70% 失败
关闭通道可能引发其他panic,且未解决阻塞原因。