go
runtime_error
ai_generated
true
fatal error: 并发读和写 map
fatal error: concurrent map read and map write
ID: go/concurrent-map-read-write
80%修复率
90%置信度
0证据数
2024-02-27首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.22 | active | — | — | — |
根因分析
一个 goroutine 读取 map 的同时另一个写入,未加锁;运行时检测到读/写竞争。
English
One goroutine reads a map while another writes to it without a lock; runtime detects read/write race.
解决方案
-
95% 成功率
var mu sync.RWMutex mu.RLock() v := m[k] mu.RUnlock() // write: mu.Lock() m[k] = v mu.Unlock()
-
88% 成功率
var m sync.Map v, _ := m.Load(k) m.Store(k, v)
无效尝试
常见但无效的做法:
-
90% 失败
Readers still race with writers; fatal error persists.
-
85% 失败
Copy itself races with the writer; still fatal.