go.race_condition
Correctness
High
Common in Incidents
Terminal window
Detects potential race conditions from concurrent access to shared state without synchronization.
Why It Matters
Section titled “Why It Matters”Race conditions cause some of the hardest bugs to find:
- Intermittent failures - May only happen under specific timing
- Data corruption - Concurrent writes produce garbage
- Security vulnerabilities - TOCTOU attacks exploit race windows
- Impossible to reproduce - Works in testing, fails in production
Go’s race detector catches some at runtime, but static analysis catches them earlier.
Example
Section titled “Example”// ❌ Beforevar counter int
func incrementUnsafe() { go func() { counter++ // Race: concurrent read-modify-write }()}Two goroutines incrementing simultaneously might both read the same value and write the same result.
// ✅ After (mutex)var ( counter int mu sync.Mutex)
func incrementSafe() { go func() { mu.Lock() counter++ mu.Unlock() }()}
// ✅ After (atomic)var counter int64
func incrementAtomic() { go func() { atomic.AddInt64(&counter, 1) }()}What Unfault Detects
Section titled “What Unfault Detects”- Shared variables accessed in goroutines without mutex
- Map access from multiple goroutines
- Struct field access without synchronization
- Closure capturing variables modified concurrently
Auto-Fix
Section titled “Auto-Fix”Unfault can add mutex protection around shared variable access when the pattern allows safe transformation.
Common Patterns
Section titled “Common Patterns”// Atomic for simple countersvar count int64atomic.AddInt64(&count, 1)val := atomic.LoadInt64(&count)
// Mutex for complex statetype SafeCache struct { mu sync.RWMutex data map[string]string}
func (c *SafeCache) Get(key string) string { c.mu.RLock() defer c.mu.RUnlock() return c.data[key]}
func (c *SafeCache) Set(key, value string) { c.mu.Lock() defer c.mu.Unlock() c.data[key] = value}
// sync.Map for simple concurrent mapsvar cache sync.Mapcache.Store("key", "value")val, ok := cache.Load("key")Testing for Races
Section titled “Testing for Races”# Run tests with race detectorgo test -race ./...
# Build with race detectorgo build -race