go runtime_error ai_generated true

fatal error: 并发写 map

fatal error: concurrent map writes

ID: go/concurrent-map-write

其他格式: JSON · Markdown 中文 · English
80%修复率
90%置信度
0证据数
2024-01-19首次发现

版本兼容性

版本状态引入弃用备注
1.18 active — — —
1.21 active — — —
1.22 active — — —

根因分析

两个或更多 goroutine 未同步地写入同一 map;运行时检测到写/写竞争后中止。

English

Two or more goroutines write to the same map without synchronization; runtime detects write/write race and aborts.

generic

解决方案

  1. 93% 成功率
    var mu sync.Mutex
    mu.Lock()
    m[k] = v
    mu.Unlock()
  2. 85% 成功率
    var m sync.Map
    m.Store(k, v)
    v, ok := m.Load(k)

无效尝试

常见但无效的做法:

  1. 99% 失败

    fatal error is not a panic; recover cannot catch it.

  2. 90% 失败

    Worsens the race by running more goroutines in parallel.