go
runtime_error
ai_generated
true
致命错误:所有协程都处于睡眠状态 - 死锁!(上下文已取消)
fatal error: all goroutines are asleep - deadlock! (context canceled)
ID: go/goroutine-context-cancel-not-checked
80%修复率
87%置信度
0证据数
2026-02-14首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.7 | active | — | — | — |
| 1.21 | active | — | — | — |
根因分析
协程在上下文被取消后阻塞在通道操作上,没有检查取消状态。
English
Goroutines block on channel operations after context is canceled, without checking cancellation.
解决方案
-
95% 成功率 Always include ctx.Done() in select
select { case <-ch: case <-ctx.Done(): return ctx.Err() } -
90% 成功率 Use context.WithTimeout to automatically cancel
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() select { case <-ch: case <-ctx.Done(): // timeout }
无效尝试
常见但无效的做法:
-
Ignoring context in select
90% 失败
Context cancellation doesn't automatically unblock other channels.
-
Using time.Sleep to wait for cancellation
80% 失败
Sleep doesn't handle cancellation; goroutine still blocks.