Race Condition
What is Race Condition?
A race condition exists when a result depends on the timing or order of concurrent operations on shared data, and an unexpected order breaks correctness or security.
It can occur between threads, processes, database transactions or separate servers. Security effects include spending a balance twice, exceeding a limit, using a one-time code repeatedly or changing a file between a permission check and its use. The last pattern is known as time of check to time of use.
How the weakness arises
Two operations read or change the same resource without adequate coordination. For instance, both read that one voucher remains, both decide redemption is allowed and both record it. Each step is valid in isolation, but the check and update were separable. The window may be tiny and appear only under load, which makes the behaviour intermittent and difficult to reproduce.
Example
A developer sends several simultaneous redemption requests for a single test voucher to a local app and sees two succeed. Combining the check and update into one atomic database operation allows only one.
Limitations and common misconceptions
A transaction does not automatically prevent every race; isolation level and query design matter. A lock inside one server cannot coordinate several instances. Disabling a button in the interface does not control direct or parallel requests. Tests that pass repeatedly may simply have missed the timing window.
Fixing and preventing it
Make checks and changes atomic using conditional updates, unique constraints, suitable isolation or locks held across the critical section. Use idempotency keys for repeatable operations, operate on already opened file handles instead of rechecking paths, define shared-state ownership, run concurrency detectors and test with parallel load.
Frequently Asked Questions
Is a race condition the same as a data race?
Why are these bugs hard to find?
Related terms
Learn and practise
Explore the topic: Web and API security