Repository navigation
Buffer event stream messages split across hackney responses - #98
Merged
devstopfix merged 7 commits intoNov 26, 2026
Merged
Conversation
hackney >= 4.10 can deliver a single HTTP chunk as several async messages, so an event stream message may arrive in pieces. Each piece was decoded on its own and surfaced as a bad or incomplete chunk, losing the message. Decode the response with a stateful stream that buffers bytes until the length declared in the prelude has arrived, and raise if the stream ends in the middle of a message.
rvasyl
marked this pull request as draft
November 19, 2026 13:16
A prelude declaring more than 128 KB of headers or 24 MB of payload is rejected as soon as it arrives, so the decoder never buffers more than one message. These are the limits botocore enforces.
rvasyl
commented
Nov 25, 2026
rvasyl
commented
Nov 25, 2026
Keep the existing contract: bytes left over when the stream ends are emitted as a bad or incomplete chunk, as before, and no longer raise.
It was only used by tests, which now go through decode_stream/1.
rvasyl
marked this pull request as ready for review
November 26, 2026 09:46
Collaborator
Author
|
@devstopfix ready for review |
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.
Problem
From hackney 4.10 a single HTTP chunk can be delivered as several async
{:hackney_response, ref, binary}messages, so one event stream message may arrive in pieces.EventStreamdecoded each binary on its own, so a split message came out as{:incomplete_chunk, _}/{:bad_chunk, _, _}and its content was lost.Change
{:incomplete_chunk, _}or{:bad_chunk, _, :invalid_chunk}.:bad_chunk(:headers_too_large/:payload_too_large), so no more than one message is ever buffered. These are the same limits that botocore, the core of Amazon's Python SDK, enforces on every event stream (eventstream.py).decode_chunk/1is removed; it was only used by tests.Testing
New tests split valid messages at every byte offset (and one byte at a time) and assert the output matches the unsplit decode.
mix test,mix format --check-formatted,mix credo --strictandmix dialyzerpass.Validation
Run locally on Elixir 1.18.1 / OTP 27 with hackney 4.9.1 (locked, before chunks were split) and 4.12.4 (latest). The base commit
d0484cbwas run through the same streams for comparison.Checks.
mix test(59 passed, 13:awsexcluded),compile --warnings-as-errors,format,credo --strictanddialyzerpass on both hackney versions. Unit tests hit every executable line of the new decoder.Old and new decoder side by side. Identical output for whole, valid messages (5,000 random runs). Output differs only on malformed input, as discussed in the review threads.
Local streams through real hackney. A local HTTP server sent event streams in many layouts (one message per chunk, several per chunk, split across chunks, fragmented TCP writes, corrupted, truncated, up to 12 MB per message), consumed with
ExAws.stream!/2.ExAws.Error.Live streams from Bedrock.
eu.amazon.nova-micro-v1:0, output checked against an independent decode of the raw bytes.