Stop simultaneous transfers from overdrawing a branch #4
Reference in New Issue
Block a user
Delete Branch "fix/transfer-race"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
A transfer read the sending branch's stock and then wrote its movements as two separate steps. Requests arriving together all saw the same balance and all passed the check. Reproduced with a test before fixing: five simultaneous transfers of 4 from a stock of 10 all succeeded, sending 20 units.
Fix
withStockLock(src/lib/server/stock.ts): a short lease on one product's stock at one branch, stored in a newStockLockcollection.docs/PILOT_READINESS.md).No MongoDB transactions or replica-set requirement; works on Atlas and a standalone local database alike.
Testing
npm test: 62 tests pass, 6 new intests/concurrency.test.ts:No migration or deploy step needed.
🤖 Generated with Claude Code