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
80%Fix Rate
84%Confidence
0Evidence
2025-04-18First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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
-
95% success Add default case to avoid blocking
for { select { case <-ch: // handle default: // non-blocking } } -
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:
-
Adding time.Sleep inside loop
90% fail
Sleep doesn't unblock channels; goroutine still blocked.
-
Using break statement to exit loop
80% fail
Break only exits select, not for loop; goroutine re-enters select and blocks again.