# rpc 错误：code = Canceled，desc = 上下文已取消

- **ID:** `go/grpc-context-canceled`
- **领域:** go
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| go 1.21+ (context.WithoutCancel) | active | — | — |

## 解决方案

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
}
   ```

## 无效尝试

- **** — An already-canceled context immediately fails every subsequent call. (99% 失败率)
- **** — The cancellation originates on the client side; server timeouts are irrelevant. (80% 失败率)
