go
runtime_error
ai_generated
true
error: context canceled(取消后 goroutine 阻塞在 channel 发送上)
error: context canceled (goroutine blocked on channel send after cancellation)
ID: go/context-canceled-while-sending
80%修复率
87%置信度
0证据数
2024-10-01首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.7+ | active | — | — | — |
根因分析
goroutine 在 context 被取消后仍向 channel 发送数据,而接收方已停止消费,导致阻塞或工作丢失。
English
A goroutine keeps sending to a channel after its context is canceled, and the receiver has stopped draining, causing a block or lost work.
解决方案
-
94% 成功率
select { case ch <- v: case <-ctx.Done(): return ctx.Err() } -
90% 成功率
g, ctx := errgroup.WithContext(ctx) g.Go(func() error { return producer(ctx, ch) }) return g.Wait()
无效尝试
常见但无效的做法:
-
80% 失败
Continues to block on a full channel and wastes resources; the cancellation is not honored.
-
75% 失败
Closing while senders are active panics with send on closed channel.