# error: context 已取消

- **ID:** `go/context-canceled-propagated`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

父 context 被取消(或超时),子操作返回 context.Canceled。常见原因是过早 defer cancel(),或把请求 context 复用到后台任务。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.21+ | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   bg := context.WithoutCancel(ctx)
go func() { longRunning(bg) }()
   ```
2. **** (92% 成功率)
   ```
   if errors.Is(err, context.Canceled) {
    return nil // expected shutdown
}
   ```

## 无效尝试

- **** — Hides real shutdown bugs and leaks resources; also masks timeout-induced failures. (60% 失败率)
- **** — Breaks request cancellation and leaks goroutines after client disconnect. (70% 失败率)
