Race Condition

Data Race

A data race occurs when two threads or processes simultaneously access the same memory location, at least one access is a write, and no synchronization orders them.

Why care?

The unordered write can make intermediate state visible to the other thread, producing nonsensical results. The bug reproduces only under specific schedules, which is why it’s hard to catch in testing.

Avoiding data races requires coordination (typically locks) to keep intermediate states from becoming visible. Rust’s borrow checker prevents data races at compile time by forbidding &mut to coexist with any other reference.

Data race vs race condition

Every data race is a race condition, but not every race condition is a data race. Filesystem or database races between atomic operations count as race conditions without being data races.