Skip to content

Decode compressed response bodies incrementally - #1126

Merged
Kludex merged 17 commits into
mainfrom
incremental-content-decoding
Aug 18, 2026
Merged

Decode compressed response bodies incrementally#1126
Kludex merged 17 commits into
mainfrom
incremental-content-decoding

Conversation

@Kludex

@Kludex Kludex commented Aug 9, 2026

Copy link
Copy Markdown
Member

Summary

The content decoders used to inflate each raw chunk fully in a single decompress() call and re-chunk the result afterwards, so a small compressed chunk could inflate to an arbitrarily large buffer and iter_bytes(chunk_size) did not actually bound peak memory.

This reworks the decoders to yield bounded pieces as they decode:

  • gzip and deflate drain a shared zlib decompressor with max_length.
  • Brotli uses output_buffer_limit, adjusted for the C backend's allocation behavior.
  • Zstandard uses max_length from compression.zstd or backports.zstd.
  • MultiDecoder pipes its children lazily, so the bound holds across stacked encodings.

Each decode step is bounded to 1 MiB (MAX_DECODE_CHUNK_SIZE). iter_bytes(chunk_size) and aiter_bytes(chunk_size) now bound peak memory rather than materializing a whole decoded chunk before re-slicing it.

The change also closes the underlying stream when decoding raises part-way through, so a decode error releases the connection instead of leaking it.

Validation

  • scripts/check
  • scripts/test && scripts/coverage - 1,990 passed, 1 skipped, 100% coverage
  • decoder tests on Python 3.13 and 3.14 - 54 passed on each

AI Disclaimer

This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.

Previously each raw chunk was fully inflated in a single `decompress()`
call before being re-chunked, so a small compressed chunk could inflate
to an arbitrarily large buffer and `iter_bytes(chunk_size)` did not
actually bound memory.

Rework the content decoders to yield bounded pieces as they decode:
`gzip`/`deflate` drain a shared `ZlibDecompressor` with `max_length`,
`brotli` uses `output_buffer_limit` (now requires `brotli>=1.2.0`), and
`zstd` uses `max_length` on the stdlib `compression.zstd` backend.
`MultiDecoder` pipes children lazily so the bound holds across stacked
encodings. `iter_bytes(chunk_size)` now bounds peak memory like
urllib3's `read(amt)`.

Also close the underlying stream when decoding raises part-way through,
so a decode error releases the connection instead of leaking it.
@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 472041bacf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/httpx2/pyproject.toml

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 6 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread src/httpx2/httpx2/_decoders.py Outdated
Comment thread src/httpx2/pyproject.toml Outdated
@codspeed-hq

codspeed-hq Bot commented Aug 9, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 17 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing incremental-content-decoding (7924107) with main (d588e52)

Open in CodSpeed

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 3 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread src/httpx2/httpx2/_decoders.py Outdated

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 8 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread tests/httpx2/test_decoders.py
Comment thread src/httpx2/pyproject.toml
…ecoding

# Conflicts:
#	src/httpx2/httpx2/_decoders.py
Comment thread src/httpx2/httpx2/_decoders.py Outdated
@Kludex
Kludex merged commit 4fd0c70 into main Aug 18, 2026
19 checks passed
@Kludex
Kludex deleted the incremental-content-decoding branch August 18, 2026 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant