go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock! (nil channel)

ID: go/channel-nil-block-forever

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-04-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0+ active — — —

Root Cause

Sending or receiving on a nil channel blocks forever, leading to deadlock if all goroutines are blocked.

generic

中文

在 nil channel 上发送或接收会永远阻塞,如果所有 goroutine 都阻塞,则导致死锁。

Workarounds

  1. 98% success
    ch := make(chan int)
  2. 90% success
    if ch != nil {
        ch <- v
    }

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Timeout on nil channel still blocks; select with timeout won't trigger

  2. 70% fail

    Default case may cause busy loop