fix: handle stream closed error with reconnection#64
Merged
Conversation
Add error handling clause for {:error, {:stream_error, :closed}}
to properly reconnect when the server closes the SSE stream.
Previously, this error was falling through to the catch-all
"Unexpected message" handler, causing the connection to fail
and increment error metrics. Now, when auto-reconnect is enabled,
the client will gracefully reconnect on stream closure.
This fix addresses the errors observed during load testing:
"Unexpected message {:error, {:stream_error, :closed}}"
The reconnection behavior respects the SSE_AUTO_RECONNECT
configuration, allowing connections to resume seamlessly
after server-side timeouts or connection resets.
Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
8fe6df7 to
1ee4e33
Compare
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.
Summary
Adds proper error handling for
{:error, {:stream_error, :closed}}to gracefully reconnect when the SSE server closes the stream.Problem
During load testing, we observed errors like:
This error occurred when:
Solution
Add a specific error handling clause for
{:error, {:stream_error, :closed}}that:reconnect/3function when the stream is closedSSE_AUTO_RECONNECTconfigurationLast-Event-IDsupportChanges
{:error, {:stream_error, :closed}}inwait_for_messages/2reconnect/3function with appropriate reason messageTest Plan
SSE_AUTO_RECONNECT=true, connections now gracefully reconnect on stream closureuserswith statuserror) are no longer incorrectly incremented🤖 Generated with Claude Code