go runtime_error ai_generated true

fatal error: concurrent map writes

ID: go/concurrent-map-write

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0+ active

Root Cause

Two goroutines wrote to the same map without synchronization. The runtime detects concurrent map writes and aborts the process; it is not recoverable.

generic

中文

两个 goroutine 在无同步的情况下写同一个 map。运行时检测到并发写 map 会直接终止进程,无法 recover。

Workarounds

  1. 97% success
    var mu sync.RWMutex
    mu.Lock()
    m[k] = v
    mu.Unlock()
    
    mu.RLock()
    v := m[k]
    mu.RUnlock()
  2. 90% success
    var m sync.Map
    m.Store(k, v)
    if v, ok := m.Load(k); ok { _ = v }

Dead Ends

Common approaches that don't work:

  1. 99% fail

    fatal error is not a panic; recover cannot intercept it. The process dies regardless.

  2. 60% fail

    If reads also race with writes, the runtime may still report concurrent map read and map write. Both paths need protection.