go runtime_error ai_generated true

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

ID: go/goroutine-select-on-nil-channels

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0 active
1.22 active

Root Cause

A select statement with all nil channels blocks forever, as nil channels never become ready.

generic

中文

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

Workarounds

  1. 95% success Always include a default case or non-nil channel in select
    select {
    case <-ch1:
    case <-ch2:
    default:
        // avoid deadlock
    }
  2. 90% success Ensure at least one channel is non-nil before select
    if ch1 == nil && ch2 == nil {
        return // or handle
    }
    select {
    case <-ch1:
    case <-ch2:
    }

Dead Ends

Common approaches that don't work:

  1. Adding a default case that does nothing 70% fail

    Default case executes immediately, but if omitted, all-nil select blocks.

  2. Using time.After in select 40% fail

    time.After returns a non-nil channel, so select won't have all nil channels.