Repository navigation
Conversation
…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.
Contributor
|
@bradbunce , will take a look once bandwidth allows. Currently prioritizing other issues going into the holiday break. |
This branch has not been deployed
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.
What
BackgroundEventSourcenow reconnects when itsEventSourceis 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'srestart(), and plainEventSourceiterators already reconnect after it.BackgroundEventSource, though, treated the resultingStreamClosedByCallerExceptionas 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
BackgroundEventSourceto get that behavior.Changes
BackgroundEventSource:StreamClosedByCallerException, it quits only if theBackgroundEventSourceor itsEventSourcehas been closed.onClosed()but notonError(). Then it keeps reading, which starts the stream again.close()is how to shut down.EventSource:CLOSEDonly if it isn't alreadySHUTDOWN.close()from another thread while one thread was reading could leavegetState()reportingCLOSEDafter shutdown.SHUTDOWNto know it must quit.stop()on aBackgroundEventSource's underlyingEventSourcenow also reconnects. This matchesEventSource's documentation that the next read starts the stream again, andBackgroundEventSourcealways reads again. UseBackgroundEventSource.close()to shut down.BackgroundEventSourceBasicTest:interrupt()reconnects,stop()reconnects, and closing theEventSourcestill shuts down. The first two failed before this change.EventSourceReadingTest: the state staysSHUTDOWNwhen the source is closed while another thread is reading. It failed before theEventSourcechange.Note
Overview
BackgroundEventSource no longer treats
StreamClosedByCallerExceptionas a permanent stop. When the underlyingEventSourceis interrupted or stopped (but not closed), it notifies the handler viaonClosed()withoutonError(), then keeps reading so the stream reconnects. Shutdown still happens only whenBackgroundEventSourceorEventSourceis closed (ReadyState.SHUTDOWN). Javadoc now documents reconnect vs shutdown.EventSource preserves
SHUTDOWNwhen a read fails afterclose()from another thread, instead of overwriting it withCLOSED—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.