go
runtime_error
ai_generated
true
fatal error: 所有 goroutine 都休眠了 - 死锁!(向 nil channel 发送)
fatal error: all goroutines are asleep - deadlock! (send on nil channel)
ID: go/send-on-nil-channel-blocks
80%修复率
89%置信度
0证据数
2024-05-30首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.22 | active | — | — | — |
根因分析
向 nil channel 发送或接收会永久阻塞。如果所有 goroutine 都这样,运行时会报告死锁。
English
Sending to or receiving from a nil channel blocks forever. If all goroutines do this, the runtime reports deadlock.
解决方案
-
95% 成功率
ch := make(chan int)
-
85% 成功率
if ch == nil { return } ch <- v
无效尝试
常见但无效的做法:
-
70% 失败
Default turns a blocking send into a busy-loop or dropped message, not a fix for the nil channel.
-
85% 失败
More goroutines blocked on nil channel just produce the same deadlock.