go runtime_error ai_generated true

WARNING: DATA RACE Read at 0x... by goroutine N: main.main.func1() Previous write at 0x... by goroutine M:

ID: go/data-race-on-shared-counter

Also available as: JSON · Markdown · 中文
80%Fix Rate
89%Confidence
0Evidence
2024-09-08First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0+ active

Root Cause

Multiple goroutines read and write a shared variable without synchronization. The race detector reports the conflicting accesses.

generic

中文

多个 goroutine 在无同步的情况下读写共享变量,race detector 报告了冲突访问。

Workarounds

  1. 95% success
    var counter int64
    atomic.AddInt64(&counter, 1)
  2. 93% success
    var mu sync.Mutex
    mu.Lock()
    counter++
    mu.Unlock()

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Hides the symptom but the underlying race still corrupts data and can crash in production.

  2. 85% fail

    unsafe does not provide synchronization; the race remains.