go
runtime_error
ai_generated
true
恐慌:向已关闭的通道发送数据
panic: send on closed channel
ID: go/channel-close-on-sender-panic
80%修复率
82%置信度
0证据数
2024-05-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
根因分析
在通道关闭后向其发送数据会导致运行时恐慌。
English
Sending to a channel after it has been closed causes a runtime panic.
解决方案
-
85% 成功率
ch := make(chan int) close(ch) // Use select with a done channel to avoid sending after close select { case ch <- 1: default: fmt.Println("channel closed") } -
90% 成功率
var mu sync.RWMutex closed := false // In sender: mu.RLock() if !closed { ch <- 1 } mu.RUnlock() // In closer: mu.Lock() close(ch) closed = true mu.Unlock()
无效尝试
常见但无效的做法:
-
60% 失败
Recovery doesn't prevent data loss or inconsistency; the sender state is corrupted.
-
80% 失败
len() doesn't indicate closed status; a closed channel can still have buffered items.
-
50% 失败
Mutex doesn't prevent closing while sending; race condition remains.