go
runtime_error
ai_generated
true
致命错误:所有 goroutine 都处于休眠状态 - 死锁!(无缓冲通道发送无接收者)
fatal error: all goroutines are asleep - deadlock! (unbuffered channel send without receiver)
ID: go/unbuffered-channel-deadlock-single-goroutine
80%修复率
85%置信度
0证据数
2024-10-01首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.1 | active | — | — | — |
根因分析
goroutine 尝试在无缓冲通道上发送,但没有相应的接收者准备好,导致其永远阻塞。如果所有 goroutine 都阻塞,程序将死锁。
English
A goroutine tries to send on an unbuffered channel without a corresponding receiver ready, causing it to block forever. If all goroutines are blocked, the program deadlocks.
解决方案
-
95% 成功率 Ensure the receiver is ready before sending
ch := make(chan int) go func() { val := <-ch fmt.Println(val) }() ch <- 42 -
90% 成功率 Use a select with a default case to make send non-blocking
select { case ch <- 42: default: fmt.Println("no receiver, dropping message") }
无效尝试
常见但无效的做法:
-
Using a buffered channel with size 1 to avoid blocking
85% 失败
A buffered channel can hold one value, but if the receiver never reads, the buffer will fill up and subsequent sends will block, leading to the same deadlock.
-
Adding a time.Sleep before sending to wait for receiver
90% 失败
Sleep is unreliable; the receiver may not be ready in time, and it introduces unnecessary delays.