feat: add transaction-level advisory locks to sync_from_ui to prevent concurrent duplicate order processing

This commit is contained in:
Suherdy Yacob 2026-06-19 09:27:31 +07:00
parent fd66566e91
commit b5fa919327

View File

@ -100,3 +100,21 @@ class PosOrder(models.Model):
return super(PosOrder, self)._process_order(order, existing_order)
@api.model
def sync_from_ui(self, orders):
"""
Override to prevent concurrent processing of the same order uuid.
Uses pg_advisory_xact_lock on the order's uuid to force subsequent
duplicate requests to wait until the first request completes/commits.
"""
# Sort orders by uuid to prevent potential deadlocks when locking multiple orders
sorted_orders = sorted(orders, key=lambda x: x.get('uuid') or '')
for order in sorted_orders:
uuid = order.get('uuid')
if uuid:
# Use PostgreSQL transaction-level advisory lock
self.env.cr.execute("SELECT pg_advisory_xact_lock(hashtext(%s))", (uuid,))
return super(PosOrder, self).sync_from_ui(orders)