go
runtime_error
ai_generated
true
致命错误:所有 goroutine 都处于休眠状态 - 死锁!
fatal error: all goroutines are asleep - deadlock!
ID: go/deadlock-unbuffered-channel-single-goroutine
80%修复率
85%置信度
0证据数
2024-03-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.21 | active | — | — | — |
根因分析
在没有接收方的同一 goroutine 中向无缓冲通道发送数据会永久阻塞,导致死锁。
English
Sending on an unbuffered channel without a receiver in the same goroutine blocks forever, causing a deadlock.
解决方案
-
95% 成功率
ch := make(chan int) go func() { ch <- 42 }() value := <-ch fmt.Println(value) -
90% 成功率
ch := make(chan int, 1) ch <- 42 fmt.Println(<-ch)
无效尝试
常见但无效的做法:
-
70% 失败
Only works if the sender doesn't need to wait for a receiver; if the buffer fills, the same deadlock occurs.
-
90% 失败
Sleeping doesn't create a receiver; the goroutine still blocks indefinitely.
-
100% 失败
The program terminates immediately; no recovery is possible.