Repository navigation
fix: keep the realtime client listening and process SSE events as they arrive - #138
Open
pcbeingused333 wants to merge 1 commit into
Open
pcbeingused333 wants to merge 1 commit into
pcbeingused333 wants to merge 1 commit into
Conversation
…y arrive listen reset @running to false as soon as the thread was spawned, so the loop exited without connecting (or after a single connection), and the response body was only read once the server closed the stream.
There was a problem hiding this comment.
No issues found across 2 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Requires human review: Auto-approval skipped because this PR is from an external contributor.
Re-trigger cubic
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.
With
enable_realtime_updates: truethe environment is never refreshed after startup:environment_data_polling_managerskips polling in realtime mode, and the realtime listener does not actually listen.Root cause
Two problems in
Flagsmith::RealtimeClient#listen:@running = falseis executed by the calling thread right afterThread.newreturns, so thewhile @runningloop in the background thread exits before its first iteration (or after a single connection). There is no reconnection.Faraday.new(...).get { ... }.body.each_line.getblocks until the server closes the connection and buffers the whole body, so events are only handled once the stream ends rather than when they are sent.The existing spec did not catch this because it stubs
Thread.newto yield synchronously and returns an already complete body.Fix
@runningis only reset when the loop finishes, in anensureinside the thread, sorunningstaystruewhile the listener is alive and the loop keeps reconnecting.req.options.on_data, buffering chunks and handling each complete line as it arrives. Line parsing is unchanged, just moved to a privateprocess_line.Reproduction
A local TCP server that sends two
data: {"updated_at": N}events with chunked encoding, keeps the connection open for 1.5 s and then closes it, withlisten(main, retry_interval: 0.1)and a 6 s wait:runningisfalseright afterlisten, the server receives 0 connections,update_environmentis never called.runningistrue, each event triggersupdate_environmentwhen it is sent (0.1 s, 0.6 s), and the client reconnects after every close (3 connections in 6 s).Test
spec/sdk/realtime_client_spec.rbnow uses a fake connection that delivers chunks throughon_datawhile the request is open. New examples: events split across chunks, a real background thread that staysrunningand reconnects untilremaining_attemptsis exhausted, and updates applied while the stream is still open. 4 examples fail before the change and all pass after (bundle exec rspec spec/sdk,bundle exec rubocop lib).This change was prepared with the assistance of an AI coding agent.