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
80%Fix Rate
86%Confidence
0Evidence
2026-01-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.22 | active | — | — | — |
Root Cause
Multiple goroutines trying to close the same channel without synchronization, causing race.
generic中文
多个协程试图关闭同一个通道而没有同步,导致竞争。
Workarounds
-
95% success Use sync.Once to ensure close is called only once
var once sync.Once once.Do(func() { close(ch) }) -
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:
-
Using atomic flag to guard close
80% fail
Close itself is not atomic; still race between check and close.
-
Using recover() to catch double close panic
90% fail
Recover doesn't prevent race; panic may still occur.