go
runtime_error
ai_generated
true
goroutine 阻塞在 select 上,没有就绪的 case(死锁或长时间停滞)
goroutine blocked in select with no ready case (deadlock or long stall)
ID: go/select-no-case-ready-block
80%修复率
85%置信度
0证据数
2024-12-09首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0+ | active | — | — | — |
根因分析
没有 default 的 select 语句会阻塞直到某个 case 可以执行。如果所有 channel 永远不会就绪,goroutine 将无限期停滞。
English
A select statement with no default blocks until one of its cases can proceed. If none of the channels ever become ready, the goroutine stalls indefinitely.
解决方案
-
90% 成功率
select { case v := <-ch: use(v) case <-time.After(5 * time.Second): return errors.New("timeout waiting for value") } -
90% 成功率
select { case v := <-ch: use(v) case <-ctx.Done(): return ctx.Err() }
无效尝试
常见但无效的做法:
-
90% 失败
A default with a loop spins the CPU at 100% and does not make any channel ready.
-
80% 失败
If the channels are never written to, more cases do not help; the goroutine still blocks.