# fatal error: concurrent map iteration and map write

- **ID:** `go/concurrent-map-iteration-write`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.19 | active | — | — |
| 1.21 | active | — | — |
| 1.22 | active | — | — |

## Workarounds

1. **** (95% success)
   ```
   Hold a read lock for the whole iteration and a write lock for mutations:

s.mu.RLock()
for k, v := range s.m {
    _ = k; _ = v
}
s.mu.RUnlock()
   ```
2. **** (90% success)
   ```
   Snapshot keys under lock, then iterate the snapshot:

s.mu.RLock()
keys := make([]string, 0, len(s.m))
for k := range s.m { keys = append(keys, k) }
s.mu.RUnlock()
for _, k := range keys { _ = k }
   ```

## Dead Ends

- **** — Assigning a map copies only the header, not the contents; both variables share the same underlying map, so writes still race with iteration. (90% fail)
- **** — Sleeping widens or shifts the race window but does not synchronize; the fatal error still occurs under load. (88% fail)
