go runtime_error ai_generated true

致命错误:所有 goroutine 都处于休眠状态 - 死锁!

fatal error: all goroutines are asleep - deadlock!

ID: go/deadlock-on-unbuffered-channel-send

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  1. 95% 成功率 Ensure a separate goroutine receives from the channel before sending
    go func() { <-ch }(); ch <- data
  2. 90% 成功率 Use a buffered channel with sufficient capacity
    ch := make(chan int, 1); ch <- 42

无效尝试

常见但无效的做法:

  1. Adding a small sleep before sending 90% 失败

    Sleep does not create a receiver; the send still blocks indefinitely.

  2. 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.