go runtime_error ai_generated true

警告:数据竞争:协程X写入,协程Y读取(原子变量)

WARNING: DATA RACE: Write by goroutine X, Read by goroutine Y (atomic variable)

ID: go/goroutine-atomic-load-store-mismatch

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2025-08-05首次发现

版本兼容性

版本状态引入弃用备注
1.4 active
1.21 active

根因分析

对同时被非原子访问的变量使用atomic.Load/Store,导致竞争。

English

Using atomic.Load/Store on a variable that is also accessed non-atomically, causing race.

generic

解决方案

  1. 95% 成功率 Use atomic operations consistently for all accesses
    var val atomic.Value
    val.Store(42)
    v := val.Load().(int)
  2. 90% 成功率 Use sync.Mutex to protect all accesses
    var mu sync.Mutex
    var x int
    mu.Lock()
    x = 42
    mu.Unlock()
    mu.Lock()
    v := x
    mu.Unlock()

无效尝试

常见但无效的做法:

  1. Using volatile keyword (not available in Go) 100% 失败

    Go doesn't have volatile; atomic operations are required.

  2. Adding memory barrier manually 70% 失败

    Go's memory model is complex; manual barriers are error-prone.