go runtime_error ai_generated true

fatal error: 所有 goroutine 都休眠 - 死锁!(阻塞在 chan receive)

fatal error: all goroutines are asleep - deadlock! (blocked on chan receive)

ID: go/fatal-error-all-goroutines-are-asleep-deadlock-blocked-

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

版本兼容性

版本状态引入弃用备注
1.18 active — — —
1.21 active — — —
1.23 active — — —

根因分析

main 从某个 channel 接收,但没有任何 goroutine 会向其发送,例如生产者 goroutine 因错误提前退出,或使用了 nil channel。

English

main receives from a channel that no goroutine ever sends to, e.g. a producer goroutine exited early due to an error or a nil channel was used.

generic

解决方案

  1. 94% 成功率
    ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
    defer cancel()
    select {
    case v := <-ch:
        use(v)
    case <-ctx.Done():
        return ctx.Err()
    }
  2. 92% 成功率
    go func() {
        defer close(ch)
        for _, x := range items {
            ch <- x
        }
    }()
    for v := range ch { use(v) }

无效尝试

常见但无效的做法:

  1. 85% 失败

    Buffer only helps if a sender exists; with no sender, receive still blocks forever.

  2. 90% 失败

    An empty select blocks forever too, converting deadlock into a silent hang.