go network_error ai_generated partial

rpc 错误:code = Canceled,desc = 上下文已取消

rpc error: code = Canceled desc = context canceled

ID: go/grpc-context-canceled

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-02-20首次发现

版本兼容性

版本状态引入弃用备注
go 1.21+ (context.WithoutCancel) active

根因分析

在 RPC 完成前调用方的 context 被取消(例如 HTTP 请求中止、父 context 超时或显式 cancel())。

English

The caller's context was canceled (e.g., HTTP request aborted, parent context deadline hit, or explicit cancel()) before the RPC completed.

generic

解决方案

  1. 90% 成功率
    Detach from the request context for background work:
    ctx = context.WithoutCancel(ctx)
    // or use context.Background() with an explicit timeout
  2. 85% 成功率
    Distinguish cancellation from real failures:
    if status.Code(err) == codes.Canceled {
        return // caller went away, do not log as error
    }

无效尝试

常见但无效的做法:

  1. 99% 失败

    An already-canceled context immediately fails every subsequent call.

  2. 80% 失败

    The cancellation originates on the client side; server timeouts are irrelevant.