[Bug]: In-memory locking: a multi-key execute that waits on its 2nd key waits on its own 1st key until timeout (5 s stall, then 500 at checkout)
Package.json file
{
"dependencies": {
"@medusajs/framework": "2.19.0",
"@medusajs/medusa": "2.19.0"
}
}(Resolved @medusajs/locking 2.19.0. The same code is unchanged on develop at 62a520014, after #16768. Permalink below.)
Node.js version
v22.22.2 (also on node:20-slim)
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?
The default in-memory locking provider cannot acquire more than one key if it has to wait on any key after the first. It waits on its own first key until the timeout, then throws Timed-out acquiring lock. The first key stays locked until it expires.
In a real store this is reserveInventoryStep, which calls locking.execute(inventoryItemIds, …). A checkout with two or more items fails whenever another checkout is reserving any item other than its first one. The shopper waits 5 s and then gets a 500. On our scratch store, a one-item cart racing a two-item cart on the two-item cart's second item failed like this in 7 of 10 runs over HTTP, and in 15 of 30 runs in-process.
Cause (packages/modules/locking/src/providers/in-memory.ts, acquire_):
- The loop takes the keys in order. The first key is free, so it is stored with
ownerId: null. - The second key is held, so it
awaits that lock's promise. That part is correct. - It then does
return this.acquire_(keys, args, cancellationToken), which restarts from the first key. - The first key now exists, is not expired, and has
ownerId === null. The owner checklock.ownerId !== null && lock.ownerId === ownerIdis false, so it awaits the first key's own promise. Only a release resolves that promise, and nobody will release it. So it waits untilgetTimeoutrejects. executethrows before itstry/finally, sorelease(keys)never runs. The first key stays held untilexpiration(= timeout, 5 s), and it blocks every other request for that key meanwhile.
A related issue: keys are taken in the caller's order. reserveInventoryStep passes Array.from(new Set(inventoryItemIds)) in cart-line order, not sorted.
Expected behavior
execute(["A", "B"], job) should run job as soon as B is released. When it times out, it should release whatever it acquired.
Actual behavior
It always times out after the full timeout, and the key it did take stays locked for another timeout seconds.
Link to reproduction repo
The provider alone reproduces this; no app is needed. Run it in any Medusa 2.19 project:
const { InMemoryLockingProvider } = require("@medusajs/locking/dist/providers/in-memory")
const keepAlive = setInterval(() => {}, 1000)
const provider = new InMemoryLockingProvider()
const t0 = Date.now()
// Request 1 holds "B" for 100 ms.
const first = provider.execute(["B"], () => new Promise((r) => setTimeout(r, 100)))
// Request 2 needs "A" then "B". It should run at ~100 ms.
const second = provider.execute(["A", "B"], async () => "ran")
Promise.allSettled([first, second]).then(async (r) => {
console.log("second:", r[1].status, r[1].value ?? r[1].reason.message, `${Date.now() - t0}ms`)
clearInterval(keepAlive)
})Output on 2.19.0:
second: rejected Timed-out acquiring lock. 5003msA fix that takes the keys in sorted order, resumes waiting at the key it was blocked on (instead of restarting), and releases the taken keys on timeout gives second: fulfilled ran 102ms. Taking the keys in sorted order also rules out an AB/BA deadlock between two requests for the same items. We are running exactly that as a replacement provider, and are happy to open a PR.
This is one of several integrity defects on the checkout and inventory path that we have filed upstream from one store (#16998, #17003, #17030, and the companion issue on cross-cart oversell, filed alongside this one).
Source: medusajs/medusa