go
runtime_error
ai_generated
true
致命错误:所有 goroutine 都处于休眠状态 - 死锁!(goroutine 1 [通道接收(nil 通道)])
fatal error: all goroutines are asleep - deadlock! (goroutine 1 [chan receive (nil chan)])
ID: go/nil-channel-block-forever
80%修复率
87%置信度
0证据数
2024-02-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.22 | active | — | — | — |
根因分析
在 nil 通道上发送或接收会永远阻塞。这发生在通道变量未初始化就使用的情况下。
English
Sending or receiving on a nil channel blocks forever. This happens when a channel variable is not initialized before use.
解决方案
-
95% 成功率
ch := make(chan int) // or with buffer: make(chan int, 10)
-
85% 成功率
var ch chan int select { case v := <-ch: // will not execute if ch is nil case <-time.After(time.Second): // handle timeout }
无效尝试
常见但无效的做法:
-
80% 失败
The default case may execute incorrectly, and the nil channel still blocks if not handled properly.
-
90% 失败
Nil channel is a different type; assigning doesn't initialize it.