go runtime_error ai_generated true

WARNING: DATA RACE: Write by goroutine X, Read by goroutine Y (atomic variable)

ID: go/goroutine-atomic-load-store-mismatch

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2025-08-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.4 active
1.21 active

Root Cause

Using atomic.Load/Store on a variable that is also accessed non-atomically, causing race.

generic

中文

对同时被非原子访问的变量使用atomic.Load/Store,导致竞争。

Workarounds

  1. 95% success Use atomic operations consistently for all accesses
    var val atomic.Value
    val.Store(42)
    v := val.Load().(int)
  2. 90% success Use sync.Mutex to protect all accesses
    var mu sync.Mutex
    var x int
    mu.Lock()
    x = 42
    mu.Unlock()
    mu.Lock()
    v := x
    mu.Unlock()

Dead Ends

Common approaches that don't work:

  1. Using volatile keyword (not available in Go) 100% fail

    Go doesn't have volatile; atomic operations are required.

  2. Adding memory barrier manually 70% fail

    Go's memory model is complex; manual barriers are error-prone.