go runtime_error ai_generated true

WARNING: DATA RACE 由 goroutine N 在 0x... 读取: main.main.func1() 由 goroutine M 在 0x... 先前写入:

WARNING: DATA RACE Read at 0x... by goroutine N: main.main.func1() Previous write at 0x... by goroutine M:

ID: go/data-race-on-shared-counter

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

版本兼容性

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

根因分析

多个 goroutine 在无同步的情况下读写共享变量,race detector 报告了冲突访问。

English

Multiple goroutines read and write a shared variable without synchronization. The race detector reports the conflicting accesses.

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 90% 失败

    Hides the symptom but the underlying race still corrupts data and can crash in production.

  2. 85% 失败

    unsafe does not provide synchronization; the race remains.