go runtime_error ai_generated true

rpc error: code = Canceled desc = context canceled

ID: go/grpc-canceled-context

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
google.golang.org/grpc v1.40+ active

Root Cause

The context passed to the RPC was canceled before completion. Usually the caller's context was canceled (parent request ended, client disconnected, or cancel() called early due to a defer).

generic

中文

传给 RPC 的上下文在完成前被取消。通常是调用方上下文被取消(父请求结束、客户端断开,或 defer 过早调用 cancel())。

Workarounds

  1. 85% success
    ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
    defer cancel()
    resp, err := client.Do(ctx, req)
  2. 80% success
    ctx, cancel := context.WithTimeout(parent, 5*time.Second)
    defer cancel() // must be after the call that uses ctx
    resp, err := client.Do(ctx, req)

Dead Ends

Common approaches that don't work:

  1. 95% fail

    A canceled context stays canceled; every retry immediately returns Canceled without contacting the server.

  2. 70% fail

    Loses deadline propagation and cancellation, causing leaked goroutines and requests that outlive their callers.