go
runtime_error
ai_generated
true
fatal error: concurrent map read and map write
ID: go/concurrent-map-read-and-write
80%Fix Rate
90%Confidence
0Evidence
2024-03-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.21 | active | — | — | — |
Root Cause
One goroutine read a map while another wrote to it. The runtime's map implementation detects this inconsistent access and aborts the program.
generic中文
一个 goroutine 读 map 的同时另一个 goroutine 写同一个 map。运行时检测到这种不一致访问会终止程序。
Workarounds
-
93% success
var mu sync.RWMutex // writer mu.Lock(); m[k]=v; mu.Unlock() // reader mu.RLock(); v := m[k]; mu.RUnlock()
-
85% success
type req struct{ key string; resp chan int } // single owner goroutine processes reqs serially
Dead Ends
Common approaches that don't work:
-
85% fail
Copying a map being concurrently written also races and may panic or produce corrupted data.
-
87% fail
Timing-based synchronization is unreliable and breaks under load or different GOMAXPROCS.