# context canceled: goroutine pool worker detected cancellation after returning result

- **ID:** `go/context-canceled-in-goroutine-pool`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

A goroutine in a pool checks context cancellation after computing a result, causing a false positive cancellation error when the parent context is cancelled just before the worker returns.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Go 1.15 | active | — | — |
| Go 1.16 | active | — | — |
| Go 1.17 | active | — | — |
| Go 1.18 | active | — | — |
| Go 1.19 | active | — | — |
| Go 1.20 | active | — | — |
| Go 1.21 | active | — | — |
| Go 1.22 | active | — | — |
| Go 1.23 | active | — | — |

## Workarounds

1. **Check context cancellation before computation, not after; use select with both context.Done() and result channel** (90% success)
   ```
   Check context cancellation before computation, not after; use select with both context.Done() and result channel
   ```
2. **Use a sync.WaitGroup or atomic flag to ensure result is fully sent before checking cancellation** (85% success)
   ```
   Use a sync.WaitGroup or atomic flag to ensure result is fully sent before checking cancellation
   ```
3. **Ignore cancellation error if result was successfully computed and sent; log but don't propagate** (80% success)
   ```
   Ignore cancellation error if result was successfully computed and sent; log but don't propagate
   ```

## Dead Ends

- **** — Without context checks, workers cannot be cancelled mid-task, leading to resource leaks and slow shutdowns. (85% fail)
- **** — If the parent context is cancelled, derived contexts are also cancelled; doesn't solve the race condition. (70% fail)
- **** — Mutexes don't prevent the race between context cancellation and result computation; can cause deadlocks. (60% fail)
