# error: context canceled

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

## Root Cause

A context was canceled (via cancel() or parent cancellation) while a goroutine or HTTP/database call was still using it, causing the operation to fail with context.Canceled.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.19 | active | — | — |
| 1.21 | active | — | — |

## Workarounds

1. **** (93% success)
   ```
   if err := doWork(ctx); err != nil {
    if errors.Is(err, context.Canceled) {
        return nil // graceful shutdown
    }
    return err
}
   ```
2. **** (88% success)
   ```
   for i := 0; i < 3; i++ {
    ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
    err := call(ctx)
    cancel()
    if err == nil { return nil }
}
   ```

## Dead Ends

- **** — If the context is canceled, the retry uses the same canceled context and fails instantly, creating a tight loop. (90% fail)
- **** — Loses timeout and cancellation propagation, causing goroutine and connection leaks. (85% fail)
