go runtime_error ai_generated true

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

ID: go/race-condition-detected-race

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0+ active — — —

Root Cause

Two goroutines accessed the same memory location concurrently, at least one being a write, without synchronization. Detected when running with the -race flag.

generic

中文

两个 goroutine 并发访问同一内存位置,且至少有一个是写操作,未使用同步。使用 -race 标志运行时被检测到。

Workarounds

  1. 95% success
    var mu sync.Mutex
    mu.Lock()
    counter++
    mu.Unlock()
  2. 90% success
    var counter int64
    atomic.AddInt64(&counter, 1)
    n := atomic.LoadInt64(&counter)

Dead Ends

Common approaches that don't work:

  1. 95% fail

    The race still exists in production and can cause corrupted data or crashes; the flag only hides detection.

  2. 80% fail

    Mixing atomic and non-atomic access to the same variable is itself a race and still flagged by the detector.