context canceled Go Origin: Application & API Last updated 4 Nov 2026

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.

Exact message you may see context canceledrpc error: code = Canceled desc = context canceledfailed to connect: context canceledsql: context cancelednet/http: request canceled
Seen on: Go

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?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

assignment mismatch: 1 variable but f returns 2 values — Go: assignment mismatch: 1 variable but os.Open returns 2 values (Go)assignment mismatch: …cannot assign to struct field m[k].f in map — Go: cannot assign to struct field users["bob"].Age in map (Go)cannot assign to stru…context deadline exceeded — Go: Get "https://api.example.com": context deadline exceeded (Client.Timeout exceeded while awaiting headers) (Go)context deadline exce…listen tcp :8080: bind: address already in use — Go: listen tcp :8080: bind: address already in use (Go)listen tcp :8080: bin…panic: assignment to entry in nil map — Go: panic: assignment to entry in nil map (Go)panic: assignment to …panic: send on closed channel — Go: panic: send on closed channel (Go)panic: send on closed…sql: database is closed / driver: bad connection — Go: sql: database is closed (Go)sql: database is clos…context c…

Explore the map of this error Full error map →

🧠 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.