[Bug]: Concurrent carts oversell an inventory item held on more than one line; reserved_quantity also loses updates across processes
Package.json file
{
"dependencies": {
"@medusajs/framework": "2.19.0",
"@medusajs/medusa": "2.19.0"
}
}(Resolved @medusajs/inventory and @medusajs/core-flows 2.19.0. The code is unchanged on develop at 62a520014.)
Node.js version
v22.22.2
Database and its version
PostgreSQL 17.11
Operating system name and version
Ubuntu 24.04.2 LTS
Browser name
n/a (server-side)
What happended?
Two carts that complete at the same moment can oversell an inventory item when either cart holds it on more than one line: units plus a multi-pack, or a bundle plus a single. Stock goes negative and both orders are placed.
This is the concurrent form of #16502, which was closed as stale today without a fix. The root cause is the same: ensureInventoryLevels (packages/modules/inventory/src/services/inventory-module.ts, the validateQuantityAtLocation loop) checks each reservation input on its own against the level's available_quantity. It never sums the inputs that share a level.
That check is the only one that runs inside reserveInventoryStep's per-item lock. Any aggregate check a store adds, such as the completeCartWorkflow validate hook (the documented extension point for this), runs before that lock. So two carts can each pass it:
- stock 10
- cart A: 5 × piece
- cart B: 3 × piece + 2 × two-pack (4 units)
Both carts' hooks see 10 available and pass. A reserves 5 under the lock. B then reserves under the lock and checks each line against 5: 3 ≤ 5 and 4 ≤ 5, so it passes. Reserved becomes 12 of 10, and available_quantity is −2.
Measured on a scratch store over HTTP, with both completions fired together: 14 of 30 runs oversold. That is every run in which A reserved first. In-process with completeCartWorkflow it was 10 of 30. With single-line carts only, the per-item lock does its job: 0 of 60.
A second, related gap: more than one backend process. The lock is per process by default. createReservationItems_ writes reserved_quantity as an absolute value computed from its earlier read, so two processes lose one of the updates. The second process's update recomputes the same number, and the ORM sees no change and skips the write. With two servers on one database, the last unit was sold twice in 23 of 90 runs. Each time the level read reserved 1 against 2 reservation rows, so the oversell did not show in available_quantity at all.
Expected behavior
- A batch reservation (and the per-line loop in
ensureInventoryLevels) should check the sum per(inventory_item_id, location_id)against availability inside the lock. reserved_quantityshould be updated as an increment (reserved_quantity = reserved_quantity + n), or the check should be done against the reservation rows, so that separate processes cannot lose an update.
Actual behavior
Each line is checked alone, so concurrent carts oversell. Separately, reserved_quantity can fall below the sum of live reservations when more than one process writes it.
Link to reproduction repo
The steps are above. What we run in production as a stopgap is a Postgres AFTER INSERT OR UPDATE trigger on reservation_item. It locks the level row, sums the live reservations and raises when the sum exceeds stocked_quantity, skipping allow_backorder rows. With it, we measured 0 of 30 oversold for the multi-line case and 0 of 90 across two processes. We are happy to share it or to open a PR for the aggregate check.
This is one of several integrity defects on the checkout and inventory path that we have filed upstream from one store (#16998, #17003, #17030, #17031).
Source: medusajs/medusa