go
runtime_error
ai_generated
true
error: context 已取消(goroutine worker 返回)
error: context canceled (goroutine worker returned)
ID: go/context-canceled-during-goroutine
80%修复率
87%置信度
0证据数
2024-11-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
| 1.21 | active | — | — | — |
| 1.22 | active | — | — | — |
| 1.23 | active | — | — | — |
根因分析
父 context 被取消(超时、手动 cancel 或 HTTP 客户端断开),worker goroutine 观察到 ctx.Err() == context.Canceled。
English
A parent context was canceled (deadline, manual cancel, or HTTP client disconnect) and the worker goroutine observes ctx.Err() == context.Canceled.
解决方案
-
92% 成功率
Treat context.Canceled as expected and return without error at shutdown: if errors.Is(err, context.Canceled) { return nil } -
90% 成功率
Propagate the context through all calls and check ctx.Err() at loop boundaries.
无效尝试
常见但无效的做法:
-
90% 失败
The context remains canceled; retries fail immediately and waste resources.
-
85% 失败
Leaks the goroutine past shutdown and defeats the purpose of cancellation.