go runtime_error ai_generated true

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

ID: go/select-on-empty-nil-channel

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0+ active — — —

Root Cause

A select with only nil channels (or a single nil channel) blocks forever because nil channels are never ready. Common when a channel is conditionally assigned nil to disable a case.

generic

中文

select 中只有 nil channel(或单个 nil channel)会永久阻塞,因为 nil channel 永不就绪。常见于条件性地将 channel 赋值为 nil 以禁用某个 case。

Workarounds

  1. 95% success
    select {
    case v := <-ch:
        handle(v)
    case <-ctx.Done():
        return ctx.Err()
    }
  2. 92% success
    if ch == nil { return ErrNoChannel }
    select { case v := <-ch: _ = v }

Dead Ends

Common approaches that don't work:

  1. 85% fail

    Turns the deadlock into a busy loop, pegging CPU and never making progress.

  2. 80% fail

    Unbuffered channel still blocks if no sender; same deadlock.