go
runtime_error
ai_generated
true
fatal error: 并发读取和写入 map
fatal error: concurrent map read and map write
ID: go/map-concurrent-read-write
80%修复率
90%置信度
0证据数
2024-05-02首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0+ | active | — | — | — |
根因分析
一个 goroutine 读取 map 的同时另一个 goroutine 写入该 map,且无同步,运行时检测到冲突并中止。
English
One goroutine reads a map while another writes it, without synchronization. The runtime detects the conflict and aborts.
解决方案
-
94% 成功率
mu.RLock() v := m[key] mu.RUnlock()
-
88% 成功率
type req struct{ key string; resp chan int } for r := range reqs { r.resp <- m[r.key] }
无效尝试
常见但无效的做法:
-
75% 失败
Copying itself iterates the map and races with concurrent writes, triggering the same fatal error.
-
85% 失败
Unlocked reads still race with locked writes; the detector fires on read/write overlap.