[Bug]: Concurrent createReservationItems calls can oversubscribe stock and desynchronize reserved_quantity
Package.json file
{
"name": "medusa-t01-backend",
"version": "0.0.1",
"dependencies": {
"@medusajs/framework": "2.19.0",
"@medusajs/medusa": "2.19.0",
"@medusajs/inventory": "2.19.0",
"@medusajs/locking": "2.19.0"
},
"engines": { "node": ">=20" }
}Node.js version
v22.23.2
Database and its version
PostgreSQL 17.7
Operating system name and version
Windows 10
Browser name
No response
What happended?
On Medusa 2.19.0, concurrent direct calls to IInventoryService.createReservationItems() — that is, calls made without the locking step used by Medusa's shipped workflows — can both succeed for the same inventory item when only one unit is available.
This can produce two distinct effects:
- more live
ReservationItemrows than the available stock allows; reserved_quantitybecoming inconsistent with the actual reservation rows because of a lost update.
The standard Medusa flows we verified are not being reported as overselling. The standard cart completion flow and the admin reservation flow both wrap reservation creation in the Locking Module using inventory_item_id as the locking key. This report is about the safety contract of the Inventory Module service method when application code calls it directly.
Observed behavior
In repeated testing, concurrent calls without a lock:
successful reservations = 2
ReservationItem rows = 2
stocked_quantity = 1
reserved_quantity = 1
available_quantity = 0Measured over 50 runs:
- without lock: 50 / 50 runs produced two reservation rows
- with lock: 0 / 50 runs produced oversubscription
Wrapping the same reservation creation in the official Locking Module:
await locking.execute([inventory_item_id], async () => {
return await inventoryModuleService.createReservationItems(...)
})allowed only one caller to acquire the last unit.
reserved_quantity divergence
The concurrency issue also exposed a separate consistency problem.
When both reservation creations succeed concurrently, two ReservationItem rows can exist while reserved_quantity is only incremented once. So the state can become:
stocked_quantity = 1
reserved_quantity = 1
live ReservationItem rows = 2The aggregate counter therefore hides the oversubscription.
Subsequent deletion of both reservation rows decrements the counter once per deleted reservation, even though it had only been incremented once. During repeated reproduction this caused reserved_quantity to become negative while no live reservation rows remained.
We also did not find a supported Inventory Module API for explicitly reconciling reserved_quantity; updating the inventory level does not accept this field.
Relationship to medusajs/medusa#16502
Related to medusajs/medusa#16502, which reports over-reservation through a different mechanism: multiple entries for the same (inventory_item_id, location_id) inside a single createReservationItems batch, validated independently against the same undecremented available_quantity. That report explicitly notes it is deterministic and involves no concurrency.
The case reported here is two separate concurrent calls in different transactions. PR #16505 aggregates demand per key within a batch, which does not introduce locking or row-level protection, so concurrent separate calls are unaffected by it.
The two also differ in outcome on the counter: medusajs/medusa#16502 shows reserved_quantity exceeding stocked_quantity, whereas here the increment is lost and the counter reads lower than the number of live reservations.
API contract question
We would like clarification on the intended contract of createReservationItems. Is one of the following expected?
- callers must already hold a Locking Module lock for the relevant
inventory_item_id; - the service method itself is expected to provide concurrency safety;
- application code should use another higher-level concurrency-safe reservation primitive.
If locking is intentionally the caller's responsibility, it may be useful to document this explicitly on the Inventory Module API, because the direct service method otherwise appears self-contained.
Separately, is the divergence between ReservationItem rows and reserved_quantity considered a bug? If divergence occurs, is there a supported reconciliation mechanism that can restore reserved_quantity from the actual reservation records?
Minimal reproduction
Start with one inventory item and one inventory level:
stocked_quantity = 1
reserved_quantity = 0
available_quantity = 1Then execute two direct concurrent reservation calls for the same item/location:
const reserve = () =>
inventoryModuleService.createReservationItems({
inventory_item_id,
location_id,
quantity: 1,
})
const result = await Promise.allSettled([reserve(), reserve()])Expected behavior
At most one of the two concurrent calls succeeds. The second is rejected with Not enough stock available for item ... at location ..., exactly as it is when the two calls are made sequentially.
reserved_quantity always reflects the number and quantity of live ReservationItem rows, and never goes below zero.
Actual behavior
Both calls succeed. Two ReservationItem rows exist for a single unit of stock, and reserved_quantity reads 1 rather than 2 — the second increment is lost.
Because the increment is lost, the familiar invariant reserved_quantity <= stocked_quantity still holds (1 <= 1) while two reservations exist for one unit, so the aggregate counter does not reveal the oversubscription. Detecting it requires counting ReservationItem rows.
Deleting the two reservations then decrements the counter twice against a single increment, leaving reserved_quantity negative with no live reservations remaining.
Link to reproduction repo
No isolated repo needed — reproducible against the Inventory Module directly. See the snippet in "What happened?"; it runs via npx medusa exec on a stock 2.19.0 install. Happy to publish a standalone repo if preferred.
Source: medusajs/medusa