Glossary termSoftware bug class

Race Condition

Also known as: Race hazard

Flaw in which a system's behavior depends on the uncontrolled timing or ordering of events, such as two threads updating the same data at once.

A race condition occurs when the correctness of a program depends on the relative timing of operations that are not properly coordinated. The classic example involves two threads incrementing a shared counter. Each reads the current value, adds one and writes it back; if both read the value 5 before either writes, both write 6, and one increment is lost. Whether the bug appears depends on scheduling, so the same program may work thousands of times and then fail unpredictably. Race conditions can occur in software, in hardware circuits and across networked systems.

Defenses focus on controlling access to shared state. Locks and mutexes ensure that only one thread at a time runs a critical section; atomic operations let processors update a value in a single indivisible step; and designs that avoid shared mutable data, such as message passing or immutable values, sidestep the problem altogether. Tools help find races: thread sanitizers detect conflicting memory accesses at run time, and the ownership rules of Rust reject data races at compile time. Overcorrecting with too many locks can introduce deadlock or slow a program down.

Race conditions have caused serious real-world harm. Software timing flaws in the Therac-25 radiation therapy machine contributed to massive overdoses delivered to patients in the 1980s, a case widely taught in software engineering ethics. In security, time-of-check to time-of-use (TOCTOU) flaws arise when a program checks a condition, such as a file's permissions, and acts on it later, leaving a window in which the condition can change; they are a recognized class of vulnerability. Careful design for concurrency is therefore a safety and security matter, not only a question of performance.

Key points

  • A race condition makes a program's outcome depend on unpredictable timing between concurrent operations.
  • Locks, atomic operations and avoiding shared mutable state are the main defenses.
  • Because they depend on timing, race conditions are often intermittent and hard to reproduce.
  • Time-of-check to time-of-use flaws are race conditions that can become security vulnerabilities.

Related entries

Glossary term

Concurrency

Programming

Ability of a program or system to make progress on several tasks during overlapping time periods, whether or not they literally run simultaneously.

Glossary term

Deadlock

Programming

Situation in which two or more processes are each waiting for another to release a resource, so that none of them can ever proceed.

Glossary term

Mutability

Programming

The property that determines whether an object or value can be changed after it is created; immutable values stay fixed once made.

Glossary term

Parallel Computing

ProgrammingHardware

Form of computation in which many calculations run simultaneously on multiple processors or cores to solve problems faster or at larger scale.

Glossary term

Vulnerability

Cybersecurity

A weakness in software, hardware, configuration or process that an attacker could exploit to compromise confidentiality, integrity or availability.

Technology

Rust

ProgrammingFOSS

Systems programming language that guarantees memory safety without a garbage collector through its ownership model, first stable in 2015.

Glossary term

Debugging

Programming

Process of finding, diagnosing and fixing defects in software or hardware that cause it to behave incorrectly or unexpectedly.

Glossary term

Static Analysis

ProgrammingCybersecurity

Examination of source code or binaries without executing them, using automated tools to find bugs, security flaws and quality issues.

Referenced by

Other entries that discuss or point to Race Condition.