Race Conditions and Data Races

Topics Covered

Two Kinds of Race

Race conditions

Data races

A race condition without a data race

A data race without a race condition

Why the distinction changes the fix

The word race is used loosely for any bug that depends on timing, but there are two distinct ideas behind it, and mixing them up leads to wrong fixes. A race condition is a flaw in program logic: the correctness of the result depends on the relative timing of events. A data race is a precise, low level property of memory accesses: two threads access the same memory location concurrently, at least one access is a write, and nothing orders them. This section defines both carefully, shows that each can occur without the other, and explains why the distinction matters for choosing a fix.

Race conditions

A race condition exists when a program can produce a wrong result because of the order in which concurrent events happen. The lost update is a race condition, and so is check then act: the program checks whether a file exists and then creates it, and another process creates the file in between. Race conditions are defined relative to what the program is supposed to do. A program in which two threads append log lines in either order may be perfectly correct if the log does not promise any order, while the same interleaving is a bug in a program that promises that the line about opening a connection comes before the line about closing it.

Race conditions are not limited to memory. Two processes renaming files, two services updating the same database row, two browser tabs writing the same local storage key, and a web page whose button can be clicked twice before the first click is processed all have race conditions without any shared memory between threads. They are a property of the design, and they are fixed by changing the design so that the operations that must happen together are atomic, or so that the result is correct in any order.

Data races

A data race is narrower and more mechanical. It happens when two threads access the same memory location, at least one of them writes, the accesses are not both atomic operations, and there is no synchronization that orders one access before the other. Synchronization here means something the language defines as creating order: acquiring and releasing the same lock, an atomic store read by an atomic load, starting or joining a thread, or sending and receiving on a channel. Two plain writes to the same integer from two threads, with no lock around either, form a data race regardless of whether the program logic cares about the result.

The definition matters because languages give data races special meaning. In C and C++, a program with a data race has undefined behavior: the compiler may assume data races do not happen and optimize accordingly, so anything can happen, including results that no interleaving could produce. Java gives racy programs weaker but defined semantics. Go treats races as bugs and may crash on some of them. Rust rejects most data races at compile time. The next section examines these differences.

A race condition is a timing dependent logic bug. A data race is unsynchronized conflicting access. Many bugs are both, but each can exist alone.

A race condition without a data race

Consider a bank account class whose balance field is protected by a lock, and whose getBalance and withdraw methods each take that lock. Every access to the balance is synchronized, so there is no data race. Now a caller writes: if getBalance is at least 100, call withdraw of 100. Two threads run this caller code at once. Both read 150 under the lock, both release it, both decide to withdraw, and both withdraw under the lock, leaving the balance at minus 50. Every individual access was properly locked, yet the program is wrong, because the check and the act were two separate atomic steps rather than one.

The fix is not more locking of each access but a change in what is atomic: a withdrawIfAtLeast method that checks and subtracts while holding the lock once, or a caller that holds the lock across both calls. This is why tools that detect data races, such as ThreadSanitizer, cannot find all concurrency bugs. They verify that each access is ordered, not that the ordering the program relies on is the right one.

Each call takes the lock, so there is no data race. But check and withdraw are separate atomic steps, so both threads pass the check.

A data race without a race condition

The opposite case also exists. A server keeps an approximate count of requests in a plain integer, incremented without synchronization by many threads and read occasionally for a dashboard. The program logic tolerates a few lost increments, so there is no race condition in the sense of a wrong result that matters. It is still a data race. In Java the consequence is mostly that the count is approximate. In C or C++, it is undefined behavior, and the compiler is allowed to transform the code in ways that break more than the count: for example, keeping the counter in a register for an entire loop and writing it back once, or in rare cases producing torn values on platforms where the type is wider than a machine word. The correct way to write such a counter is a relaxed atomic increment, which costs little and has defined behavior.

Developers sometimes call these benign data races. Research on real programs, including a well known study by Hans Boehm titled How to miscompile programs with benign data races, shows that many supposedly benign races become harmful under ordinary compiler optimizations. The practical rule is that in languages with undefined behavior for data races, there is no such thing as a benign one; use atomics for intentionally unsynchronized data.

Why the distinction changes the fix

When a bug report says there is a race, the first question to ask is which kind. If it is a data race, the fix is to add the missing order: a lock, an atomic, a channel, or a thread start or join that makes one access happen before the other. A race detector can confirm the fix. If it is a race condition, adding order to individual accesses does not help; the fix is to change which operations are atomic, often by moving logic into a single method, a single database statement or a single message handler, or to change the design so that every order of events produces an acceptable result. Many real bugs are both, and fixing only the data race, for instance by making a counter field atomic while leaving a check then act around it, leaves the race condition in place with a false sense of security. Asking both questions every time avoids this trap.

A related term is the general race, sometimes called a determinacy race: any two accesses whose order is not fixed by the program, even if each is atomic. Two atomic increments on the same counter form a general race but not a data race, and the final count is still deterministic because addition commutes. Two atomic stores of different values are a general race whose outcome depends on timing. Research tools for deterministic parallelism, such as those for fork join programs, look for general races, because any of them can make a program produce different output on different runs, which matters for reproducible builds, simulations and tests.

Distinguish the logic bug from the memory bug

A race condition is a design flaw in which the result depends on timing, and it can exist even when every access is locked. A data race is two unordered accesses to the same location, at least one a write, and in C and C++ it is undefined behavior. Race detectors find data races; only reasoning about which operations must be atomic finds race conditions.