{
  "id": "go/concurrent-map-iteration-write",
  "signature": "fatal error: concurrent map iteration and map write",
  "signature_zh": "fatal error: 并发 map 迭代与写入",
  "regex": "fatal\\ error:\\ concurrent\\ map\\ iteration\\ and\\ map\\ write",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A range loop over a map runs concurrently with a write to the same map in another goroutine. The runtime detects the iterator/writer conflict and aborts.",
  "root_cause_type": "generic",
  "root_cause_zh": "对 map 的 range 迭代与另一个 goroutine 对同一 map 的写入并发执行。运行时检测到迭代器与写入冲突后终止程序。",
  "versions": [
    {
      "version": "1.19",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.21",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.22",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Assigning a map copies only the header, not the contents; both variables share the same underlying map, so writes still race with iteration.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Sleeping widens or shifts the race window but does not synchronize; the fatal error still occurs under load.",
      "fail_rate": 0.88,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Hold a read lock for the whole iteration and a write lock for mutations:\n\ns.mu.RLock()\nfor k, v := range s.m {\n    _ = k; _ = v\n}\ns.mu.RUnlock()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Snapshot keys under lock, then iterate the snapshot:\n\ns.mu.RLock()\nkeys := make([]string, 0, len(s.m))\nfor k := range s.m { keys = append(keys, k) }\ns.mu.RUnlock()\nfor _, k := range keys { _ = k }",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.9,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-06-19",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}