go timeout_error ai_generated partial

context deadline exceeded

ID: go/context-deadline-exceeded

Also available as: JSON · Markdown
82%Fix Rate
85%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1 active
121 active

Root Cause

Operation timed out via context.WithTimeout/WithDeadline. Server call or DB query took too long.

generic

Workarounds

  1. 90% success Increase timeout to a reasonable value based on expected operation time
    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)

    Sources: https://pkg.go.dev/context#WithTimeout

  2. 88% success Investigate why the operation is slow: check DB queries, network latency, etc.
    check DB queries, network latency, etc

    Sources: https://pkg.go.dev/context

  3. 80% success Add retry logic for transient timeouts
    for attempt := 0; attempt < 3; attempt++ {
      ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
      err := doWork(ctx)
      cancel()
      if err == nil { break }
      time.Sleep(time.Duration(attempt+1) * 100 * time.Millisecond)
    }

    Sources: https://pkg.go.dev/context

Dead Ends

Common approaches that don't work:

  1. Remove the context timeout 75% fail

    Operations may hang forever without timeouts

  2. Set a very large timeout 60% fail

    Just delays the inevitable — find why the operation is slow

Error Chain

Frequently confused with: