Skip to content
cloudgrip

$ man race-condition

//Attacks and vulnerabilities

What is a race condition?

A race condition is a flaw where the result depends on timing. An application checks a condition and then acts on it in separate steps, so requests that arrive at almost the same moment can all pass the check before any of them changes the state.

  • Attacks and vulnerabilities
  • 3 min read
  • updated Nov 11, 2026

How it works

A race condition appears when code does two things in sequence and assumes nothing else happens in between. First it checks: is the balance high enough, is the coupon unused, is there a seat left. Then it acts: pay out, apply the discount, book the seat.

Between the check and the action there is a short gap, often a few milliseconds. If two requests enter that gap together, both see the old state, both pass the check, and both act. The name for this pattern is time-of-check to time-of-use (TOCTOU).

Each request is valid on its own. The flaw only shows when requests overlap, which is why ordinary testing, one click at a time, never finds it.

What it looks like

A wallet feature lets users withdraw their balance.

output
# Vulnerable: the check and the update are separate steps
account = db.get_account(user_id)
if account.balance >= amount:
    send_payout(user_id, amount)
    db.set_balance(user_id, account.balance - amount)

A user with a balance of 100 sends two withdrawal requests for 100 at the same instant.

Time Request A Request B
1 Reads balance: 100
2 Reads balance: 100
3 Check passes, pays out 100
4 Check passes, pays out 100
5 Sets balance to 0 Sets balance to 0

The user receives 200 and the balance shows 0.

The same pattern turns up wherever a limit is enforced by reading and then writing:

  • Redeeming a single-use code or reset token several times.
  • Changing an object between a security check and its use, such as a file that is scanned and then swapped.

How to test for it

Look for any feature that enforces "only once" or "only up to N", then try to break the limit with simultaneous requests.

  1. Pick an action with a limit and prepare one valid request for it.
  2. Send many copies at the same time, from one session.
  3. Count the successes. If the limit is one and three requests succeed, the race is confirmed.

Timing is the hard part, because network delay spreads requests apart. A technique published by PortSwigger Research in 2023, known as the single-packet attack, uses HTTP/2 to deliver many requests in one network packet so they reach the server at virtually the same moment.

Use test accounts and test data, ideally outside production, because a successful race creates real state such as a real payout.

How to fix it

Make the check and the action one indivisible step, and let the database enforce it.

output
UPDATE accounts
SET balance = balance - 100
WHERE id = 'acct_42' AND balance >= 100;

This statement checks and updates together. The application pays out only if exactly one row changed. Two simultaneous requests cannot both succeed, because the database applies them one after the other.

Other tools for the same job:

  • Row locks. Read the row with a lock inside a transaction, so a second transaction waits until the first finishes.
  • Unique constraints. If a code can be redeemed once per user, a unique index on the code and user makes the second insert fail.
  • Idempotency keys. The client sends a unique key with each operation, and the server refuses to process the same key twice.

Fixes that do not work on their own include checks in application memory when several servers run in parallel, and rate limiting, which slows an attacker but does not close the gap.

How it relates to business logic flaws

A race condition is often filed under business logic vulnerabilities, because the damage is a broken business rule and not a crashed server. The distinction is the cause. A typical logic flaw can be triggered with one request sent at any time, such as a negative quantity in a cart. A race condition needs overlap, and disappears when requests arrive one by one.

That makes it easy to miss in secure code review as well. The useful review question is what happens if the same lines run twice at once.

See what an attacker would find.

Give us a URL and two test logins. If there’s something to find, your first exploited findings land within 24 hours.

You talk to the people who build Hydra and Cerberus. No sales script.

or write to founders@cloudgrip.ai