A race condition is a software flaw where the result depends on timing, so two actions that arrive at almost the same moment can slip past a rule that works perfectly when they arrive one at a time.
This page sits under application security. If you are new to the field, start with Application Security for Beginners, then come back here. This guide explains the idea in plain English for students, career changers, testers and managers. It covers how the timing gap appears, a worked example, common mistakes and the first steps a small team can take.
By the end of this guide you will be able to:
- Explain what a race condition is and why timing matters.
- Describe the "check, then act" gap that most race conditions come from.
- Identify simple design habits that make race conditions much harder to exploit.
Key takeaways
- Timing is the weakness. A race condition appears when two pieces of work touch the same data at the same time and nothing makes them take turns.[1]
- Check, then act is the usual trap. If code checks something first and acts on it a moment later, the situation can change in between.[2]
- One-time rules are the favourite target. Single-use discount codes, gift cards, balances and "one vote per person" limits can be beaten when the same request is sent several times at once.[3]
- The fix is to make the check and the change one step. Databases and programming languages offer tools for this, such as transactions, locks and uniqueness rules.[1][3]
What it is, in plain English
Modern applications do many things at once. A web shop may handle hundreds of requests per second, each on its own thread (a separate line of work running inside the program). Usually this is fine. Problems start when two threads read and change the same piece of data, such as an account balance, without any rule that makes them wait for each other. That shared data is called a shared resource.
MITRE's Common Weakness Enumeration (CWE), the public catalogue of software weakness types, files this under CWE-362. It describes code that needs temporary, exclusive access to a shared resource, but leaves a timing window in which other code running at the same time can change it.[1] The best-known form is the time-of-check to time-of-use (TOCTOU) flaw, CWE-367: the program checks the state of something, then uses it, and the state changes between the check and the use.[2] The short period when this can happen is often called the race window.[3]
An everyday comparison: two people share a bank card and stand at two cash machines at the same time. Each machine checks the balance, sees 100 pounds, and pays out 100 pounds. Each check was correct when it was made, yet the account paid out twice. Where the analogy breaks down: real banks design their systems to stop exactly this, and in software the window is often a fraction of a second, so it only shows up under load or when someone deliberately sends requests together.
Why it matters: a race condition can turn a safe business rule into one that can be repeated, which can mean lost money, wrong data or, in the worst cases, someone gaining rights they should not have. MITRE lists consequences that include crashes, data being read or overwritten, and protection bypass where the timing guards security-critical code such as sign-in state.[1]
How it works, step by step
Here is the pattern at concept level, using a single-use promotional code as the example.[3]
- A rule exists. The shop says each code can be used once.
- The code checks first. When a request arrives, the application looks up whether the code has already been used.
- The code acts. If not used, it applies the discount.
- The code records. Only then does it mark the code as used.
- Two requests overlap. If two requests for the same code arrive together, both can pass step 2 before either reaches step 4. Both see "not used yet".
- The rule is broken. The discount is applied twice, even though every individual line of code behaved as written.
The gap between steps 2 and 4 is the race window. The same shape appears in gift card redemption, withdrawals beyond a balance, repeated product ratings and rate limits that count attempts too late.[3] In files and operating systems the shape is similar: a program checks a file, then opens it, and the file is swapped in the gap.[2]
The defence is to close the window. OWASP's Application Security Verification Standard (ASVS) 5.0 asks that a check on a resource and the action that depends on it are done as a single atomic operation, one that completes as a whole with no in-between state, and that shared objects in multi-threaded code use thread-safe types and locks (requirements V15.4.1 and V15.4.2).[4]
| Approach | What it does, in plain terms |
|---|---|
| Database transaction | Groups the check and the change so they succeed or fail together.[3] |
| Row lock | Makes other work on the same record wait until the first job finishes. In PostgreSQL, for example, a row locked with SELECT FOR UPDATE blocks other updates to it until the transaction ends.[5] |
| Uniqueness rule | The database itself refuses a second "code used" record, as a safety net behind the code.[3] |
| Mutex or lock in code | Lets only one thread run a sensitive section at a time.[1] |
| Idempotency key | A unique value sent with a request so a repeat returns the first result instead of doing the work twice.[6] |
A worked example
Harbourside Books is a fictional online bookshop with a small development team. Its marketing lead, Priya, sends loyal customers a gift card worth 20 pounds, which can be spent once.
A week later, Tom in finance notices that a handful of gift cards were spent two or three times within the same second. Nobody hacked the password database. The redemption code read the card balance, approved the order and only then set the balance to zero. Several orders sent together all read the full balance.
The lead developer, Sam, makes three changes. First, the balance check and the deduction now run inside one database transaction that locks the card record, so a second order waits and then sees a zero balance. Second, the database gets a rule that a card balance can never go below zero, as a backstop. Third, the checkout sends an idempotency key, so a customer who double-clicks "Pay" gets one order, not two. Sam also asks the tester to include a check that sends several redemption requests together in the test environment, an approved test on the team's own system.
The result is modest but real: the duplicate redemptions stop appearing in the finance report, and the team now reviews any new "single use" feature for the same pattern. Sam notes that other parts of the shop may still have similar gaps, so the team adds them to its review list.
Common mistakes
| Mistake | What to do instead |
|---|---|
| Testing features with one request at a time only | Include tests that send the same request several times at once, in an approved test environment. |
| Reading a value, deciding in code, then writing it back | Let the database do the check and the change together, for example in one transaction or a conditional update.[3] |
| Relying on the user session to enforce a database limit | Enforce limits in the same place the data lives; one storage layer should not be used to protect another.[3] |
| Assuming a simple counter update is safe | Use atomic operations; MITRE notes that even "x++" is a read, a calculation and a write.[1] |
| Holding locks for a long time | Keep locked sections short; PostgreSQL warns that a transaction waiting on a conflicting lock can wait indefinitely unless a deadlock is detected.[5] |
Key terms
- Race condition
- A timing flaw where a security decision depends on something that can change before the action completes.
- Time-of-check to time-of-use (TOCTOU)
- A race condition where a system checks a resource at one moment, then uses it later after it may have changed.
- Atomic operation
- A single, indivisible action that completes without exposing an in-between state.
- Mutex
- A lock that lets only one thread or process use a resource or run a piece of code at a time.
- Idempotency key
- A unique value sent with a request so that a repeated request returns the original result instead of creating a second action.
- Concurrency
- Several requests or tasks being handled by a system at the same time.
First steps
Three actions for this week:
- List the features in your application that enforce a "once only" or "up to a limit" rule, such as vouchers, credits, votes and balances.
- For each one, ask a developer whether the check and the change happen in one step, and write down the answer.
- Agree with your team to add a parallel request test for the riskiest feature in your test environment.
Checklist
Checklist:
- Limit-based features are listed with an owner.
- Sensitive check-and-change logic runs as one transaction or atomic update.
- The database has uniqueness or "never below zero" rules as a backstop.
- Payment and order endpoints accept an idempotency key.
- Code reviews ask "what if this runs twice at the same moment?"
- Tests include parallel requests, not only single requests.
Suggested learning pathway
- Application Security for Beginners: the umbrella topic this page sits under.
- Secure Coding for Beginners: everyday habits that avoid common flaws.
- Business Logic Flaws for Beginners: the wider family of flaws in how rules are enforced.
- The OWASP Top 10 for Beginners: the best-known list of web application risks.
- Race Condition Vulnerabilities Guide: the practitioner deep dive on locking, isolation levels and testing.
Standards and references
- CWE-362: Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition'). MITRE Common Weakness Enumeration, CWE version 5.0. https://cwe.mitre.org/data/definitions/362.html, accessed 27 Nov 2026.
- CWE-367: Time-of-check Time-of-use (TOCTOU) Race Condition. MITRE Common Weakness Enumeration, CWE version 5.0. https://cwe.mitre.org/data/definitions/367.html, accessed 27 Nov 2026.
- Race conditions. Web Security Academy, PortSwigger. https://portswigger.net/web-security/race-conditions, accessed 27 Nov 2026.
- Application Security Verification Standard 5.0.0, V15.4 Safe Concurrency. OWASP Foundation. https://owasp.org/www-project-application-security-verification-standard/.
- Explicit Locking. PostgreSQL Documentation (current version), The PostgreSQL Global Development Group. https://www.postgresql.org/docs/current/explicit-locking.html, accessed 27 Nov 2026.
- Idempotent requests. Stripe API Reference, Stripe. https://docs.stripe.com/api/idempotent_requests, accessed 27 Nov 2026.
Related NHI Mgmt Group resources: Race Condition Vulnerabilities Guide · Secure Coding for Beginners · Application Security for Beginners