Skip to main content
System SecurityAdvanced

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?
Not quite. A data race usually means unsynchronised concurrent memory access with at least one write. A race condition is broader: correctness depends on timing, even when each individual access is synchronised.
Why are these bugs hard to find?
The harmful ordering may occur only occasionally and can change when logging or debugging alters timing. Code review of shared state plus repeated concurrent testing is more dependable than single-request checks.

Related terms

Learn and practise

Explore the topic: Web and API security

References

NEWPractise supported commands in browser simulations, alongside the lesson.Try a browser practice exercise.Try a labCodelivly is 100% free, no paywall. If it helped you, chip in to keep it running.100% free. Help keep it so.Support us