go concurrency_error ai_generated true

WARNING: DATA RACE Read at 0x00c0000 by goroutine N: Previous write at 0x00c0000 by goroutine M:

ID: go/race-condition-detected

Also available as: JSON · Markdown
87%Fix Rate
92%Confidence
50Evidence
2023-06-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1 active
1 active

Root Cause

Go 1.22+ race detector found unsynchronized read/write access to shared memory from different goroutines. The improved race detector in 1.22 catches more races with lower overhead.

generic

Workarounds

  1. 93% success Protect shared state with sync.Mutex or sync.RWMutex
    var mu sync.RWMutex
    mu.RLock() // for reads
    mu.Lock()  // for writes

    Sources: https://pkg.go.dev/sync#RWMutex

  2. 90% success Use sync/atomic for simple counters and flags
    var counter atomic.Int64
    counter.Add(1)
    val := counter.Load()

    Sources: https://pkg.go.dev/sync/atomic

Dead Ends

Common approaches that don't work:

  1. Disable the race detector with build tags to suppress warnings 95% fail

    The race still exists in production — disabling detection does not fix the bug, it just hides it

  2. Use atomic operations on complex structs 70% fail

    atomic only works for primitive types (int32, int64, pointer); struct-level atomics are not supported

Error Chain

Leads to:
Preceded by:
Frequently confused with: