go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock! (goroutine 1 [select (no cases))])

ID: go/select-on-nil-channel

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.19 active
1.22 active

Root Cause

A select statement with all nil channels blocks forever because nil channels are never ready.

generic

中文

所有通道都是 nil 的 select 语句会永远阻塞,因为 nil 通道永远不会就绪。

Workarounds

  1. 95% success
    ch1 := make(chan int)
    ch2 := make(chan int)
    select {
    case v := <-ch1:
        //
    case v := <-ch2:
        //
    }
  2. 90% success
    select {
    case v := <-ch:
        //
    case <-time.After(time.Second):
        // timeout
    }

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Default case may execute incorrectly and not handle the intended communication.

  2. 90% fail

    Sleeping doesn't resolve the block; the select still blocks.