go runtime_error ai_generated true

WARNING: DATA RACE: Write by goroutine X, Read by goroutine Y (channel close)

ID: go/goroutine-racy-channel-close

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2026-01-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0 active
1.22 active

Root Cause

Multiple goroutines trying to close the same channel without synchronization, causing race.

generic

中文

多个协程试图关闭同一个通道而没有同步,导致竞争。

Workarounds

  1. 95% success Use sync.Once to ensure close is called only once
    var once sync.Once
    once.Do(func() { close(ch) })
  2. 85% success Use a dedicated closer goroutine
    closeCh := make(chan struct{})
    go func() {
        <-closeCh
        close(ch)
    }()
    // signal close
    close(closeCh)

Dead Ends

Common approaches that don't work:

  1. Using atomic flag to guard close 80% fail

    Close itself is not atomic; still race between check and close.

  2. Using recover() to catch double close panic 90% fail

    Recover doesn't prevent race; panic may still occur.