Conversation
There was a problem hiding this comment.
Pull request overview
Resolves the checkout-race-condition in checkout-api by preventing overselling during concurrent inventory reservations and adjusting concurrent checkout handling to tolerate expected out-of-stock failures.
Changes:
- Add a commit-time (post-delay) inventory availability guard in
reserveStockto prevent negative inventory under concurrency. - Make concurrent checkout use
Promise.allSettledand treatOutOfStockrejections as expected while still surfacing unexpected failures.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| demo_app/src/inventory/reserve-stock.ts | Adds a post-delay availability check and aligns returned reservation metadata with the committed record state. |
| demo_app/src/checkout/submit-order.ts | Switches concurrent execution to allSettled, ignoring expected OutOfStock failures while propagating other errors. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const hardFailure = settled.find( | ||
| (result) => | ||
| result.status === "rejected" && | ||
| !(result.reason instanceof Error && result.reason.message.startsWith("OutOfStock:")) | ||
| ); |
There was a problem hiding this comment.
Classifying an expected failure by checking error.message.startsWith("OutOfStock:") is brittle (message text changes will alter control flow). Prefer a dedicated OutOfStockError (or an error.code field) thrown from reserveStock, and filter using instanceof/code instead of string parsing.
| if (record.available < quantity) { | ||
| throw new Error(`OutOfStock: ${sku}`); | ||
| } |
There was a problem hiding this comment.
Now that a post-delay availability check is added (and the concurrent negative-inventory path should be prevented), the nearby comment describing this delay as an "Intentional bug" is misleading. Update the comment to reflect the current behavior (e.g., that the delay simulates latency but the commit-time check prevents oversell).
Summary
Resolve
checkout-race-conditionforcheckout-apiwith a validated ReplayX patch candidate.Changed Files
Validation
Rollback
Revert the live inventory guard and concurrent checkout settlement handling.