go
runtime_error
ai_generated
true
致命错误:所有协程都处于睡眠状态 - 死锁!(nil通道)
fatal error: all goroutines are asleep - deadlock! (nil channel)
ID: go/goroutine-nil-channel-block
80%修复率
85%置信度
0证据数
2024-03-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.20 | active | — | — | — |
根因分析
向nil通道发送或从nil通道接收会永远阻塞,如果所有协程都卡住会导致死锁。
English
Sending to or receiving from a nil channel blocks forever, causing deadlock if all goroutines are stuck.
解决方案
-
99% 成功率 Initialize channel before use
var ch chan int ch = make(chan int) ch <- 42 // safe
-
85% 成功率 Use select with default to avoid blocking on nil channel
select { case ch <- val: default: // handle nil or full channel }
无效尝试
常见但无效的做法:
-
Checking channel with if ch != nil before operation
50% 失败
Nil check is correct but often omitted due to oversight; still a logic error.
-
Using recover() to handle panic from nil channel
100% 失败
Nil channel doesn't panic; it blocks forever, so recover is useless.