From fba12be30aa6e545c9ec2bbeb4c87e31961360fd Mon Sep 17 00:00:00 2001 From: "Michiel W. Beijen" Date: Mon, 27 Oct 2025 17:49:37 +0100 Subject: [PATCH 1/2] bugfix for zstd decompressobj reuse Fixes https://github.com/encode/httpx/discussions/3538 --- httpx/_decoders.py | 8 +++++++- tests/test_decoders.py | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/httpx/_decoders.py b/httpx/_decoders.py index 899dfada87..9043c1cf6e 100644 --- a/httpx/_decoders.py +++ b/httpx/_decoders.py @@ -176,6 +176,7 @@ def __init__(self) -> None: self.decompressor = zstandard.ZstdDecompressor().decompressobj() self.seen_data = False + self.seen_eof = False def decode(self, data: bytes) -> bytes: assert zstandard is not None @@ -187,6 +188,11 @@ def decode(self, data: bytes) -> bytes: unused_data = self.decompressor.unused_data self.decompressor = zstandard.ZstdDecompressor().decompressobj() output.write(self.decompressor.decompress(unused_data)) + # If the decompressor reached EOF, create a new one for the next call + # since zstd decompressors cannot be reused after EOF + if self.decompressor.eof: + self.seen_eof = True + self.decompressor = zstandard.ZstdDecompressor().decompressobj() except zstandard.ZstdError as exc: raise DecodingError(str(exc)) from exc return output.getvalue() @@ -195,7 +201,7 @@ def flush(self) -> bytes: if not self.seen_data: return b"" ret = self.decompressor.flush() # note: this is a no-op - if not self.decompressor.eof: + if not self.decompressor.eof and not self.seen_eof: raise DecodingError("Zstandard data is incomplete") # pragma: no cover return bytes(ret) diff --git a/tests/test_decoders.py b/tests/test_decoders.py index 9ffaba189d..8f0db9ae44 100644 --- a/tests/test_decoders.py +++ b/tests/test_decoders.py @@ -141,6 +141,26 @@ def test_zstd_multiframe(): assert response.content == b"foobar" +def test_zstd_streaming_multiple_frames(): + body1 = b"test 123 " + body2 = b"another frame" + + # Create two separate complete frames + frame1 = zstd.compress(body1) + frame2 = zstd.compress(body2) + + # Create an iterator that yields frames separately + def content_iterator() -> typing.Iterator[bytes]: + yield frame1 + yield frame2 + + headers = [(b"Content-Encoding", b"zstd")] + response = httpx.Response(200, headers=headers, content=content_iterator()) + response.read() + + assert response.content == body1 + body2 + + def test_multi(): body = b"test 123" From 70c4d371eac00fdfe4ca87af6746c043cfe28291 Mon Sep 17 00:00:00 2001 From: "Michiel W. Beijen" Date: Mon, 23 Nov 2026 07:37:03 +0100 Subject: [PATCH 2/2] Detect truncated zstd frames arriving after a completed frame Instead of tracking a sticky `seen_eof` flag, reset the decompressor lazily when new data arrives after a completed frame. This lets `flush()` check whether the current frame is complete, so a truncated second frame in a later chunk now raises DecodingError. Co-Authored-By: Claude Opus 5.5 --- httpx/_decoders.py | 15 +++++++-------- tests/test_decoders.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/httpx/_decoders.py b/httpx/_decoders.py index 9043c1cf6e..a437d432dc 100644 --- a/httpx/_decoders.py +++ b/httpx/_decoders.py @@ -176,23 +176,22 @@ def __init__(self) -> None: self.decompressor = zstandard.ZstdDecompressor().decompressobj() self.seen_data = False - self.seen_eof = False def decode(self, data: bytes) -> bytes: assert zstandard is not None self.seen_data = True output = io.BytesIO() try: + # zstd decompressors cannot be reused after EOF, so start a new one + # when more data arrives after a completed frame. This is done lazily + # so that `flush()` can check whether the current frame is complete. + if data and self.decompressor.eof: + self.decompressor = zstandard.ZstdDecompressor().decompressobj() output.write(self.decompressor.decompress(data)) while self.decompressor.eof and self.decompressor.unused_data: unused_data = self.decompressor.unused_data self.decompressor = zstandard.ZstdDecompressor().decompressobj() output.write(self.decompressor.decompress(unused_data)) - # If the decompressor reached EOF, create a new one for the next call - # since zstd decompressors cannot be reused after EOF - if self.decompressor.eof: - self.seen_eof = True - self.decompressor = zstandard.ZstdDecompressor().decompressobj() except zstandard.ZstdError as exc: raise DecodingError(str(exc)) from exc return output.getvalue() @@ -201,8 +200,8 @@ def flush(self) -> bytes: if not self.seen_data: return b"" ret = self.decompressor.flush() # note: this is a no-op - if not self.decompressor.eof and not self.seen_eof: - raise DecodingError("Zstandard data is incomplete") # pragma: no cover + if not self.decompressor.eof: + raise DecodingError("Zstandard data is incomplete") return bytes(ret) diff --git a/tests/test_decoders.py b/tests/test_decoders.py index 8f0db9ae44..e9bfeaf6ab 100644 --- a/tests/test_decoders.py +++ b/tests/test_decoders.py @@ -161,6 +161,34 @@ def content_iterator() -> typing.Iterator[bytes]: assert response.content == body1 + body2 +def test_zstd_streaming_truncated_second_frame(): + frame1 = zstd.compress(b"first") + frame2 = zstd.compress(b"second") + + def content_iterator() -> typing.Iterator[bytes]: + yield frame1 + yield frame2[:12] + + headers = [(b"Content-Encoding", b"zstd")] + response = httpx.Response(200, headers=headers, content=content_iterator()) + with pytest.raises(httpx.DecodingError): + response.read() + + +def test_zstd_streaming_empty_chunk_after_frame(): + frame = zstd.compress(b"complete") + + def content_iterator() -> typing.Iterator[bytes]: + yield frame + yield b"" + + headers = [(b"Content-Encoding", b"zstd")] + response = httpx.Response(200, headers=headers, content=content_iterator()) + response.read() + + assert response.content == b"complete" + + def test_multi(): body = b"test 123"