Skip to content
BENIBIX Learn

Race Condition

Development⚙️ Compilers & RuntimesConcurrency

Also known as Data Race, Race Bug

A bug where the result depends on which task gets there first.

Think of it like

Think of it like two people writing on the same line of a board at once. The words get mixed up.

Example

Jack's shop has 1 item left. Two buyers click at the same moment, both checks see 1 in stock, and the item is sold twice.

Why it matters

Race bugs pass tests. Then they fail in real use, when many users act at the same moment.

Definition

A race condition is a bug where the result depends on which of two tasks gets there first. It happens when threads or requests read and change the same data at the same time. Adding 1 to a number is really three steps: read it, add, write it back. If two threads do this at once, one write can wipe out the other. The bug appears only now and then, which makes it hard to find.

Common mistake

Adding a short wait to make the bug go away. It only hides it. Use a lock or a database transaction instead.

Key takeaways

  1. 1
    Timing decides. Who gets there first.
  2. 2
    Shared data. Read, change, write.
  3. 3
    Rare, then costly. Hard to reproduce.

Compare