go runtime_error ai_generated true

WARNING: DATA RACE Goroutine 5 在 0x00c0000b4010 处写入:

WARNING: DATA RACE Write at 0x00c0000b4010 by goroutine 5:

ID: go/race-detector-data-race

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

版本兼容性

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

根因分析

两个 goroutine 在无同步的情况下访问同一内存位置,且至少一个是写操作。仅在以 -race 构建时被检测到。

English

Two goroutines access the same memory location without synchronization, and at least one is a write. Detected only when the binary is built with -race.

generic

解决方案

  1. 95% 成功率
    var counter atomic.Int64
    counter.Add(1)
    n := counter.Load()
  2. 92% 成功率
    ch := make(chan int)
    go func() { ch <- compute() }()
    result := <-ch

无效尝试

常见但无效的做法:

  1. 95% 失败

    Hides the symptom; the race still corrupts data nondeterministically.

  2. 90% 失败

    Yielding does not create a happens-before relationship; race remains.