go
runtime_error
ai_generated
true
panic: 向已关闭的通道发送数据
panic: send on closed channel
ID: go/send-on-closed-channel
80%修复率
90%置信度
0证据数
2024-05-02首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.13 | active | — | — | — |
| 1.21 | active | — | — | — |
根因分析
生产者 goroutine 向已被其他 goroutine(或自身)关闭的通道发送数据。常见于协调者过早关闭通道的扇入模式。
English
A producer goroutine sends to a channel that has already been closed by another goroutine (or by itself). Classic with fan-in patterns where a coordinator closes the channel too early.
解决方案
-
93% 成功率
select { case ch <- v: case <-ctx.Done(): return } -
90% 成功率
done := make(chan struct{}) go func() { defer close(ch) for { select { case <-done: return case ch <- next(): } } }()
无效尝试
常见但无效的做法:
-
80% 失败
A closed channel is always ready to receive; select will still pick the closed case and send panics.
-
55% 失败
Correct but often forgotten on one path; still panics if any send path bypasses the mutex, and adds contention.