Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix deadlocks on checkout and order (#19001)
Iga Karbowiak74887e787f83
diff --git a/saleor/checkout/migrations/tasks/saleor3_20.py b/saleor/checkout/migrations/tasks/saleor3_20.pyindex d009c2258d..48d898ae96 100644--- a/saleor/checkout/migrations/tasks/saleor3_20.py+++ b/saleor/checkout/migrations/tasks/saleor3_20.py@@ -1,10 +1,12 @@from django.conf import settings+from django.db import transactionfrom django.db.models import Exists, OuterReffrom ....celeryconf import appfrom ....core.db.connection import allow_writerfrom ....discount import DiscountTypefrom ....discount.models import CheckoutLineDiscount+from ...lock_objects import checkout_lines_qs_select_for_updatefrom ...models import Checkout, CheckoutLine# Takes about 0.1 second to process@@ -49,5 +51,11 @@ def clean_duplicated_gift_lines_task(created_after=None):else:seen_checkouts.add(line.checkout_id)- CheckoutLine.objects.filter(id__in=lines_to_delete).delete()+ with transaction.atomic():+ pks = (+ checkout_lines_qs_select_for_update()+ .filter(id__in=lines_to_delete)+ .values_list("id", flat=True)+ )+ CheckoutLine.objects.filter(id__in=pks).delete()clean_duplicated_gift_lines_task.delay(created_after=checkout_created_after)diff --git a/saleor/checkout/migrations/tasks/saleor3_21.py b/saleor/checkout/migrations/tasks/saleor3_21.pyindex e9dcaf6139..ad334ff290 100644--- a/saleor/checkout/migrations/tasks/saleor3_21.py+++ b/saleor/checkout/migrations/tasks/saleor3_21.py@@ -1,8 +1,10 @@from django.conf import settings+from django.db import transactionfrom django.db.models import Exists, OuterReffrom ....account.models import Addressfrom ....celeryconf import app+from ....checkout.lock_objects import checkout_qs_select_for_updatefrom ....checkout.models import Checkoutfrom ....core.db.connection import allow_writerfrom ....order.models import Order@@ -46,6 +48,12 @@ def fix_shared_address_instances_task(field=BILLING_FIELD):for checkout, address in zip(checkout_instances, addresses, strict=True):setattr(checkout, field, address)- Checkout.objects.bulk_update(checkout_instances, [field])+ with transaction.atomic():+ list(+ checkout_qs_select_for_update()+ .filter(pk__in=[c.pk for c in checkout_instances])+ .values_list("pk", flat=True)+ )+ Checkout.objects.bulk_update(checkout_instances, [field])fix_shared_address_instances_task.delay(field=field)diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex 7647689b64..3fdffc7c18 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -151,14 +151,16 @@ def delete_checkouts(checkout_pks_to_delete: list[UUID]) -> int:"""Delete a checkouts with lock applied on them."""with transaction.atomic():CheckoutLine.objects.filter(- id__in=CheckoutLine.objects.order_by("id")- .select_for_update()+ id__in=checkout_lines_qs_select_for_update().filter(checkout_id__in=checkout_pks_to_delete).values_list("id", flat=True)).delete()- deleted_count, _ = Checkout.objects.filter(- pk__in=checkout_pks_to_delete- ).delete()+ locked_pks = list(+ checkout_qs_select_for_update()+ .filter(pk__in=checkout_pks_to_delete)+ .values_list("pk", flat=True)+ )+ deleted_count, _ = Checkout.objects.filter(pk__in=locked_pks).delete()return deleted_countdiff --git a/saleor/graphql/order/mutations/order_line_delete.py b/saleor/graphql/order/mutations/order_line_delete.pyindex 13058b025c..b09bd939ec 100644--- a/saleor/graphql/order/mutations/order_line_delete.py+++ b/saleor/graphql/order/mutations/order_line_delete.py@@ -5,6 +5,7 @@ from ....core.tracing import traced_atomic_transactionfrom ....order import eventsfrom ....order.error_codes import OrderErrorCodefrom ....order.fetch import OrderLineInfo+from ....order.lock_objects import order_qs_select_for_updatefrom ....order.search import update_order_search_vectorfrom ....order.utils import (delete_order_line,@@ -61,6 +62,11 @@ class OrderLineDelete(EditableOrderValidationMixin, BaseMutation):else None)with traced_atomic_transaction():+ # Lock the order before modifying lines to ensure consistent+ # lock ordering (order → lines) with `process_order_prices`,+ # and preventing deadlocks.+ order_qs_select_for_update().only("pk").get(pk=order.pk)+line_info = OrderLineInfo(line=line,quantity=line.quantity,