Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Remove reference cycle in gzip (#17589)
Filip Owczarekaba2e11b9836
diff --git a/saleor/asgi/gzip_compression.py b/saleor/asgi/gzip_compression.pyindex e9cd558ca6..34caf54f4f 100644--- a/saleor/asgi/gzip_compression.py+++ b/saleor/asgi/gzip_compression.py@@ -139,6 +139,8 @@ def gzip_compression(await send(message)await app(scope, receive, send_compressed)+ gzip_file.close()+ gzip_buffer.close()returnawait app(scope, receive, send)diff --git a/saleor/graphql/core/tests/garbage_collection/test_gzip.py b/saleor/graphql/core/tests/garbage_collection/test_gzip.pynew file mode 100644index 0000000000..a556eddcc2--- /dev/null+++ b/saleor/graphql/core/tests/garbage_collection/test_gzip.py@@ -0,0 +1,40 @@+import gc+import gzip+import io++import pytest++from .utils import (+ clean_up_after_garbage_collection_test,+ disable_gc_for_garbage_collection_test,+)+++# 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_GzipFile_with_BytesIO_buffer_remove_all_reference_cycles():+ try:+ # given+ # Disable automatic garbage collection and set debugging flag.+ disable_gc_for_garbage_collection_test()++ # when+ # Create GzipFile object with BytesIO buffer and close it to free resources.+ with io.BytesIO() as buffer:+ with gzip.GzipFile(mode="wb", fileobj=buffer):+ pass++ # 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 == []+ # 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/patch_gzip.py b/saleor/patch_gzip.pynew file mode 100644index 0000000000..69fd007775--- /dev/null+++ b/saleor/patch_gzip.py@@ -0,0 +1,16 @@+from gzip import _WriteBufferStream # type: ignore[attr-defined]+++def __del_tmp__(self):+ del self.gzip_file+++def patch_gzip():+ """Patch `__del__` in `_WriteBufferStream` from `gzip` to avoid memory leaks.++ Those changes will remove the circular references in `GzipFile`, allowing memory to be freed immediately,+ without the need of a deep garbage collection cycle.+ Cycle: `GzipFile._buffer` -> `BufferedWriter._raw` -> `_WriteBufferStream.gzip_file` -> `GzipFile`.+ Issue: https://github.com/python/cpython/issues/129640+ """+ _WriteBufferStream.__del__ = __del_tmp__diff --git a/saleor/settings.py b/saleor/settings.pyindex 9d44b0c09c..cc06de0fdd 100644--- a/saleor/settings.py+++ b/saleor/settings.py@@ -33,6 +33,7 @@ from .core.languages import LANGUAGES as CORE_LANGUAGESfrom .core.schedules import initiated_promotion_webhook_schedulefrom .graphql.executor import patch_executorfrom .graphql.promise import patch_promise+from .patch_gzip import patch_gzipdjango_stubs_ext.monkeypatch()@@ -1064,3 +1065,7 @@ i18n_rules_override()# Patch Promise to remove all references that could result in reference cycles, allowing memory to be freed# immediately, without the need of a deep garbage collection cycle.patch_promise()++# Patch `_WriteBufferStream` from `gizip` to remove all references that could result in reference cycles,+# allowing memory to be freed immediately, without the need of a deep garbage collection cycle.+patch_gzip()