go runtime_error ai_generated true

上下文截止时间已超过

context deadline exceeded

ID: go/context-deadline-exceeded-in-goroutine

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  1. 90% 成功率
    for {
        select {
        case <-ctx.Done():
            return ctx.Err()
        default:
            doChunk()
        }
    }
  2. 92% 成功率
    ctx, cancel := context.WithCancel(parent)
    defer cancel()

无效尝试

常见但无效的做法:

  1. 80% 失败

    Hides the underlying slowness; the deadline will eventually be hit again.

  2. 85% 失败

    Downstream operations see a cancelled context and fail with different errors.