go
runtime_error
ai_generated
true
fatal error: all goroutines are asleep - deadlock! (select on nil channels)
ID: go/goroutine-select-on-nil-channels
80%Fix Rate
83%Confidence
0Evidence
2024-05-12First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.22 | active | — | — | — |
Root Cause
A select statement with all nil channels blocks forever, as nil channels never become ready.
generic中文
所有通道都为nil的select语句永远阻塞,因为nil通道永远不会就绪。
Workarounds
-
95% success Always include a default case or non-nil channel in select
select { case <-ch1: case <-ch2: default: // avoid deadlock } -
90% success Ensure at least one channel is non-nil before select
if ch1 == nil && ch2 == nil { return // or handle } select { case <-ch1: case <-ch2: }
Dead Ends
Common approaches that don't work:
-
Adding a default case that does nothing
70% fail
Default case executes immediately, but if omitted, all-nil select blocks.
-
Using time.After in select
40% fail
time.After returns a non-nil channel, so select won't have all nil channels.