Skip to content

Buffer event stream messages split across hackney responses - #98

Merged
devstopfix merged 7 commits into
devstopfix:mainfrom
holsee:fix/event-stream-buffer-split-frames
Nov 26, 2026
Merged

devstopfix merged 7 commits into
devstopfix:mainfrom
holsee:fix/event-stream-buffer-split-frames

Conversation

@rvasyl

@rvasyl rvasyl commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

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. EventStream decoded each binary on its own, so a split message came out as {:incomplete_chunk, _} / {:bad_chunk, _, _} and its content was lost.

Change

  • The response is now decoded with a stateful stream that buffers bytes until the length declared in the prelude has arrived, then emits the message and keeps the remainder for the next binary.
  • Bytes left over when the stream ends are reported as before: {:incomplete_chunk, _} or {:bad_chunk, _, :invalid_chunk}.
  • A message declaring more than 128 KB of headers or 24 MB of payload is rejected up front as a :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/1 is 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 --strict and mix dialyzer pass.

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 d0484cb was run through the same streams for comparison.

Checks. mix test (59 passed, 13 :aws excluded), compile --warnings-as-errors, format, credo --strict and dialyzer pass 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.

Streams as expected hackney 4.9.1 hackney 4.12.4
This PR 6,898 / 6,898 6,898 / 6,898
Base 4,167 / 6,898 841 / 6,898
  • hackney 4.12.4 delivers an HTTP chunk in pieces, so even one message per chunk arrives split: the base code decoded 5 of 300 such streams.
  • A bad message checksum or an exception event costs only that message. A bad prelude is reported and later messages resume once a binary starts on a message boundary. A dropped connection raises ExAws.Error.

Live streams from Bedrock. eu.amazon.nova-micro-v1:0, output checked against an independent decode of the raw bytes.

Streams Messages Decoded exactly
This PR, hackney 4.9.1 300 124,442 300
This PR, hackney 4.12.4 300 123,938 300
Base, hackney 4.9.1 100 40,586 100
Base, hackney 4.12.4 100 40,940 100
  • About 20% of binaries held several messages (up to 30). No corruption in 329,906 messages.
  • No message was split live: Nova messages are 164-531 bytes and arrive whole, so the base code passed too. The split path is validated by the local runs only.

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
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.
Comment thread lib/ex_aws/bedrock/event_stream.ex Resolved
Comment thread lib/ex_aws/bedrock/event_stream.ex Resolved
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
rvasyl marked this pull request as ready for review November 26, 2026 09:46
@rvasyl

rvasyl commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

@devstopfix ready for review

@devstopfix
devstopfix merged commit 3d2edf4 into devstopfix:main Nov 26, 2026
8 checks passed
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.

2 participants