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.
Race conditions and interleavings card 5 of 6

Every access locked, still a race

Each map access holds the lock, so `go test -race` stays quiet: there is no data race. But the check (`ok`) and the act (the store) are in separate critical sections. Two goroutines can both miss, both `dial`, and the second store overwrites the first. **Later cost:** on every collision, one caller keeps a connection the cache no longer tracks, so nothing ever closes it. BETTER: one critical section covers both the check and the store (simple, but other callers wait during the dial), or a per-key `sync.Once` makes sure only one caller dials.
// Go
func (c *Cache) Get(k string) *Conn {
	c.mu.Lock()
	v, ok := c.m[k]
	c.mu.Unlock()
	if !ok {
		v = dial(k)
		c.mu.Lock()
		c.m[k] = v
		c.mu.Unlock()
	}
	return v
}
Terms on this card
Data race
Two threads access the same memory, at least one of them writes, and no synchronization orders the two accesses.
go test -race
Runs Go tests with the race detector. It reports data races that actually happen during that run, and it cannot see logical races.
critical section
A piece of code that runs while holding a lock, so only one thread executes it at a time.
dial
In Go networking, opening a connection to a server (as in `net.Dial`). Here it is a helper that opens a new connection.
sync.Once
A Go type whose Do method runs a function exactly once, even when many goroutines call it at the same time. The other callers wait until it finishes.
Learn this step by step on Bytewise
Lesson goal: Explain how concurrent code can interleave and produce wrong results, using check-then-act and read-modify-write as examples. Distinguish a data race from a logical race, and explain why only synchronization (locks, atomics, channels) creates ordering while timing does not. · the whole lesson