go runtime_error ai_generated true

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

ID: go/goroutine-nil-channel-block

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-03-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0 active
1.20 active

Root Cause

Sending to or receiving from a nil channel blocks forever, causing deadlock if all goroutines are stuck.

generic

中文

向nil通道发送或从nil通道接收会永远阻塞,如果所有协程都卡住会导致死锁。

Workarounds

  1. 99% success Initialize channel before use
    var ch chan int
    ch = make(chan int)
    ch <- 42 // safe
  2. 85% success Use select with default to avoid blocking on nil channel
    select {
    case ch <- val:
    default:
        // handle nil or full channel
    }

Dead Ends

Common approaches that don't work:

  1. Checking channel with if ch != nil before operation 50% fail

    Nil check is correct but often omitted due to oversight; still a logic error.

  2. Using recover() to handle panic from nil channel 100% fail

    Nil channel doesn't panic; it blocks forever, so recover is useless.