go
runtime_error
ai_generated
true
fatal error: 所有 goroutine 都处于休眠状态 - 死锁!(nil channel 接收)
fatal error: all goroutines are asleep - deadlock! (nil channel receive)
ID: go/channel-receive-from-nil-channel
80%修复率
87%置信度
0证据数
2024-09-03首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0+ | active | — | — | — |
根因分析
某个 goroutine 尝试从 nil channel 接收数据,这会永久阻塞。如果所有 goroutine 都阻塞在 nil channel 上,运行时会检测到死锁。
English
A goroutine attempted to receive from a nil channel, which blocks forever. If all goroutines are blocked on nil channels, the runtime detects a deadlock.
解决方案
-
98% 成功率
ch := make(chan int) // or ch := make(chan int, 1)
-
90% 成功率
select { case v := <-ch: // use v case <-time.After(1 * time.Second): return errors.New("timeout: channel may be nil") }
无效尝试
常见但无效的做法:
-
90% 失败
A nil channel is not the same as an uninitialized variable; you must explicitly create it with make().
-
85% 失败
A select with a nil channel case will block if no other case is ready; default only helps if you want non-blocking behavior.