Skip to content

fix: Fix zstd decompression of multi-frame responses#12290

Open
josumoreno-BP wants to merge 2 commits intoaio-libs:masterfrom
josumoreno-BP:fix/zstd-multiframe
Open

fix: Fix zstd decompression of multi-frame responses#12290
josumoreno-BP wants to merge 2 commits intoaio-libs:masterfrom
josumoreno-BP:fix/zstd-multiframe

Conversation

@josumoreno-BP
Copy link
Copy Markdown

ZstdDecompressor is one-shot-per-frame: once a frame ends, subsequent decompress() calls raise EOFError. This broke HTTP responses where the server sends multiple zstd frames (common with chunked transfer encoding).

Detect frame boundaries via eof/unused_data attributes and create fresh decompressor instances for subsequent frames.

What do these changes do?

Detect frame boundaries via eof/unused_data attributes and create fresh decompressor instances for subsequent frames.

Are there changes in behavior for the user?

The change is seamless for the uses

Is it a substantial burden for the maintainers to support this?

I think this is needed to cover multi-framed zstd responses. I tried to keep it simple but covering the cases that came to my mind.

Related issue number

Fixes #12234

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes - I don't think we need to update the docs in this case
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES/ folder
    • name it <issue_or_pr_num>.<type>.rst (e.g. 588.bugfix.rst)

    • if you don't have an issue number, change it to the pull request
      number after creating the PR

      • .bugfix: A bug fix for something the maintainers deemed an
        improper undesired behavior that got corrected to match
        pre-agreed expectations.
      • .feature: A new behavior, public APIs. That sort of stuff.
      • .deprecation: A declaration of future API removals and breaking
        changes in behavior.
      • .breaking: When something public is removed in a breaking way.
        Could be deprecated in an earlier release.
      • .doc: Notable updates to the documentation structure or build
        process.
      • .packaging: Notes for downstreams about unobvious side effects
        and tooling. Changes in the test invocation considerations and
        runtime assumptions.
      • .contrib: Stuff that affects the contributor experience. e.g.
        Running tests, building the docs, setting up the development
        environment.
      • .misc: Changes that are hard to assign to any of the above
        categories.
    • Make sure to use full sentences with correct case and punctuation,
      for example:

      Fixed issue with non-ascii contents in doctest text files
      -- by :user:`contributor-gh-handle`.

      Use the past tense or the present tense a non-imperative mood,
      referring to what's changed compared to the last released version
      of this project.

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Mar 27, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.11%. Comparing base (993989c) to head (9a000fd).
⚠️ Report is 145 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #12290   +/-   ##
=======================================
  Coverage   99.11%   99.11%           
=======================================
  Files         130      130           
  Lines       45446    45524   +78     
  Branches     2398     2403    +5     
=======================================
+ Hits        45043    45122   +79     
  Misses        272      272           
+ Partials      131      130    -1     
Flag Coverage Δ
CI-GHA 98.97% <98.75%> (+<0.01%) ⬆️
OS-Linux 98.71% <98.75%> (ø)
OS-Windows 96.98% <98.75%> (+<0.01%) ⬆️
OS-macOS 97.87% <98.75%> (+<0.01%) ⬆️
Py-3.10.11 97.42% <97.50%> (+<0.01%) ⬆️
Py-3.10.20 97.89% <97.50%> (-0.01%) ⬇️
Py-3.11.15 98.09% <97.50%> (-0.01%) ⬇️
Py-3.11.9 97.63% <97.50%> (-0.01%) ⬇️
Py-3.12.10 97.72% <97.50%> (+<0.01%) ⬆️
Py-3.12.13 98.19% <97.50%> (-0.01%) ⬇️
Py-3.13.12 98.43% <97.50%> (-0.01%) ⬇️
Py-3.14.3 98.49% <97.50%> (-0.01%) ⬇️
Py-3.14.3t 97.50% <97.50%> (+<0.01%) ⬆️
Py-pypy3.11.15-7.3.21 97.40% <22.50%> (-0.14%) ⬇️
VM-macos 97.87% <98.75%> (+<0.01%) ⬆️
VM-ubuntu 98.71% <98.75%> (ø)
VM-windows 96.98% <98.75%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 27, 2026

Merging this PR will not alter performance

✅ 59 untouched benchmarks


Comparing josumoreno-BP:fix/zstd-multiframe (9a000fd) with master (07a2ca4)

Open in CodSpeed

ZstdDecompressor is one-shot-per-frame: once a frame ends, subsequent
decompress() calls raise EOFError. This broke HTTP responses where the
server sends multiple zstd frames (common with chunked transfer encoding).

Detect frame boundaries via eof/unused_data attributes and create fresh
decompressor instances for subsequent frames.
if zstd_max_length != ZSTD_MAX_LENGTH_UNLIMITED:
zstd_max_length -= len(result)
if zstd_max_length <= 0:
break
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we're missing an edge case, which probably needs a new test.

If we have unused data here and break, we've ended up throwing it away. Presumably when it continues with further data, it'll fail as the input is corrupted.

@Dreamsorcerer Dreamsorcerer added the backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot label Mar 29, 2026
@Dreamsorcerer
Copy link
Copy Markdown
Member

Other than that subtle edge case, this looks very solid!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ClientPayloadError: 400, message: Can not decode content-encoding: zstd - Already at the end of a Zstandard frame

2 participants