-
Notifications
You must be signed in to change notification settings - Fork 135
BugFix: users can cancel an order cooperatively at the same time #733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…nction and update the cancelOrder command to utilize it.
WalkthroughIntroduces a helper function Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
bot/commands.ts (1)
734-758:⚠️ Potential issue | 🟠 MajorEmit
orderUpdatedwith the freshly saved order to avoid stale event payloads.
updateOrderis persisted withstatus = 'CANCELED', but the event is emitted with the pre-updateorder, so listeners may seeACTIVEand miss the cancellation. EmitupdateOrderinstead (or re-fetch) to keep downstream state consistent.🛠️ Proposed fix
- logger.info('cancelOrder => OrderEvents.orderUpdated(order);'); - OrderEvents.orderUpdated(order); + logger.info('cancelOrder => OrderEvents.orderUpdated(updateOrder);'); + OrderEvents.orderUpdated(updateOrder);
mostronator
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM ✅ Good fix for the race condition in cooperative cancellation (#583).
What was wrong: Both parties could read the order, see the other hadn't cancelled yet, then both set their own flag and save — causing a race where the mutual-cancel branch was never reached.
What this fixes: Uses findOneAndUpdate with { new: true } to atomically set the cooperative cancel flag, then checks the returned document. This eliminates the TOCTOU race.
Minor note: On line OrderEvents.orderUpdated(order) (after the cancel branch), you're passing the original order object instead of updateOrder. This is likely fine since the event just needs the order ID, but for consistency it should ideally be updateOrder. Not a blocker.
Good work, resolves #583.
Description
Fix race condition when processing simultaneous cooperative cancellations: if buyer and seller send
/cancel order-idat nearly the same time both flags (buyer_cooperativecancel,seller_cooperativecancel) were set but the order remained inActivestatus.Now the operation uses an atomic update/lock so when both confirm the order transitions to
cancelledand bot replies remain consistent.Summary by CodeRabbit