go
runtime_error
ai_generated
true
致命错误:所有协程都处于睡眠状态 - 死锁!(for-select没有default)
fatal error: all goroutines are asleep - deadlock! (for-select with no default)
ID: go/goroutine-for-select-no-default
80%修复率
84%置信度
0证据数
2025-04-18首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.20 | active | — | — | — |
根因分析
一个没有default情况的for-select循环,所有通道都阻塞,导致协程永远睡眠。
English
A for-select loop with no default case and all channels blocked causes goroutine to sleep forever.
解决方案
-
95% 成功率 Add default case to avoid blocking
for { select { case <-ch: // handle default: // non-blocking } } -
90% 成功率 Use a done channel to break out of loop
done := make(chan struct{}) for { select { case <-ch: // handle case <-done: return } }
无效尝试
常见但无效的做法:
-
Adding time.Sleep inside loop
90% 失败
Sleep doesn't unblock channels; goroutine still blocked.
-
Using break statement to exit loop
80% 失败
Break only exits select, not for loop; goroutine re-enters select and blocks again.