Work effectively with AI coding agents. Understand them as language models running in a tool loop with limited context, which is why they guess, game the visible signal and report confidently. Recognise the shortcut and correctness tells in their code across Python, TypeScript/JavaScript, Rust, Java and Go, and know the damage each one does later. Verify changes instead of trusting them, review diffs fast for quality and security, judge when work is truly finished, and choose, design and steer tasks so agents can finish them well.
Races introduced by plausible concurrent code
card 3 of 6
Four shapes and what catches each
builds on: Race conditions and interleavings
- **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.
Terms on this card
- goroutine
- A lightweight concurrent function in Go, started with the `go` keyword. Many can run at the same time on different threads.
- sync.Mutex
- A Go lock. Code between `Lock()` and `Unlock()` runs in one goroutine at a time, which orders access to shared data.
- go test -race
- Runs Go tests with the race detector. It reports unsynchronized conflicting memory accesses that occur during that run.
- data race
- Two threads access the same memory, at least one access is a write, and no synchronization orders the accesses. Results can be corrupted or unpredictable.
- logical race
- Each single access is safe (no data race), but the result still depends on how steps from different tasks interleave. Example: a check and an act done as separate calls.
Lesson goal: 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. ·
the whole lesson