go
runtime_error
ai_generated
true
panic: 在已关闭的通道上进行 select
panic: select on closed channel
ID: go/select-random-choice
80%修复率
84%置信度
0证据数
2024-03-30首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
根因分析
select 语句中包含向已关闭通道发送的 case,当该 case 被选中时引发 panic。
English
A select statement includes a case that sends to a closed channel, causing a panic when selected.
解决方案
-
90% 成功率
Before sending, check if channel is closed using a separate mechanism: select { case <-done: return case ch <- v: } // Ensure done is closed before ch is closed. -
85% 成功率
Use a separate struct with a mutex to manage channel state, or use a single producer that closes the channel.
无效尝试
常见但无效的做法:
-
50% 失败
If the channel is closed dynamically, it can't be removed at compile time.
-
80% 失败
Default case doesn't prevent panic when sending on a closed channel that is selected.