go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock! (for-select with no default)

ID: go/goroutine-for-select-no-default

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2025-04-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0 active
1.20 active

Root Cause

A for-select loop with no default case and all channels blocked causes goroutine to sleep forever.

generic

中文

一个没有default情况的for-select循环,所有通道都阻塞,导致协程永远睡眠。

Workarounds

  1. 95% success Add default case to avoid blocking
    for {
        select {
        case <-ch:
            // handle
        default:
            // non-blocking
        }
    }
  2. 90% success Use a done channel to break out of loop
    done := make(chan struct{})
    for {
        select {
        case <-ch:
            // handle
        case <-done:
            return
        }
    }

Dead Ends

Common approaches that don't work:

  1. Adding time.Sleep inside loop 90% fail

    Sleep doesn't unblock channels; goroutine still blocked.

  2. Using break statement to exit loop 80% fail

    Break only exits select, not for loop; goroutine re-enters select and blocks again.