CWE-362OWASP A04:2021CWE-367CWE-840

CWE-362: Race Condition

Two or more operations access a shared resource at the same time without proper synchronization, producing results the code never intended.

Also known as: Race condition, Concurrency bug, Improper synchronization

Risk

Typically medium to high, higher when money or limits are involved. Concurrent requests can double-spend, oversell, or corrupt shared state, and the flaw appears only under timing that single-request testing never reproduces.

Definition

What it is

CWE-362 occurs when concurrent operations read and write a shared resource without synchronization, so the final result depends on the order in which they interleave. A read-modify-write done without a lock or an atomic operation can lose updates or apply a rule twice when two requests run at once. The code looks correct in isolation but breaks under concurrency. The weakness is the missing guard that would make the sequence atomic or serialize access.

Use-cases

How it shows up in real code

Double-spend on balance

Two withdrawal requests read the same balance, both pass the check, and both subtract, so a user spends more than they have because neither saw the other's update.

Coupon used twice

A single-use coupon is submitted twice at once. Both requests read it as unused before either marks it used, so the discount applies twice.

Overselling limited stock

The last item in stock is bought by two concurrent orders that both read quantity as one and both decrement, overselling the inventory.

Duplicate record creation

Two requests to create a unique resource run together, both find none existing, and both insert, producing duplicates the logic assumed impossible.

AI risk

Why AI-generated code has increased the exposure

  • Assistants write the straightforward read-then-write that is correct for one request but omit the lock or atomic operation needed under concurrency.
  • The model cannot see that an endpoint will be hit by many simultaneous requests, so it treats the operation as if it runs alone.
  • Models optimize for code that passes a sequential test, and the simplest version skips synchronization entirely.
  • Training data is full of non-atomic read-modify-write snippets presented as complete, and that pattern is reproduced by default.
  • As the assistant scaffolds more endpoints that update shared counters, balances, or limits, each is an unsynchronized race, and traffic scales the collisions.

Vibe coding

How vibe coding amplifies the risk

  • Vibe coding ships read-modify-write code whose missing synchronization is invisible in an unread diff.
  • Testing one request at a time, the developer never triggers the interleaving, so the race stays hidden.
  • Prompt-and-ship skips the review where someone would ask what happens if two requests hit this at once.
  • A prototype that worked under light manual use is promoted to production where real concurrency exposes it.
  • Rapid growth brings the concurrent traffic that turns a dormant race into double-spends faster than review catches it.

Detection

How to find it

  • Find read-modify-write sequences on shared state and confirm they are atomic or guarded by a lock.
  • For limits, balances, and single-use items, confirm the database enforces the rule with a constraint or atomic update.
  • Load-test the operation with concurrent requests and confirm totals and limits stay correct.
  • Review whether unique resources rely on a database uniqueness constraint rather than a check-then-insert in code.

Remediation

How to fix it

  • Make the critical section atomic with a database transaction, atomic update, or a lock around the shared resource.
  • Enforce invariants in the database with constraints and conditional updates rather than in application code alone.
  • Use optimistic concurrency with a version check so a stale update is rejected instead of silently applied.
  • Push counters and balances into atomic operations rather than reading, computing, and writing separately.
  • Add concurrency tests that fire simultaneous requests and fail the build if a limit or total is violated.

Frequently asked questions

What is CWE-362 (race condition)?
CWE-362 is when concurrent operations access a shared resource without proper synchronization, so the outcome depends on how they interleave. A read-modify-write done without a lock or atomic operation can lose updates or apply a rule twice, letting concurrent requests double-spend, oversell, or corrupt shared state.
How have AI assistants and vibe coding increased race condition risk?
Assistants write the straightforward read-then-write that is correct for a single request but omit the lock or atomic operation, because they cannot see that the endpoint will face many simultaneous requests. Vibe coding ships that code unread, and because testing runs one request at a time, the race stays hidden until real concurrency arrives.
How do I fix race conditions?
Make the critical section atomic with a transaction, atomic update, or lock, enforce invariants with database constraints, use optimistic concurrency with a version check, prefer atomic counter operations, and add concurrency tests that fire simultaneous requests.

Related weaknesses

Catch CWE-362 before you ship

Tethlyn Audit scans your codebase and its git history for CWE-362 and the rest of the business logic vulnerabilities class, then ranks each finding by real exploitability and data sensitivity.

See Tethlyn Audit