go runtime_error ai_generated true

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

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

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.16 active — — —
1.19 active — — —
1.22 active — — —

Root Cause

Receiving or sending on a nil channel blocks forever; a channel declared but never initialized with make is nil.

generic

中文

在 nil channel 上接收或发送会永久阻塞;声明但未用 make 初始化的 channel 即为 nil。

Workarounds

  1. 98% success
    ch := make(chan int, 1)
    ch <- 1
    v := <-ch
  2. 90% success
    if ch == nil {
        return errors.New("channel not initialized")
    }

Dead Ends

Common approaches that don't work:

  1. 80% fail

    A nil channel case never becomes ready, so select with default just spins or returns default, never delivering data.

  2. 95% fail

    Closing a nil channel panics with 'close of nil channel'.