Skip to content

fix: Reconnect a BackgroundEventSource after interrupt() instead of shutting it down - #123

Open
bradbunce wants to merge 1 commit into
launchdarkly:mainfrom
launchdarkly-labs:fix/background-reconnect-after-interrupt
Open

bradbunce wants to merge 1 commit into
launchdarkly:mainfrom
launchdarkly-labs:fix/background-reconnect-after-interrupt

Conversation

@bradbunce

@bradbunce bradbunce commented Nov 20, 2026 •

Copy link
Copy Markdown

What

BackgroundEventSource now reconnects when its EventSource is interrupted, instead of stopping for good.

Why

Calling getEventSource().interrupt() is the documented way to drop a connection and reconnect. It's the 4.x equivalent of 3.x's restart(), and plain EventSource iterators already reconnect after it.

BackgroundEventSource, though, treated the resulting StreamClosedByCallerException as a signal to quit. So interrupting a background stream ended it silently: no further events, and no error to the handler.

We hit this in a new LaunchDarkly client-side Java SDK. The SDK needs to reconnect after a malformed payload, as the streaming spec requires, and had to stop using BackgroundEventSource to get that behavior.

Changes

  • BackgroundEventSource:
    • On StreamClosedByCallerException, it quits only if the BackgroundEventSource or its EventSource has been closed.
    • Otherwise it dispatches the fault, so the handler gets onClosed() but not onError(). Then it keeps reading, which starts the stream again.
    • The class Javadoc now explains how to force a reconnect, and that close() is how to shut down.
  • EventSource:
    • When a read fails, the state is set to CLOSED only if it isn't already SHUTDOWN.
    • Before this change, a close() from another thread while one thread was reading could leave getState() reporting CLOSED after shutdown.
    • The background loop relies on seeing SHUTDOWN to know it must quit.
  • Behavior change: calling stop() on a BackgroundEventSource's underlying EventSource now also reconnects. This matches EventSource's documentation that the next read starts the stream again, and BackgroundEventSource always reads again. Use BackgroundEventSource.close() to shut down.
  • Tests:
    • BackgroundEventSourceBasicTest: interrupt() reconnects, stop() reconnects, and closing the EventSource still shuts down. The first two failed before this change.
    • EventSourceReadingTest: the state stays SHUTDOWN when the source is closed while another thread is reading. It failed before the EventSource change.
    • All 237 tests pass on JDK 17. CI covers Java 8 and 11.

Note

Overview
BackgroundEventSource no longer treats StreamClosedByCallerException as a permanent stop. When the underlying EventSource is interrupted or stopped (but not closed), it notifies the handler via onClosed() without onError(), then keeps reading so the stream reconnects. Shutdown still happens only when BackgroundEventSource or EventSource is closed (ReadyState.SHUTDOWN). Javadoc now documents reconnect vs shutdown.

EventSource preserves SHUTDOWN when a read fails after close() from another thread, instead of overwriting it with CLOSED—so background loops can tell a real shutdown from a reconnectable close.

Tests cover interrupt/stop reconnect behavior, close still shuts down, and shutdown state during concurrent close+read.

Reviewed by Cursor Bugbot for commit c17f901. Bugbot is set up for automated code reviews on this repo. Configure here.

…hutting it down

BackgroundEventSource treated the StreamClosedByCallerException from
EventSource.interrupt() or stop() as a reason to quit, so interrupting a
background stream to force a reconnect ended it for good. It now tells the
handler the connection closed (onClosed, not onError) and keeps reading,
which reconnects. It quits only when it or its EventSource has been closed.

EventSource no longer overwrites SHUTDOWN with CLOSED when a read fails
because close() was called from another thread, so getState() stays
accurate and the background loop can tell a close from an interrupt.
@bradbunce bradbunce closed this Nov 20, 2026
@bradbunce bradbunce reopened this Nov 20, 2026
@bradbunce bradbunce closed this Nov 20, 2026
@bradbunce bradbunce reopened this Nov 20, 2026
@bradbunce bradbunce closed this Nov 20, 2026
@bradbunce bradbunce reopened this Nov 20, 2026
@bradbunce
bradbunce marked this pull request as ready for review November 20, 2026 13:49
@bradbunce
bradbunce requested a review from a team as a code owner November 20, 2026 13:49
@tanderson-ld

Copy link
Copy Markdown
Contributor

@bradbunce , will take a look once bandwidth allows. Currently prioritizing other issues going into the holiday break.

This branch has not been deployed

No deployments
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