Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Remove reference cycle in Saleor context (#17588)
Filip Owczarekeb911194170b
diff --git a/saleor/graphql/context.py b/saleor/graphql/context.pyindex ab8e059378..05176c3748 100644--- a/saleor/graphql/context.py+++ b/saleor/graphql/context.py@@ -28,6 +28,7 @@ def get_context_value(request: HttpRequest) -> SaleorContext:def clear_context(context: SaleorContext):context.dataloaders.clear()+ del context.userclass RequestWithUser(HttpRequest):diff --git a/saleor/graphql/core/tests/garbage_collection/test_saleor_context.py b/saleor/graphql/core/tests/garbage_collection/test_saleor_context.pynew file mode 100644index 0000000000..2ea239413d--- /dev/null+++ b/saleor/graphql/core/tests/garbage_collection/test_saleor_context.py@@ -0,0 +1,65 @@+import gc+import json++import pytest+from django.core.serializers.json import DjangoJSONEncoder++from .....core.jwt import create_access_token+from ....api import backend, schema+from ....tests.utils import get_graphql_content+from ....views import GraphQLView+from .utils import (+ clean_up_after_garbage_collection_test,+ disable_gc_for_garbage_collection_test,+)++PRODUCTS_QUERY = """+{+ me {+ }+}+"""+++# Group all tests that require garbage collection so that they do not run concurrently.+# This is necessary to ensure that tests don't interfere with each other.+# Without grouping we could receive false positive results.[email protected]_group(name="garbage_collection")+def test_query_remove_SaleorContext_memory_cycles(rf, staff_user):+ try:+ # given+ # Disable automatic garbage collection and set debugging flag.+ disable_gc_for_garbage_collection_test()+ # Prepare request body with GraphQL query.+ data = {"query": PRODUCTS_QUERY}+ data = json.dumps(data, cls=DjangoJSONEncoder)+ jwt_token = create_access_token(staff_user)++ # when+ # Execute the query as staff user.+ content = get_graphql_content(+ GraphQLView(backend=backend, schema=schema).handle_query(+ rf.post(+ path="/graphql/",+ data=data,+ content_type="application/json",+ headers={"authorization": f"JWT {jwt_token}"},+ )+ ),+ ignore_errors=True,+ )+ # Enforce garbage collection to populate the garbage list for inspection.+ gc.collect()++ # then+ # Ensure that the garbage list is empty. The garbage list is only valid+ # until the next collection cycle so we can only make assertions about it+ # before re-enabling automatic collection.+ assert gc.garbage == []+ # Ensure that the query returned the expected data.+ assert content["data"]["me"]["email"] == staff_user.email+ # Restore garbage collection settings to their original state. This should always be run to avoid interfering+ # with other tests to ensure that code should be executed in the `finally' block.+ finally:+ clean_up_after_garbage_collection_test()diff --git a/saleor/graphql/core/tests/test_graphql.py b/saleor/graphql/core/tests/test_graphql.pyindex 7d934964fb..db7bae7365 100644--- a/saleor/graphql/core/tests/test_graphql.py+++ b/saleor/graphql/core/tests/test_graphql.py@@ -28,6 +28,8 @@ def test_middleware_dont_generate_sql_requests(client, settings, assert_num_querdef test_jwt_middleware(client, admin_user):+ # We should't base on `wsgi_request` attribute to check if user is authenticated.+ # Saleor is ASGI app, also request could be changed after response is returned.user_details_query = """{me {@@ -51,14 +53,12 @@ def test_jwt_middleware(client, admin_user):response = api_client_post(data={"query": user_details_query})repl_data = response.json()assert response.status_code == 200- assert not response.wsgi_request.userassert repl_data["data"]["me"] is None# test creating a token for admin userresponse = api_client_post(data={"query": create_token_query})repl_data = response.json()assert response.status_code == 200- assert response.wsgi_request.user == admin_usertoken = repl_data["data"]["tokenCreate"]["token"]assert token is not None@@ -68,7 +68,6 @@ def test_jwt_middleware(client, admin_user):)repl_data = response.json()assert response.status_code == 200- assert response.wsgi_request.user == admin_userassert "errors" not in repl_dataassert repl_data["data"]["me"] == {"email": admin_user.email}diff --git a/saleor/graphql/giftcard/bulk_mutations/gift_card_bulk_create.py b/saleor/graphql/giftcard/bulk_mutations/gift_card_bulk_create.pyindex a750de4dde..29c39dcdd7 100644--- a/saleor/graphql/giftcard/bulk_mutations/gift_card_bulk_create.py+++ b/saleor/graphql/giftcard/bulk_mutations/gift_card_bulk_create.py@@ -91,8 +91,9 @@ class GiftCardBulkCreate(BaseMutation):instances = cls.create_instances(input, info)if tags:cls.assign_gift_card_tags(instances, tags)+ manager = get_plugin_manager_promise(info.context).get()transaction.on_commit(- lambda: cls.call_gift_card_created_on_plugins(instances, info.context)+ lambda: cls.call_gift_card_created_on_plugins(instances, manager))return cls(count=len(instances), gift_cards=instances)@@ -172,8 +173,7 @@ class GiftCardBulkCreate(BaseMutation):tag_instance.gift_cards.set(instances)@classmethod- def call_gift_card_created_on_plugins(cls, instances, context):+ def call_gift_card_created_on_plugins(cls, instances, manager):webhooks = get_webhooks_for_event(WebhookEventAsyncType.GIFT_CARD_CREATED)- manager = get_plugin_manager_promise(context).get()for instance in instances:cls.call_event(manager.gift_card_created, instance, webhooks=webhooks)diff --git a/saleor/graphql/order/mutations/order_confirm.py b/saleor/graphql/order/mutations/order_confirm.pyindex abf572b701..ec98a81068 100644--- a/saleor/graphql/order/mutations/order_confirm.py+++ b/saleor/graphql/order/mutations/order_confirm.py@@ -92,7 +92,7 @@ class OrderConfirm(DeprecatedModelMutation):transaction.on_commit(lambda: order_charged(order_info,- info.context.user,+ user,app,authorized_payment.total,authorized_payment,