go
runtime_error
ai_generated
true
error: context canceled while worker still processing (ctx not checked in loop)
ID: go/context-canceled-in-select-loop
80%Fix Rate
88%Confidence
0Evidence
2024-09-03First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.21 | active | — | — | — |
Root Cause
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.
generic中文
长时间运行的 goroutine 只 select 数据 channel,从不检查 ctx.Done(),因此取消无法停止它们;它们继续工作并可能阻塞关闭流程。
Workarounds
-
95% success
Add ctx to every select: 'select { case <-ctx.Done(): return ctx.Err(); case v := <-in: ... }' -
90% success
Pass ctx into blocking calls (http.NewRequestWithContext, db.QueryContext) so they abort on cancel
Dead Ends
Common approaches that don't work:
-
85% fail
Skips deferred cleanup, corrupts state, drops in-flight data
-
75% fail
Does not propagate intent; workers run to full timeout even when done