Skip to content

Data Race

Medium — good to knowBackend

ELI5 - The Vibe Check

A data race is what happens when two threads touch the exact same piece of memory at the exact same time, and at least one of them is writing, with nothing coordinating who goes first. The result is undefined, sometimes it looks fine, sometimes you get half-written garbage, and the truly evil part is it might work perfectly a thousand times in a row and then corrupt data the one time it matters.

Real Talk

A data race occurs when two or more threads access the same memory location concurrently, at least one access is a write, and there is no synchronization mechanism establishing an ordering between them. Unlike a race condition, which is a broader logic bug about timing-dependent outcomes, a data race specifically means the program's behavior is undefined by the language's memory model, and tools like ThreadSanitizer exist specifically to catch it.

Show Me The Code

// Two goroutines writing the same variable with no synchronization
var counter int
go func() { counter++ }()
go func() { counter++ }() // data race: run with `go run -race`

When You'll Hear This

"ThreadSanitizer flagged a data race on that counter, it's not protected by anything." / "It's not a data race exactly, it's a race condition, the logic is wrong regardless of locking."

Made with passive-aggressive love by manoga.digital. Powered by Claude.