go
runtime_error
ai_generated
true
fatal error: 所有 goroutine 都处于休眠状态 - 死锁!(在 nil channel 上 select)
fatal error: all goroutines are asleep - deadlock! (select on nil channel)
ID: go/select-on-empty-nil-channel
80%修复率
87%置信度
0证据数
2024-05-21首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0+ | active | — | — | — |
根因分析
select 中只有 nil channel(或单个 nil channel)会永久阻塞,因为 nil channel 永不就绪。常见于条件性地将 channel 赋值为 nil 以禁用某个 case。
English
A select with only nil channels (or a single nil channel) blocks forever because nil channels are never ready. Common when a channel is conditionally assigned nil to disable a case.
解决方案
-
95% 成功率
select { case v := <-ch: handle(v) case <-ctx.Done(): return ctx.Err() } -
92% 成功率
if ch == nil { return ErrNoChannel } select { case v := <-ch: _ = v }
无效尝试
常见但无效的做法:
-
85% 失败
Turns the deadlock into a busy loop, pegging CPU and never making progress.
-
80% 失败
Unbuffered channel still blocks if no sender; same deadlock.