go
runtime_error
ai_generated
true
fatal error: all goroutines are asleep - deadlock! (context canceled)
ID: go/goroutine-context-cancel-not-checked
80%Fix Rate
87%Confidence
0Evidence
2026-02-14First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.7 | active | — | — | — |
| 1.21 | active | — | — | — |
Root Cause
Goroutines block on channel operations after context is canceled, without checking cancellation.
generic中文
协程在上下文被取消后阻塞在通道操作上,没有检查取消状态。
Workarounds
-
95% success Always include ctx.Done() in select
select { case <-ch: case <-ctx.Done(): return ctx.Err() } -
90% success Use context.WithTimeout to automatically cancel
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() select { case <-ch: case <-ctx.Done(): // timeout }
Dead Ends
Common approaches that don't work:
-
Ignoring context in select
90% fail
Context cancellation doesn't automatically unblock other channels.
-
Using time.Sleep to wait for cancellation
80% fail
Sleep doesn't handle cancellation; goroutine still blocks.