go runtime_error ai_generated true

rpc错误:代码=已取消 描述=上下文已取消

rpc error: code = Canceled desc = context canceled

ID: go/grpc-canceled-context-cancelled

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2025-03-05首次发现

版本兼容性

版本状态引入弃用备注
1.0 active
1.1 active

根因分析

客户端在RPC完成之前取消了上下文,通常是由于用户操作或超时。

English

The client cancelled the context before the RPC completed, often due to user action or timeout.

generic

解决方案

  1. 90% 成功率 Create a new context for each RPC call to avoid using cancelled contexts.
    ctx := context.Background()
    resp, err := client.SomeRPC(ctx, req)
    if err != nil {
        if status.Code(err) == codes.Canceled {
            // Create new context and retry if appropriate
            ctx2 := context.Background()
            resp, err = client.SomeRPC(ctx2, req)
        }
    }
  2. 85% 成功率 Handle cancellation gracefully by checking context errors.
    select {
    case <-ctx.Done():
        return nil, ctx.Err()
    default:
    }
    resp, err := client.SomeRPC(ctx, req)

无效尝试

常见但无效的做法:

  1. Ignore the cancellation and retry with the same context. 100% 失败

    The cancelled context cannot be reused; a new context is needed.

  2. Remove context cancellation logic entirely. 90% 失败

    This can lead to resource leaks and unresponsive applications.