go
runtime_error
ai_generated
true
error: select on nil channel blocks forever
ID: go/nil-channel-blocks-forever
80%Fix Rate
88%Confidence
0Evidence
2024-09-12First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.21 | active | — | — | — |
| 1.22 | active | — | — | — |
Root Cause
Sending to or receiving from a nil channel blocks forever. In a select, a nil channel case is never ready, so if all cases are nil, the select blocks indefinitely.
generic中文
向 nil 通道发送或从 nil 通道接收会永久阻塞。在 select 中,nil 通道分支永远不会就绪,若所有分支都是 nil,select 会无限阻塞。
Workarounds
-
95% success
var ch chan int = make(chan int) select { case v := <-ch: _ = v case <-time.After(time.Second): } -
85% success
var never chan struct{} // nil disables the case select { case <-never: case <-ch: }
Dead Ends
Common approaches that don't work:
-
85% fail
With default, the select always takes the default branch, so nothing ever happens; the intended wait is lost.
-
75% fail
The outer select still holds the nil reference; the goroutine's new channel is unrelated.