go runtime_error ai_generated true

fatal error: 所有 goroutine 都处于休眠状态 - 死锁!

fatal error: all goroutines are asleep - deadlock!

ID: go/channel-deadlock-all-goroutines-asleep

其他格式: JSON · Markdown 中文 · English
80%修复率
90%置信度
0证据数
2024-02-11首次发现

版本兼容性

版本状态引入弃用备注
1.16 active
1.20 active
1.22 active

根因分析

主 goroutine 在通道发送/接收上永久阻塞,且没有其他 goroutine 能够解除阻塞。常见于从无发送者的无缓冲通道接收,或向无接收者的通道发送。

English

The main goroutine blocked forever on a channel send/receive with no other goroutine able to unblock it. Common when receiving from an unbuffered channel with no sender, or sending with no receiver.

generic

解决方案

  1. 90% 成功率
    ch := make(chan int, 1)
    ch <- 1 // non-blocking
    v := <-ch
  2. 85% 成功率
    select {
    case v := <-ch:
        use(v)
    case <-time.After(2 * time.Second):
        return errors.New("timeout")
    }

无效尝试

常见但无效的做法:

  1. 70% 失败

    Buffering only delays the deadlock. Once the buffer fills, the same blocking occurs; if the logic is fundamentally single-goroutine, it deadlocks regardless.

  2. 85% 失败

    Sleeps don't create concurrency; if there is no sender goroutine at all, no amount of waiting helps.