Skip to content

fix: handle stream closed error with reconnection#64

Merged
ntalfer merged 1 commit intomainfrom
fix/handle-stream-closed-error
Jan 16, 2026
Merged

fix: handle stream closed error with reconnection#64
ntalfer merged 1 commit intomainfrom
fix/handle-stream-closed-error

Conversation

@ntalfer
Copy link
Contributor

@ntalfer ntalfer commented Jan 16, 2026

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:

09:37:30.358 [error] user_c2f88c99-7612-419c-a573-b845b1471862 / 5009 ms / 0 < 192 / 0:  Unexpected message {:error, {:stream_error, :closed}}

This error occurred when:

  1. The SSE server timeout (default 5 minutes) was shorter than the client timeout
  2. The server closed the stream after its timeout
  3. The error fell through to the catch-all "Unexpected message" handler
  4. The connection failed and error metrics were incremented

Solution

Add a specific error handling clause for {:error, {:stream_error, :closed}} that:

  • Calls the reconnect/3 function when the stream is closed
  • Respects the SSE_AUTO_RECONNECT configuration
  • Allows seamless connection resumption with Last-Event-ID support
  • Prevents false error metrics for expected reconnections

Changes

  • Added pattern match for {:error, {:stream_error, :closed}} in wait_for_messages/2
  • Routes to existing reconnect/3 function with appropriate reason message

Test Plan

  • With SSE_AUTO_RECONNECT=true, connections now gracefully reconnect on stream closure
  • Error metrics (users with status error) are no longer incorrectly incremented
  • Reconnection metrics properly track expected reconnections

🤖 Generated with Claude Code

@ntalfer ntalfer requested review from a team, achouippe, bpaquet, gcxRun and papey as code owners January 16, 2026 14:47
@ntalfer ntalfer marked this pull request as draft January 16, 2026 14:49
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>
@ntalfer ntalfer force-pushed the fix/handle-stream-closed-error branch from 8fe6df7 to 1ee4e33 Compare January 16, 2026 14:54
@ntalfer ntalfer marked this pull request as ready for review January 16, 2026 14:55
papey
papey approved these changes Jan 16, 2026
@ntalfer ntalfer merged commit 14645b6 into main Jan 16, 2026
5 checks passed
@ntalfer ntalfer deleted the fix/handle-stream-closed-error branch January 16, 2026 15:30
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