go runtime_error ai_generated true

fatal error: concurrent map writes

ID: go/concurrent-map-write-fatal

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.20 active — — —
1.22 active — — —

Root Cause

Multiple goroutines write to the same Go map without synchronization. Go's runtime detects the concurrent write and aborts the program; this is not recoverable via recover().

generic

中文

多个 goroutine 在无同步的情况下写入同一个 Go map。Go 运行时检测到并发写入后直接终止程序,无法通过 recover() 恢复。

Workarounds

  1. 95% success
    var mu sync.RWMutex; mu.Lock(); m[k]=v; mu.Unlock() — guard every access (read and write) with the same mutex
  2. 90% success
    Use sync.Map for read-mostly or disjoint-key workloads: var m sync.Map; m.Store(k, v); m.Load(k)

Dead Ends

Common approaches that don't work:

  1. 95% fail

    fatal error is thrown by the runtime and bypasses the normal panic/recover mechanism entirely

  2. 80% fail

    Partial locking still leaves a write racing a read; the runtime still aborts