Races introduced by plausible concurrent code
Recognise races that look fine on the page: a Go map written from goroutines, unsynchronized lazy initialisation in Java, check-then-act on a cache, and async read-modify-write in JS. Name which tool or synchronization would catch or fix each one.
Builds on: Race conditions and interleavings
Parallel, green, and reads cleanly
You asked the agent to speed up a slow report. It wrapped the loop body in goroutines, added a `sync.WaitGroup`, and reported: "Now 4x faster, all tests green." The diff is short and reads cleanly.
The same shape appears elsewhere: a lazily created singleton in Java, a 'get or compute' cache, an async handler that loads a record, changes it and saves it. Each one looks correct line by line.
Before you merge, what do you look for, and which check would actually catch the problem?
Why these races look fine on the page
The race lives *between* two runs of the same code, not in any one line. Tests usually make one call at a time on tiny data, so the bad interleaving rarely happens; green tests prove little here.
Two kinds:
- **Data race**: two threads touch the same memory, at least one writes, and no synchronization orders them. Race detectors find these.
- **Logical race**: each step is safe, but a check and its act, or a read and its write, are separate steps. Race detectors miss these.
Later cost: lost updates, half-built objects, crashes under load. All rare, all hard to reproduce.
Four shapes and what catches each
- **Go map written from goroutines**: data race. `go test -race` catches it in a concurrent test. Fix: `sync.Mutex` or one owner goroutine fed by a channel.
- **Java lazy init** (`if (x == null) x = …`): data race. Java has no built-in detector, so review is the check. Fix: holder class or `volatile` field.
- **Check-then-act on a cache**: logical race. Fix: one atomic call (`computeIfAbsent`, `LoadOrStore`) or a lock around both steps.
- **JS read, `await`, write**: other callbacks run during `await`, so a value read before it can go stale. Even `x += await f()` reads `x` first. Logical race; no tool flags it. Fix: no `await` between read and write.
Java lazy init: the missing volatile
The agent wrote double-checked locking: check `inst`, lock, check again, create.
The tell: the first read of `inst` is outside the lock, and the field is not `volatile`. Another thread can see a non-null `inst` before the constructor's writes are visible to it, so its fields are still null or zero.
Later cost: rare NullPointerExceptions or wrong defaults in production that no test reproduces.
BETTER: make `inst` `volatile`; a thread that reads it then also sees the constructor's writes. Or use a holder class: the JVM initialises a class once, thread-safely, on first use.
// Java
// BAD: plain field
private static Svc inst;
static Svc get() {
if (inst == null) {
synchronized (L.class) {
if (inst == null) inst = new Svc();
}
}
return inst;
}
// BETTER: same get(), field made volatile
private static volatile Svc inst;
The Go map written from goroutines
`regionOf(u)` is the slow call; the agent moved the whole loop body into goroutines. The tell: `counts[regionOf(u)]++` now runs in many goroutines with no lock. Go maps are unsafe for concurrent writes, and `++` is a read-modify-write.
With 10 test users it often passes. Later cost: under load the runtime can stop with `fatal error: concurrent map writes`, killing the process. `go test -race` flags it even when the timing was harmless, if the test runs this loop with two or more users.
BETTER: call `regionOf(u)` outside any lock, then lock a `sync.Mutex` around just the increment.
// Go
--- a/report.go
+++ b/report.go
counts := map[string]int{}
+var wg sync.WaitGroup
for _, u := range users {
- counts[regionOf(u)]++
+ wg.Add(1)
+ go func(u User) {
+ defer wg.Done()
+ counts[regionOf(u)]++
+ }(u)
}
+wg.Wait()
Wrong between the lines, not on them
Concurrent code can read fine line by line and still be wrong between the lines. Look for two things:
- shared state that goroutines, threads or async callbacks touch without a lock, an atomic call or a single owner;
- a check or read separated from its act or write.
For data races, run the race detector where one exists (`go test -race`) on a test that runs the code concurrently. Race detectors stay silent on logical races, so your review is the check.
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.