# Go: context canceled (rpc error: code = Canceled desc = context canceled)

**Code:** `context canceled` · **Category:** Go · **Seen on:** Go

The context was cancelled before the operation finished — the caller gave up, the client disconnected, or a parent was cancelled.

## Meaning

In HTTP servers `r.Context()` is cancelled when the client disconnects, so database calls inside the handler return `context canceled`. This is often normal; handle it quietly. Other causes: calling `cancel()` too early (before goroutines finish) or reusing a cancelled context.

## Common causes

- Client disconnected from an HTTP request
- cancel() called too early
- Parent context cancelled on shutdown
- Using request context for background work
- errgroup cancelling on first error

## Quick fix

1. Treat `errors.Is(err, context.Canceled)` as non-fatal
2. Use a detached context (`context.WithoutCancel`, Go 1.21) for work that must outlive the request
3. Keep cancel until all work is done

## Detailed fix by platform

### Go

1. `context.WithoutCancel(r.Context())` for background tasks started from a handler

## How to diagnose

1. **Cancel** — Who cancelled?
2. **Scope** — Request lifetime?
3. **Work** — Must outlive caller?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/go/context-canceled)
