go
network_error
ai_generated
true
rpc 错误:code = Canceled desc = 上下文已取消
rpc error: code = Canceled desc = context canceled
ID: go/grpc-canceled-context-canceled
80%修复率
86%置信度
0证据数
2024-07-21首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.x | active | — | — | — |
根因分析
传给 RPC 的上下文在完成前被取消,可能由显式 cancel() 调用、父上下文取消,或客户端拦截器引起。服务器感知到取消并中止处理程序。
English
The context passed to the RPC was canceled before completion, either by an explicit cancel() call, a parent context canceling, or a client-side interceptor. The server sees the cancellation and aborts the handler.
解决方案
-
88% 成功率
Derive a fresh context for retries and never reuse a canceled parent: ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() resp, err := client.Do(ctx, req) if status.Code(err) == codes.Canceled { // parent was canceled; abort cleanly, don't retry with same ctx } -
82% 成功率
Trace where the cancel is coming from — check for deferred cancel() firing too early or a request-scoped context tied to an HTTP handler that already returned: // ensure cancel() is deferred AFTER the RPC completes, not before ctx, cancel := context.WithTimeout(r.Context(), time.Second) defer cancel() resp, err := client.Do(ctx, req)
无效尝试
常见但无效的做法:
-
95% 失败
The context is already done; every call with it returns Canceled immediately without even reaching the server.
-
90% 失败
A canceled RPC returns a nil/zero response; proceeding as if it succeeded produces corrupt state downstream.