go
runtime_error
ai_generated
true
上下文截止时间已超过
context deadline exceeded
ID: go/context-deadline-exceeded-in-goroutine
80%修复率
88%置信度
0证据数
2024-10-01首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.7+ | active | — | — | — |
根因分析
使用带截止时间的上下文的 goroutine 未在规定时间内完成,派生上下文返回 DeadlineExceeded。
English
A goroutine using a context with a deadline did not complete in time; the derived context returns DeadlineExceeded.
解决方案
-
90% 成功率
for { select { case <-ctx.Done(): return ctx.Err() default: doChunk() } } -
92% 成功率
ctx, cancel := context.WithCancel(parent) defer cancel()
无效尝试
常见但无效的做法:
-
80% 失败
Hides the underlying slowness; the deadline will eventually be hit again.
-
85% 失败
Downstream operations see a cancelled context and fail with different errors.