Fix EventStream async streaming under hackney 4 - #81
Open
rvasyl wants to merge 2 commits into
Open
Conversation
With hackney 4.x the streaming path hung indefinitely: the stream opened
but no chunks were ever delivered. Two incompatibilities, plus two
hardening fixes:
- Force HTTP/1.1 (protocols: [:http1]). hackney 4 negotiates HTTP/2 via
ALPN by default and Bedrock accepts h2, but hackney's h2 path never
delivers async body messages, so the first receive (status) blocked
forever. HTTP/1.1 is the documented protocol for
InvokeModelWithResponseStream and what boto3 uses; it also guarantees
the Transfer-Encoding: chunked header this module verifies.
- Accept pid async refs. hackney 4 correlates async messages by the
connection pid (async_ref :: pid()), not an Erlang reference, so the
`ref when is_reference(ref)` clause raised FunctionClauseError on the
first body read. Match on reference or pid to support both majors.
- Honor caller :http_opts (recv_timeout/connect_timeout/pool) via
hackney_options/1, streaming defaults winning on conflict. Previously
the stream always used hackney's built-in recv_timeout (5s) and
dropped slow responses regardless of the caller's configured timeout.
- Raise on {:error, reason} async messages. hackney 4 emits e.g.
{:error, :closed} on mid-stream disconnects; previously any shape
other than {:closed, :timeout} either blocked the receive forever or
fell into the body-data clause and crashed decode with a badarg.
Guard the data clause with is_binary/1 and raise ExAws.Error with the
real reason instead.
Verified live against eu.anthropic.claude-sonnet-4-5-20250929-v1:0
(eu-west-1) with hackney 4.5.2: full stream decoded, zero bad chunks.
rvasyl
marked this pull request as draft
July 15, 2026 16:43
rvasyl
marked this pull request as ready for review
July 16, 2026 13:06
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.
Supersedes #80.
Problem
With hackney 4.x,
ExAws.Bedrock.stream!/2hangs indefinitely: the connection opens but no chunks are ever delivered. Two incompatibilities inEventStream:receive(waiting on the status line) blocks forever. Bedrock's InvokeModelWithResponseStream is an HTTP/1.1 API (botocore has no HTTP/2 support either), and this module'sTransfer-Encoding: chunkedverification only exists on HTTP/1.1.ref when is_reference(ref)clause in the body loop raisesFunctionClauseErroron the first read.Separately (affects both hackney majors): the streaming path discarded caller
:http_opts, so every stream ran with hackney's built-in 5srecv_timeoutwith no way to override it — slow model responses died mid-stream as{:closed, :timeout}. (The non-streaming path already forwards these viaExAws.Request.)Changes
protocols: [:http1]in the streaming hackney options.is_reference(ref) or is_pid(ref)so both hackney majors work.:http_opts(recv_timeout,connect_timeout,pool) by merging them under the streaming defaults via a testablehackney_options/1— streaming-critical keys (async: :once,protocols) can't be overridden.ExAws.Erroron{:error, reason}async messages and on unexpected message shapes instead of hanging thereceiveor crashing the decoder; guard the body-data clause withis_binary/1.Testing
mix testandmix credo --strictpass.