go runtime_error ai_generated true

fatal error: concurrent map writes

ID: go/map-concurrent-write

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0+ active

Root Cause

Two or more goroutines write to the same map without synchronization. The runtime detects overlapping writes and aborts the program.

generic

中文

多个 goroutine 在无同步的情况下写入同一个 map,运行时检测到重叠写入并中止程序。

Workarounds

  1. 94% success
    var mu sync.RWMutex
    mu.Lock()
    m[key] = value
    mu.Unlock()
  2. 90% success
    var m sync.Map
    m.Store(key, value)
    v, ok := m.Load(key)

Dead Ends

Common approaches that don't work:

  1. 80% fail

    The runtime detector may still fire and the race is not eliminated, only made less frequent.

  2. 70% fail

    Atomic values do not protect the map's internal buckets; the map itself is still raced.