go runtime_error ai_generated true

恐慌:向已关闭的通道发送数据

panic: send on closed channel

ID: go/channel-send-on-closed

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-03-15首次发现

版本兼容性

版本状态引入弃用备注
1.0 active
1.21 active

根因分析

在另一个goroutine关闭通道后,没有正确同步就向该通道发送数据。

English

Sending data to a channel that has been closed by another goroutine without proper synchronization.

generic

解决方案

  1. 85% 成功率
    Ensure the sender knows when to stop. Use a done channel to signal closure, and only close the channel from the sender side.
  2. 90% 成功率
    Use sync.Once to close the channel only once, ensuring no other sender attempts to send after close.

无效尝试

常见但无效的做法:

  1. 90% 失败

    The panic is fatal and cannot be recovered; it will crash the program.

  2. 95% 失败

    len() does not indicate whether a channel is closed, only the number of buffered elements.

  3. 70% 失败

    The close operation may still race with the send; mutex does not prevent closing while sending.