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

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-09-03First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 95% success
    Add ctx to every select: 'select { case <-ctx.Done(): return ctx.Err(); case v := <-in: ... }'
  2. 90% success
    Pass ctx into blocking calls (http.NewRequestWithContext, db.QueryContext) so they abort on cancel

Dead Ends

Common approaches that don't work:

  1. 85% fail

    Skips deferred cleanup, corrupts state, drops in-flight data

  2. 75% fail

    Does not propagate intent; workers run to full timeout even when done