go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock! (select with all nil channels)

ID: go/goroutine-select-nil-channel

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active
1.19 active
1.20 active
1.21 active

Root Cause

A select statement where all cases involve nil channels, causing the select to block forever.

generic

中文

一个 select 语句,其中所有 case 都涉及 nil 通道,导致 select 永远阻塞。

Workarounds

  1. 100% success Initialize all channels before select
    ch1 := make(chan int)
    ch2 := make(chan int)
    select {
    case <-ch1:
    case <-ch2:
    }
  2. 90% success Use a non-nil channel as a sentinel
    ch := make(chan int)
    close(ch) // closed channel always returns zero value
    select {
    case <-ch:
    }

Dead Ends

Common approaches that don't work:

  1. Add a default case to select 50% fail

    Default case will execute immediately, which may not be desired; also doesn't fix the nil channel issue.

  2. Check each channel for nil before select 60% fail

    Can become cumbersome; still need to handle nil channels properly.