go runtime_error ai_generated true

警告:数据竞争

WARNING: DATA RACE

ID: go/race-detector-warning-data-race

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-07-14首次发现

版本兼容性

版本状态引入弃用备注
1.0+ active

根因分析

两个 goroutine 并发访问同一内存位置,至少一个是写操作,且没有同步。由 -race 构建检测。

English

Two goroutines access the same memory location concurrently, at least one is a write, without synchronization. Detected by -race build.

generic

解决方案

  1. 95% 成功率
    var mu sync.Mutex
    mu.Lock()
    shared++
    mu.Unlock()
  2. 92% 成功率
    var n int64
    atomic.AddInt64(&n, 1)

无效尝试

常见但无效的做法:

  1. 90% 失败

    Hides the bug; corrupted state and crashes still occur in production.

  2. 80% 失败

    Go has no volatile; atomic fences alone don't fix logical races.