go
runtime_error
ai_generated
true
恐慌:上下文已取消(在 goroutine 中)
panic: context canceled (in goroutine)
ID: go/context-cancel-not-propagated
80%修复率
85%置信度
0证据数
2024-11-01首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
| 1.22 | active | — | — | — |
根因分析
上下文被取消,但 goroutine 未检查上下文并尝试使用它,导致恐慌或错误。
English
A context is canceled, but a goroutine doesn't check the context and tries to use it, causing a panic or error.
解决方案
-
90% 成功率
Check ctx.Err() before and after operations, and return early if canceled.
-
95% 成功率
Pass ctx to all functions and use select with ctx.Done() to handle cancellation.
无效尝试
常见但无效的做法:
-
80% 失败
May lead to resource leaks or incorrect results; cancellation should be respected.
-
60% 失败
Loses cancellation ability; may cause indefinite blocking.