go
runtime_error
ai_generated
true
错误:context 已取消但 worker 仍在处理(循环中未检查 ctx)
error: context canceled while worker still processing (ctx not checked in loop)
ID: go/context-canceled-in-select-loop
80%修复率
88%置信度
0证据数
2024-09-03首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.21 | active | — | — | — |
根因分析
长时间运行的 goroutine 只 select 数据 channel,从不检查 ctx.Done(),因此取消无法停止它们;它们继续工作并可能阻塞关闭流程。
English
Long-running goroutines select only on data channels and never check ctx.Done(), so cancellation does not stop them; they keep working and may block shutdown.
解决方案
-
95% 成功率
Add ctx to every select: 'select { case <-ctx.Done(): return ctx.Err(); case v := <-in: ... }' -
90% 成功率
Pass ctx into blocking calls (http.NewRequestWithContext, db.QueryContext) so they abort on cancel
无效尝试
常见但无效的做法:
-
85% 失败
Skips deferred cleanup, corrupts state, drops in-flight data
-
75% 失败
Does not propagate intent; workers run to full timeout even when done