go runtime_error ai_generated true

WARNING: DATA RACE Write at 0x00c000... by goroutine 7 Previous write at 0x00c000... by goroutine 8

ID: go/race-detector-write-write

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.21 active — — —
1.22 active — — —

Root Cause

Two goroutines access the same memory location without synchronization, with at least one write, detected by the race detector when running with -race.

generic

中文

两个 goroutine 在没有同步的情况下访问同一内存位置,且至少有一次写操作,在 -race 模式下被竞态检测器捕获。

Workarounds

  1. 95% success
    var mu sync.Mutex
    mu.Lock()
    counter++
    mu.Unlock()
    // or
    atomic.AddInt64(&counter, 1)
  2. 92% success
    results := make(chan int, len(items))
    for _, it := range items {
        it := it
        go func(v int) { results <- compute(v) }(it)
    }

Dead Ends

Common approaches that don't work:

  1. 95% fail

    The race is real; disabling detection does not fix corruption, which surfaces later as random crashes.

  2. 90% fail

    Sleep reduces the window but does not eliminate the race; the detector still reports it.