go
runtime_error
ai_generated
true
WARNING: DATA RACE Write at 0x00c0000b4010 by goroutine 7:
ID: go/data-race-detector-warning
80%Fix Rate
88%Confidence
0Evidence
2024-07-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.21 | active | — | — | — |
| 1.22 | active | — | — | — |
Root Cause
Two goroutines access the same memory location concurrently, at least one is a write, and no synchronization orders them. Detected only when built with -race; the program otherwise has undefined behavior.
generic中文
两个 goroutine 并发访问同一内存位置,至少一个是写操作,且没有同步机制排序。仅在 -race 构建时被检测到;否则程序行为未定义。
Workarounds
-
95% success
Protect the shared variable with a mutex: var mu sync.Mutex mu.Lock() counter++ mu.Unlock()
-
92% success
Use atomic operations for simple counters: var counter int64 atomic.AddInt64(&counter, 1) _ = atomic.LoadInt64(&counter)
Dead Ends
Common approaches that don't work:
-
95% fail
Hiding the report does not fix the race; the bug persists and manifests as corrupted data or crashes in production.
-
90% fail
Sleep reduces the probability but does not establish a happens-before relationship; the race and its undefined behavior remain.