go
runtime_error
ai_generated
true
致命错误:所有 goroutine 都处于休眠状态 - 死锁!
fatal error: all goroutines are asleep - deadlock!
ID: go/deadlock-on-unbuffered-channel-send
80%修复率
85%置信度
0证据数
2024-01-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.21 | active | — | — | — |
根因分析
在同一 goroutine 中向无缓冲通道发送数据而没有对应的接收者会导致死锁。
English
Sending on an unbuffered channel without a corresponding receiver in the same goroutine causes a deadlock.
解决方案
-
95% 成功率 Ensure a separate goroutine receives from the channel before sending
go func() { <-ch }(); ch <- data -
90% 成功率 Use a buffered channel with sufficient capacity
ch := make(chan int, 1); ch <- 42
无效尝试
常见但无效的做法:
-
Adding a small sleep before sending
90% 失败
Sleep does not create a receiver; the send still blocks indefinitely.
-
Using a buffered channel with capacity 1 but still no receiver
80% 失败
Buffered channel only helps if capacity is not exceeded; if no receiver, the send blocks after buffer fills.