go
runtime_error
ai_generated
true
fatal error: concurrent map read and map write
ID: go/data-race-on-map
80%Fix Rate
90%Confidence
0Evidence
2024-01-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.6 | active | — | — | — |
| 1.21 | active | — | — | — |
Root Cause
Multiple goroutines access a map concurrently without synchronization, causing a runtime fatal error.
generic中文
多个goroutine在没有同步的情况下并发访问map,导致运行时致命错误。
Workarounds
-
95% success
Use sync.RWMutex to protect reads and writes to the map.
-
90% success
Use sync.Map which is safe for concurrent use.
Dead Ends
Common approaches that don't work:
-
90% fail
It can cause crashes or data corruption; ignoring is not safe.
-
40% fail
If not all accesses are protected, the race can still occur.
-
70% fail
Copying is expensive and does not solve the root race condition.