go runtime_error ai_generated true

context deadline exceeded

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

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.7+ active

Root Cause

A goroutine using a context with a deadline did not complete in time; the derived context returns DeadlineExceeded.

generic

中文

使用带截止时间的上下文的 goroutine 未在规定时间内完成,派生上下文返回 DeadlineExceeded。

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 80% fail

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

  2. 85% fail

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