From 750419989b335b53279724db8e76434fc4085e72 Mon Sep 17 00:00:00 2001 From: FFspiky Date: Mon, 16 Nov 2026 09:05:49 +0800 Subject: [PATCH] Release compression reader results on destruction and read errors --- c-ext/compressionreader.c | 3 ++ tests/test_compressor_stream_reader.py | 70 ++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/c-ext/compressionreader.c b/c-ext/compressionreader.c index b14a63b1..3fe84e5e 100644 --- a/c-ext/compressionreader.c +++ b/c-ext/compressionreader.c @@ -19,6 +19,8 @@ static void compressionreader_dealloc(ZstdCompressionReader *self) { memset(&self->buffer, 0, sizeof(self->buffer)); } + Py_CLEAR(self->readResult); + PyObject_Del(self); } @@ -292,6 +294,7 @@ static PyObject *compressionreader_read(ZstdCompressionReader *self, readResult = read_compressor_input(self); if (-1 == readResult) { + Py_DECREF(result); return NULL; } else if (0 == readResult) { diff --git a/tests/test_compressor_stream_reader.py b/tests/test_compressor_stream_reader.py index 6c1fca21..828f83de 100644 --- a/tests/test_compressor_stream_reader.py +++ b/tests/test_compressor_stream_reader.py @@ -1,5 +1,9 @@ +import gc import io +import os +import sys import unittest +import weakref import zstandard as zstd @@ -10,6 +14,72 @@ class TestCompressor_stream_reader(unittest.TestCase): + def test_read_result_released_on_error(self): + class ReadResult: + pass + + refs = [] + + class Source: + def read(self, size): + result = ReadResult() + refs.append(weakref.ref(result)) + return result + + reader = zstd.ZstdCompressor().stream_reader(Source()) + with self.assertRaises(TypeError): + reader.read(1) + del reader + gc.collect() + self.assertTrue(refs[0]() is None) + + def test_read_result_released_on_destruction(self): + class ReadResult(bytearray): + pass + + refs = [] + + class Source: + def read(self, size): + result = ReadResult(os.urandom(1024 * 1024)) + refs.append(weakref.ref(result)) + return result + + reader = zstd.ZstdCompressor().stream_reader(Source()) + self.assertEqual(len(reader.read(1)), 1) + self.assertIsNotNone(refs[0]()) + del reader + gc.collect() + self.assertTrue(refs[0]() is None) + + @unittest.skipUnless( + sys.implementation.name == "cpython", "tracemalloc requires CPython" + ) + def test_read_error_releases_output(self): + import tracemalloc + + class Source: + def read(self, size): + raise OSError("source read failed") + + reader = zstd.ZstdCompressor().stream_reader(Source()) + size = 1024 * 1024 + already_tracing = tracemalloc.is_tracing() + if not already_tracing: + tracemalloc.start() + try: + gc.collect() + before = tracemalloc.get_traced_memory()[0] + for _ in range(8): + with self.assertRaisesRegex(OSError, "source read failed"): + reader.read(size) + gc.collect() + retained = tracemalloc.get_traced_memory()[0] - before + self.assertLess(retained, size) + finally: + if not already_tracing: + tracemalloc.stop() + def test_context_manager(self): cctx = zstd.ZstdCompressor()