go
runtime_error
ai_generated
true
fatal error:并发 map 写入
fatal error: concurrent map writes
ID: go/data-race-map-concurrent-write
80%修复率
89%置信度
0证据数
2024-04-03首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.16 | active | — | — | — |
| 1.21 | active | — | — | — |
| 1.22 | active | — | — | — |
根因分析
两个 goroutine 在无同步的情况下写入同一个 map。运行时检测到并发写并中止进程。
English
Two goroutines write to the same map without synchronization. The runtime detects concurrent writes and aborts the process.
解决方案
-
92% 成功率
var m sync.Map m.Store(k, v) v, ok := m.Load(k)
-
96% 成功率
var mu sync.RWMutex mu.Lock() m[k] = v mu.Unlock()
无效尝试
常见但无效的做法:
-
90% 失败
Other writes still race; the runtime aborts on the first detected concurrent pair.
-
85% 失败
Sleeps don't eliminate races under real load; the crash just becomes intermittent.