go runtime_error ai_generated true

fatal error: concurrent map read and map write

ID: go/map-concurrent-read-write

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-05-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0+ active

Root Cause

One goroutine reads a map while another writes it, without synchronization. The runtime detects the conflict and aborts.

generic

中文

一个 goroutine 读取 map 的同时另一个 goroutine 写入该 map,且无同步,运行时检测到冲突并中止。

Workarounds

  1. 94% success
    mu.RLock()
    v := m[key]
    mu.RUnlock()
  2. 88% success
    type req struct{ key string; resp chan int }
    for r := range reqs {
        r.resp <- m[r.key]
    }

Dead Ends

Common approaches that don't work:

  1. 75% fail

    Copying itself iterates the map and races with concurrent writes, triggering the same fatal error.

  2. 85% fail

    Unlocked reads still race with locked writes; the detector fires on read/write overlap.