go runtime_error ai_generated true

恐慌:关闭已关闭的通道(在 goroutine 中)

panic: close of closed channel (in goroutine)

ID: go/goroutine-channel-close-race

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  1. 95% 成功率
    Use sync.Once to ensure channel is closed only once.
  2. 90% 成功率
    Designate a single goroutine to own the channel and close it.

无效尝试

常见但无效的做法:

  1. 80% 失败

    Recovering doesn't solve the race; it masks the error but may leave other goroutines in inconsistent state.

  2. 90% 失败

    Flag check is racy; another goroutine may close it between check and close.