Skip to content

feat: Add thread reader to client #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Exterm1nate
Copy link
Contributor

When stopping the client its thread continues working for some time. This is not a problem in production, but in tests it is. Webmock removes its stub after the test, while the thread is still working, so we sometimes receive such errors:

#<Thread:0x0000761ef6935090@LD/SSEClient /usr/local/bundle/ruby/3.1.0/gems/ld-eventsource-2.2.3/lib/ld-eventsource/client.rb:144 run> terminated with exception (report_on_exception is true):
/usr/local/bundle/ruby/3.1.0/gems/webmock-3.25.1/lib/webmock/http_lib_adapters/http_rb/webmock.rb:63:in `halt': Real HTTP connections are disabled. Unregistered request: GET http://localhost:3003/admin/v1/emails with headers {'Accept'=>'text/event-stream', 'Authorization'=>'Bearer test_key', 'Cache-Control'=>'no-cache', 'Connection'=>'close', 'Host'=>'localhost:3003', 'Last-Event-Id'=>'0', 'User-Agent'=>'ruby-eventsource'} (WebMock::NetConnectNotAllowedError)

This PR adds a reader for thread, so it can be killed after the test example.

subject(:call_method) do
  processor.start
  sleep 0.1
end

after do
  processor.connection.thread&.kill
end

@Exterm1nate Exterm1nate requested a review from a team as a code owner April 18, 2025 08:01
@keelerm84
Copy link
Member

@Exterm1nate thank you for raising the issue and working on this contribution.

I would prefer to not expose the thread as that's an implementation detail, and it enables a lot of misuse. I've updated the PR to add a close_and_wait method. This will block until that thread has shutdown, or some timeout period has elapsed.

Would this work for you?

@Exterm1nate
Copy link
Contributor Author

This solution appears to work, but it has a noticeable disadvantage: tests will take longer if we wait for the last run_stream cycle to complete. It would be better if we could terminate the thread immediately without waiting.

@keelerm84
Copy link
Member

tests will take longer if we wait for the last run_stream cycle to complete.

I wouldn't expect this to take long at all. That method should early exit when it sees we have called close. This makes me think something else is preventing the thread from exiting immediately, like a hung IO reader.

Do you have a small reproduction case I could reference to dig into this further?

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