go
runtime_error
ai_generated
true
fatal error: concurrent map iteration and map write
ID: go/concurrent-map-iterations
80%Fix Rate
90%Confidence
0Evidence
2024-06-09First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.0+ | active | — | — | — |
Root Cause
A goroutine iterates a map with for range while another goroutine writes to the same map, which the runtime detects and aborts.
generic中文
某 goroutine 使用 for range 迭代 map,同时另一 goroutine 向同一 map 写入,运行时检测到后直接终止。
Workarounds
-
97% success
mu.Lock() for k, v := range m { process(k, v) } mu.Unlock() -
93% success
type req struct{ reply chan map[string]int } for r := range reqs { r.reply <- snapshot(m) }
Dead Ends
Common approaches that don't work:
-
88% fail
Copying without locking still races with the writer during the copy itself.
-
86% fail
Taking the snapshot races with concurrent writes and can still trigger the detector.