# rpc error: code = Canceled desc = context canceled

- **ID:** `go/grpc-canceled-context-canceled`
- **Domain:** go
- **Category:** network_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.x | active | — | — |

## Workarounds

1. **** (88% success)
   ```
   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
}
   ```
2. **** (82% success)
   ```
   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)
   ```

## Dead Ends

- **** — The context is already done; every call with it returns Canceled immediately without even reaching the server. (95% fail)
- **** — A canceled RPC returns a nil/zero response; proceeding as if it succeeded produces corrupt state downstream. (90% fail)
