go runtime_error ai_generated true

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

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-09-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.x active — — —

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 70% fail

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

  2. 65% fail

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