Repository navigation
fix: Stop the worker thread promptly when the client is closed during a reconnect wait - #93
Merged
Merged
Conversation
jsonbailey
marked this pull request as ready for review
September 25, 2026 20:59
keelerm84
approved these changes
Sep 28, 2026
jsonbailey
pushed a commit
that referenced
this pull request
Sep 28, 2026
🤖 I have created a release *beep* *boop* --- ## [3.0.0](2.6.0...3.0.0) (2026-09-28) ### ⚠ BREAKING CHANGES * Report a server-initiated stream close to the error handler ([#91](#91)) ### Features * Report a server-initiated stream close to the error handler ([#91](#91)) ([7e4dc1b](7e4dc1b)) ### Bug Fixes * Declare logger as a runtime dependency ([#94](#94)) ([f7809c2](f7809c2)) * Stop the worker thread promptly when the client is closed during a reconnect wait ([#93](#93)) ([054b11b](054b11b)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Overview** > **Release 3.0.0** — bumps the package version from `2.6.0` to `3.0.0` in `.release-please-manifest.json`, `lib/ld-eventsource/version.rb`, and documents the release in `CHANGELOG.md`. > > This is a Release Please cut, not new library logic in the diff. The changelog records what ships in **3.0.0**: a **breaking** behavior change where a **server-initiated stream close** is reported through the client **error handler** (e.g. `StreamClosedByServerError`), plus fixes to declare **`logger` as a runtime dependency** and to **stop the worker thread promptly** when the client is closed during a reconnect backoff wait. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit d5431c5. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.
Summary
connectchecks the stop state only beforesleep(interval), andKernel#sleepcannot be interrupted. Whencloseis called during a reconnect wait, the worker thread stays alive until the interval ends (up to 30 seconds). Then it sends one more request, and the connection from that request is not closed.This PR:
Concurrent::Eventthatclosesets, so the wait ends when the client is closed.close.closeruns, before the worker thread returns.AtomicBooleanis removed.This problem was reported externally in ruby-eventsource PR 54. That PR proposed
Thread#kill, which can stop the thread in the middle of a user callback. This PR takes a different approach.Repro
A local TCP server closes the stream, and the client is closed during the reconnect wait:
reconnect_time: 1) and 2.85s (reconnect_time: 3) afterclose, and each run sent 1 request afterclose.close.Testing
New specs in
spec/client_spec.rb:close during a reconnect wait stops the worker thread promptly: withreconnect_time: 5and a server that returns 500, the worker thread ends within 1s ofclose.close during a reconnect wait does not send another request: the server gets only the first request.closes a connection that opens while the client is closed: aquery_paramscallback callsclose, so the request always opens a connection afterclose. A raw TCP server checks that the client closes that connection.Mutation check: when I reverted each fix line by itself, its spec failed and did not hang. The reverted lines were: event wait back to
sleep, no check after the wait, no connection close inrun_stream, and no event set inclose.bundle exec rspec spec: 174 examples, 0 failures (run twice).bundle exec rubocop --parallel: no offenses.Known, not fixed here
With http 6,
@http_clientis a non-persistentHTTP::Session, so@http_client.closedoes nothing. Because of this,closecannot interrupt a request that is still connecting or waiting for response headers. The thread stops when that request returns or times out, and the new check then closes the connection.Jira
SDK-3206
Note
Overview
Fixes a shutdown bug where
closeduring a reconnect backoff could leave theLD/SSEClientworker alive untilsleepfinished (up to ~30s), then issue one more HTTP request and sometimes leave that connection open.Stop signaling is consolidated on a
Concurrent::Event:closesets it, loops useset?, and reconnect backoff uses@stop_event.wait(interval)instead ofKernel#sleep, so the wait ends immediately on close.connectre-checks stop after the wait so no retry request runs once closed.run_streamadds a post-connect check thatreset_httpand returns ifcloseraced after a successful connect (e.g. duringquery_params).New specs cover prompt worker exit, no extra request after close during wait, and closing a connection opened after the client is already closed.
Reviewed by Cursor Bugbot for commit 2433eef. Bugbot is set up for automated code reviews on this repo. Configure here.