go runtime_error ai_generated true

panic: close of closed channel (in goroutine)

ID: go/goroutine-channel-close-race

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-12-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.21 active

Root Cause

Two or more goroutines attempt to close the same channel without synchronization, causing a panic.

generic

中文

两个或多个 goroutine 在没有同步的情况下尝试关闭同一个通道,导致恐慌。

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 80% fail

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

  2. 90% fail

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