go runtime_error ai_generated true

panic: close of closed channel

ID: go/panic-close-of-closed-channel

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-04-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0+ active

Root Cause

close(ch) invoked more than once, often from multiple cleanup paths or deferred closes.

generic

中文

close(ch) 被调用多次,通常来自多条清理路径或多个 defer 关闭。

Workarounds

  1. 95% success
    var once sync.Once
    once.Do(func() { close(ch) })
  2. 93% success
    go func() {
        defer close(ch)
        for v := range src { ch <- v }
    }()

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Silently swallows a logic bug; downstream consumers may still observe inconsistent state.

  2. 95% fail

    Multiple goroutines closing the same channel guarantees the panic.