go runtime_error ai_generated true

WARNING: DATA RACE Read at 0x00c0000b4010 by goroutine 7:

ID: go/data-race-detector

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.19 active — — —
1.21 active — — —
1.22 active — — —

Root Cause

The race detector (go test -race / go run -race) found unsynchronized concurrent access to the same memory location. Usually a shared struct field or slice index accessed without a mutex or atomic.

generic

中文

竞争检测器(go test -race / go run -race)发现对同一内存位置的无同步并发访问。通常是共享结构体字段或切片索引在无 mutex/atomic 保护下访问。

Workarounds

  1. 95% success
    var hits atomic.Int64
    hits.Add(1)
    n := hits.Load()
  2. 97% success
    type Counter struct {
        mu sync.Mutex
        n  int
    }
    func (c *Counter) Inc() { c.mu.Lock(); c.n++; c.mu.Unlock() }
  3. 92% success
    results := make(chan Result)
    go func() { results <- compute() }()
    r := <-results // ownership transferred

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Hides the bug; production crashes or corrupts state under real concurrency.

  2. 85% fail

    Does not establish happens-before; race remains and detector still fires.