go
runtime_error
ai_generated
true
恐慌:关闭已关闭的通道(在 goroutine 中)
panic: close of closed channel (in goroutine)
ID: go/goroutine-channel-close-race
80%修复率
88%置信度
0证据数
2024-12-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.21 | active | — | — | — |
根因分析
两个或多个 goroutine 在没有同步的情况下尝试关闭同一个通道,导致恐慌。
English
Two or more goroutines attempt to close the same channel without synchronization, causing a panic.
解决方案
-
95% 成功率
Use sync.Once to ensure channel is closed only once.
-
90% 成功率
Designate a single goroutine to own the channel and close it.
无效尝试
常见但无效的做法:
-
80% 失败
Recovering doesn't solve the race; it masks the error but may leave other goroutines in inconsistent state.
-
90% 失败
Flag check is racy; another goroutine may close it between check and close.