# error: context canceled（取消后 goroutine 阻塞在 channel 发送上）

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

## 根因

goroutine 在 context 被取消后仍向 channel 发送数据，而接收方已停止消费，导致阻塞或工作丢失。

## 版本兼容性

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

## 解决方案

1. **** (94% 成功率)
   ```
   select {
case ch <- v:
case <-ctx.Done():
    return ctx.Err()
}
   ```
2. **** (90% 成功率)
   ```
   g, ctx := errgroup.WithContext(ctx)
g.Go(func() error { return producer(ctx, ch) })
return g.Wait()
   ```

## 无效尝试

- **** — Continues to block on a full channel and wastes resources; the cancellation is not honored. (80% 失败率)
- **** — Closing while senders are active panics with send on closed channel. (75% 失败率)
