go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock! (blocked in select {})

ID: go/select-no-cases-blocks

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.22 active — — —

Root Cause

An empty select{} blocks the current goroutine forever, and if it is the only goroutine, the runtime deadlocks.

generic

中文

空的 select{} 会永久阻塞当前 goroutine,若它是唯一 goroutine,运行时判定死锁。

Workarounds

  1. 95% success
    ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
    defer stop()
    <-ctx.Done()
  2. 90% success
    done := make(chan struct{})
    go func(){ /* work */ close(done) }()
    <-done
  3. 93% success
    for {
        select {
        case <-ctx.Done(): return
        case msg := <-msgs: handle(msg)
        }
    }

Dead Ends

Common approaches that don't work:

  1. 85% fail

    Busy-waits and burns CPU; still no way to exit cleanly.

  2. 95% fail

    Same deadlock, just a spin loop; runtime still aborts if no other goroutine.

  3. 80% fail

    Moves the block elsewhere; leaked goroutine and no shutdown path.