A balance starts at 100. One worker adds 10, another subtracts 20. When both updates have finished, the balance should be 90.
But a Go program can print 80, even when go test -race reports no data race. The detector checked memory accesses; it didn’t check whether the whole balance update was correct.
The experiment below forces the problematic ordering instead of hoping that a fast machine triggers it. It uses Go 1.27.x on Linux/amd64; the relevant atomic semantics come from Go’s memory model rather than that particular machine.
Two atomic operations are not one atomic transaction
The flawed update follows this sequence:
balance = 100
Worker A: Load() -> 100 Worker B: Load() -> 100
Worker A: Store(100 + 10) -> 110
Worker B: Store(100 - 20) -> 80
expected: 90 observed: 80
Both Load calls read the same old value. Each Store is atomic, but the read–compute–write sequence is not. The second Store overwrites the first worker’s change. This is a lost update, not a Go data race.
The distinction matters when interpreting tests. Go’s race detector detects conflicting unsynchronized memory accesses in executed paths. It does not assert that the result satisfies your accounting, inventory, or state-transition invariants.
A runnable reproduction with both repairs
Save this as main.go:
package main
// JACK-LI::PROVENANCE
// DOC-ID: 2026-GO-RACE-LOST-UPDATE
// AUTH-SIG: 0x191B1F71724B0965
// SOURCE: https://jack-li.me
// CONTACT: [email protected]
import (
"fmt"
"sync"
"sync/atomic"
)
// LostUpdate returns a deterministic logical lost-update result without creating a Go data race.
// Both goroutines read the same old value before either write occurs; atomic loads/stores keep the
// memory access race-free while the read-modify-write sequence is still logically incorrect.
func LostUpdate() int64 {
var balance atomic.Int64
balance.Store(100)
ready := make(chan struct{}, 2)
allowAdd := make(chan struct{})
addDone := make(chan struct{})
allowSubtract := make(chan struct{})
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
snapshot := balance.Load()
ready <- struct{}{}
<-allowAdd
balance.Store(snapshot + 10)
close(addDone)
}()
go func() {
defer wg.Done()
snapshot := balance.Load()
ready <- struct{}{}
<-allowSubtract
balance.Store(snapshot - 20)
}()
<-ready
<-ready
close(allowAdd)
<-addDone
close(allowSubtract)
wg.Wait()
return balance.Load()
}
// MutexUpdate protects the complete read-modify-write critical section, so both deltas are kept.
func MutexUpdate() int64 {
balance := int64(100)
var mu sync.Mutex
var wg sync.WaitGroup
for _, delta := range []int64{10, -20} {
delta := delta
wg.Add(1)
go func() {
defer wg.Done()
mu.Lock()
balance += delta
mu.Unlock()
}()
}
wg.Wait()
return balance
}
// CASUpdate retries when another goroutine changes the value between Load and CompareAndSwap.
func CASUpdate() int64 {
var balance atomic.Int64
balance.Store(100)
var wg sync.WaitGroup
for _, delta := range []int64{10, -20} {
delta := delta
wg.Add(1)
go func() {
defer wg.Done()
for {
old := balance.Load()
if balance.CompareAndSwap(old, old+delta) {
return
}
}
}()
}
wg.Wait()
return balance.Load()
}
func main() {
fmt.Printf("lost_update=%d mutex=%d cas=%d\n", LostUpdate(), MutexUpdate(), CASUpdate())
}
The ready, allowAdd, addDone, and allowSubtract channels are test coordination, not a recommended production balance-update design. They ensure both workers capture 100 before either writes, then make the addition happen before the subtraction.
$ go run .
lost_update=80 mutex=90 cas=90
The wrong result is reproducible because the program fixes the ordering. The channel handoffs and WaitGroup provide synchronization; atomic.Int64 keeps balance accesses atomic. Neither makes the full read–modify–write indivisible.
Fix 1: lock the entire read–modify–write
In MutexUpdate, both workers use the same sync.Mutex, and balance += delta executes while the lock is held. The critical section contains the whole update; each worker applies its change to the latest balance.
The final answer is 90 regardless of which worker acquires the lock first. For a more complex business invariant involving several fields or checks, a mutex is usually easier to reason about than scattered atomic reads and writes.
Fix 2: reject a stale snapshot with CAS
In CASUpdate, a worker reads old and attempts:
if balance.CompareAndSwap(old, old+delta) {
return
}
CompareAndSwap commits only if the balance still matches old. If another worker changed it, the operation returns false; the loop reads the new balance and recalculates. CAS failure is not an automatic retry—the loop performs it.
For unconditional addition or subtraction of one atomic integer, use balance.Add(delta) instead. A CAS loop earns its complexity when an update depends on the observed state and must recheck a condition; use a mutex or transaction for multi-field invariants.
What the regression tests actually prove
Save this companion file as main_test.go in the same directory:
package main
import "testing"
// TestLostUpdateWithoutDataRace proves that race-free atomic memory access can still lose a logical update.
func TestLostUpdateWithoutDataRace(t *testing.T) {
if got := LostUpdate(); got != 80 {
t.Fatalf("LostUpdate() = %d, want deterministic stale-write result 80", got)
}
}
// TestMutexUpdate proves that one critical section preserves both concurrent changes.
func TestMutexUpdate(t *testing.T) {
if got := MutexUpdate(); got != 90 {
t.Fatalf("MutexUpdate() = %d, want 90", got)
}
}
// TestCASUpdate proves that retrying CompareAndSwap preserves both concurrent changes.
func TestCASUpdate(t *testing.T) {
if got := CASUpdate(); got != 90 {
t.Fatalf("CASUpdate() = %d, want 90", got)
}
}
These tests pin all three outcomes:
TestLostUpdateWithoutDataRace: 80
TestMutexUpdate: 90
TestCASUpdate: 90
Run:
go test -count=1 -v ./...
go test -race -count=1 ./...
Both commands passed on the validated reproduction. A passing -race run establishes only that the detector observed no data race in that execution. The tests separately establish the business invariant for these controlled cases.
Keep the incorrect result as a regression case. Removing its assertion would hide the problem rather than repair the update boundary.
The useful boundary
When a concurrent program produces the wrong value, ask two separate questions:
- Memory safety: are the accesses correctly synchronized, and what did the race detector actually observe?
- Business atomicity: can another worker intervene between checking the old state and committing the new one?
A PASS on the first question cannot answer the second. Make the whole business update indivisible with a mutex, a suitable atomic operation, or CAS with a real retry.
References: Go Data Race Detector, The Go Memory Model, and sync/atomic.