Skip to content

fix: keep the realtime client listening and process SSE events as they arrive - #138

Open
pcbeingused333 wants to merge 1 commit into
Flagsmith:mainfrom
pcbeingused333:fix/realtime-client-stream-reconnect
Open

pcbeingused333 wants to merge 1 commit into
Flagsmith:mainfrom
pcbeingused333:fix/realtime-client-stream-reconnect

Conversation

@pcbeingused333

Copy link
Copy Markdown

With enable_realtime_updates: true the environment is never refreshed after startup: environment_data_polling_manager skips polling in realtime mode, and the realtime listener does not actually listen.

Root cause

Two problems in Flagsmith::RealtimeClient#listen:

  1. @running = false is executed by the calling thread right after Thread.new returns, so the while @running loop in the background thread exits before its first iteration (or after a single connection). There is no reconnection.
  2. The response is read with Faraday.new(...).get { ... }.body.each_line. get blocks 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.new to yield synchronously and returns an already complete body.

Fix

  • @running is only reset when the loop finishes, in an ensure inside the thread, so running stays true while the listener is alive and the loop keeps reconnecting.
  • The stream is read with req.options.on_data, buffering chunks and handling each complete line as it arrives. Line parsing is unchanged, just moved to a private process_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, with listen(main, retry_interval: 0.1) and a 6 s wait:

  • before: running is false right after listen, the server receives 0 connections, update_environment is never called.
  • after: running is true, each event triggers update_environment when 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.rb now uses a fake connection that delivers chunks through on_data while the request is open. New examples: events split across chunks, a real background thread that stays running and reconnects until remaining_attempts is 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.

…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.
@pcbeingused333
pcbeingused333 requested a review from a team as a code owner November 28, 2026 12:17
@pcbeingused333
pcbeingused333 requested review from khvn26 and removed request for a team November 28, 2026 12:17

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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.

1 participant