go network_error ai_generated true

error: context deadline exceeded (from select ctx.Done())

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

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.7+ active

Root Cause

A select statement observes ctx.Done() because the parent context's deadline elapsed. The downstream operation is cancelled, but the caller treats it as a generic error.

generic

中文

select 语句观察到 ctx.Done(),因为父上下文的截止时间已到。下游操作被取消,但调用方将其视为通用错误。

Workarounds

  1. 96% success
    select {
    case <-ctx.Done():
        return ctx.Err()
    case res := <-ch:
        return res
    }
  2. 90% success
    for i := 0; i < 3; i++ {
        ctx, cancel := context.WithTimeout(parent, 2*time.Second)
        err := call(ctx)
        cancel()
        if err == nil { return nil }
    }

Dead Ends

Common approaches that don't work:

  1. 95% fail

    The deadline is fixed; every retry burns the same expired context and returns instantly.

  2. 90% fail

    Callers receive silently wrong results and downstream logic corrupts data.