go runtime_error ai_generated true

WARNING: DATA RACE - Write at 0x00c000012345 by goroutine 6

ID: go/data-race-on-variable

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-06-25First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.22 active

Root Cause

Concurrent reads and writes to a shared variable without synchronization.

generic

中文

并发读写共享变量而没有同步。

Workarounds

  1. 95% success
    var mu sync.Mutex
    var x int
    // Write
    mu.Lock()
    x = 1
    mu.Unlock()
    // Read
    mu.Lock()
    _ = x
    mu.Unlock()
  2. 90% success
    var x atomic.Int32
    x.Store(1)
    _ = x.Load()

Dead Ends

Common approaches that don't work:

  1. 50% fail

    Reads still race unless also atomic.

  2. 100% fail

    Go doesn't have volatile; no effect.

  3. 90% fail

    Sleep doesn't establish happens-before relationship.