Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix concurrent access to Checkout metadata

Patryk Zawadzki58e476b970ef
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e42097959d..b20494d0fc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,3 +18,4 @@ All notable, unreleased changes to this project will be documented in this file.
### Other changes
- Added support for numeric and lower-case boolean environment variables - #16313 by @NyanKiyoshi
+- Fixed a potential crash when Checkout metadata is accessed with high concurrency - #16411 by @patrys
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index 38b9d24f05..fe12776921 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -1022,8 +1022,8 @@ def delete_external_shipping_id(checkout: Checkout, save: bool = False):
def get_or_create_checkout_metadata(checkout: "Checkout") -> CheckoutMetadata:
if hasattr(checkout, "metadata_storage"):
return checkout.metadata_storage
- else:
- return CheckoutMetadata.objects.create(checkout=checkout)
+ metadata, _ = CheckoutMetadata.objects.get_or_create(checkout=checkout)
+ return metadata
@allow_writer()