Go: context canceled (rpc error: code = Canceled desc = context canceled)
The context was cancelled before the operation finished — the caller gave up, the client disconnected, or a parent was cancelled.
context canceledrpc error: code = Canceled desc = context canceledfailed to connect: context canceledsql: context cancelednet/http: request canceled 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
- Treat
errors.Is(err, context.Canceled)as non-fatal - Use a detached context (
context.WithoutCancel, Go 1.21) for work that must outlive the request - Keep cancel until all work is done
Detailed fix by platform
Go
context.WithoutCancel(r.Context())for background tasks started from a handler
How to diagnose
- Cancel — Who cancelled?
- Scope — Request lifetime?
- Work — Must outlive caller?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- assignment mismatch: 1 variable but f returns 2 values Go: assignment mismatch: 1 variable but os.Open returns 2 values
- cannot assign to struct field m[k].f in map Go: cannot assign to struct field users["bob"].Age in map
- context deadline exceeded Go: Get "https://api.example.com": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
Most viewed in Go
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 4 Nov 2026