go runtime_error ai_generated true

WARNING: DATA RACE Write at 0x00c0000b4010 by goroutine 5:

ID: go/race-detector-data-race

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active
1.21 active
1.23 active

Root Cause

Two goroutines access the same memory location without synchronization, and at least one is a write. Detected only when the binary is built with -race.

generic

中文

两个 goroutine 在无同步的情况下访问同一内存位置,且至少一个是写操作。仅在以 -race 构建时被检测到。

Workarounds

  1. 95% success
    var counter atomic.Int64
    counter.Add(1)
    n := counter.Load()
  2. 92% success
    ch := make(chan int)
    go func() { ch <- compute() }()
    result := <-ch

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Hides the symptom; the race still corrupts data nondeterministically.

  2. 90% fail

    Yielding does not create a happens-before relationship; race remains.