go
runtime_error
ai_generated
true
panic:向已关闭的 channel 发送数据
panic: send on closed channel
ID: go/panic-send-on-closed-channel
80%修复率
90%置信度
0证据数
2024-03-11首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0+ | active | — | — | — |
根因分析
某个 goroutine 在另一个 goroutine 调用 close() 之后仍向该 channel 发送数据,违反了单一关闭者/关闭后不发送的规则。
English
A goroutine sends on a channel after another goroutine called close() on it, violating the single-closer / no-send-after-close rule.
解决方案
-
95% 成功率
done := make(chan struct{}) go func() { for { select { case <-done: return case ch <- v: } } }() -
88% 成功率
mu.Lock() if !closed { ch <- v } mu.Unlock()
无效尝试
常见但无效的做法:
-
85% 失败
Recover only protects the current goroutine; other senders still panic and the channel remains closed.
-
90% 失败
len() is racy; the channel can be closed between the check and the send.