go runtime_error ai_generated true

fatal error: 所有 goroutine 都已休眠 - 死锁!(向 nil chan 发送)

fatal error: all goroutines are asleep - deadlock! (send on nil chan)

ID: go/send-on-nil-channel-block

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

版本兼容性

版本状态引入弃用备注
1.x active — — —

根因分析

向 nil channel 发送或接收会永久阻塞。通常是 channel 字段未初始化。

English

Sending or receiving on a nil channel blocks forever. Often a channel field was never initialized.

generic

解决方案

  1. 95% 成功率
    func New() *Worker {
        return &Worker{ch: make(chan int, 16)}
    }
  2. 90% 成功率
    if w.ch == nil {
        return errors.New("worker channel not initialized")
    }
    w.ch <- v

无效尝试

常见但无效的做法:

  1. 70% 失败

    Default fires immediately, silently dropping messages instead of fixing the nil.

  2. 65% 失败

    Turns a deadlock into a timeout but the channel is still nil and unusable.