go
runtime_error
ai_generated
true
fatal error: all goroutines are asleep - deadlock! (blocked in select {})
ID: go/select-no-cases-blocks
80%Fix Rate
90%Confidence
0Evidence
2024-05-21First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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
-
95% success
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop() <-ctx.Done()
-
90% success
done := make(chan struct{}) go func(){ /* work */ close(done) }() <-done -
93% success
for { select { case <-ctx.Done(): return case msg := <-msgs: handle(msg) } }
Dead Ends
Common approaches that don't work:
-
85% fail
Busy-waits and burns CPU; still no way to exit cleanly.
-
95% fail
Same deadlock, just a spin loop; runtime still aborts if no other goroutine.
-
80% fail
Moves the block elsewhere; leaked goroutine and no shutdown path.