go runtime_error ai_generated true

WARNING: DATA RACE Write at 0x00c0000b4010 by goroutine 7: main.main.func1() Previous read at 0x00c0000b4010 by main goroutine:

ID: go/race-detector-write-read

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.1+ active

Root Cause

A shared variable is read and written by different goroutines without synchronization. Go's memory model gives no ordering guarantees, so torn reads and stale values occur.

generic

中文

共享变量被不同 goroutine 无同步地读写。Go 内存模型不提供顺序保证,因此会出现撕裂读取和过期值。

Workarounds

  1. 97% success
    var mu sync.Mutex
    var counter int
    mu.Lock()
    counter++
    mu.Unlock()
  2. 96% success
    var counter atomic.Int64
    counter.Add(1)
    _ = counter.Load()

Dead Ends

Common approaches that don't work:

  1. 92% fail

    Hides the symptom; the underlying race can corrupt memory or produce wrong results silently.

  2. 98% fail

    Go has no volatile keyword; the compiler and CPU may still reorder accesses.