DeflateBuffer: skip header sniff on empty chunks#12995
Draft
HrachShah wants to merge 2 commits into
Draft
Conversation
The deflate decoder FSM in feed_data() inspected chunk[0] to detect a raw deflate stream (no zlib header) before any data had been seen. AIOHTTP pauses and resumes decoders by calling feed_data(b""), and on the first such resume the empty chunk made chunk[0] raise IndexError, propagating out through the protocol's resume path. Guard the sniff with an explicit empty-chunk check so feed_data(b"") is a no-op before the decoder has started, while keeping the existing behaviour for real first-byte data. Add two regression tests in TestDeflateBuffer covering the empty-chunk-before-data and empty-chunk-after-data cases.
for more information, see https://pre-commit.ci
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #12995 +/- ##
=======================================
Coverage 98.95% 98.96%
=======================================
Files 131 131
Lines 48029 48103 +74
Branches 2495 2496 +1
=======================================
+ Hits 47529 47603 +74
Misses 376 376
Partials 124 124
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
Merging this PR will not alter performance
Comparing Footnotes
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What do these changes do?
DeflateBuffer.feed_dataraisedIndexError: string index out of rangefrom the RFC1950 header sniff whenever the underlying transport resumed a paused decoder withfeed_data(b""). The sniff is only meaningful when the first byte of the chunk actually arrived, so an empty chunk must be a no-op.The guard now reads
if (chunk and not self._started_decoding and self.encoding == "deflate" and chunk[0] & 0xF != 8):instead ofif (not self._started_decoding and self.encoding == "deflate" and chunk[0] & 0xF != 8):. Withchunkshort-circuiting toFalseonb"", the branch is skipped beforechunk[0]is read. The downstreamdecompress_sync(b"")already handles the empty payload correctly (the new testtest_feed_empty_chunk_after_real_datacovers this).Are there changes in behavior for the user?
Yes — bugfix. A response that previously crashed with
IndexErrornow decodes normally. No public API change.Is it a substantial burden for the maintainers to support this?
No. The change is local to a single private class, has two new regression tests, and a CHANGES fragment. No new public API, no new dependencies, no new defaults.
Related issue number
Fixes #12994
Checklist
CONTRIBUTORS.txtCHANGES/folderTest log
test_feed_empty_chunk_before_any_datafails on master withIndexError: index out of rangefromaiohttp/http_parser.py:1142and passes with this fix.test_feed_empty_chunk_after_real_datapasses both ways (regression-of-regression: the pre-existingdecompress_sync(b"")path is left alone).Drafted with Zo Computer (MiniMax M3); reviewed by HrachShah.