Chewy.close_client closes Elasticsearch connections#1036
Merged
Conversation
c9115e9 to
3078273
Compare
The per-thread Chewy.client keeps its connections open until the GC reclaims the dead thread's client, leaking file descriptors in long-lived multi-threaded processes (e.g. Sidekiq, which churns threads). Neither elasticsearch-ruby nor elastic-transport expose a way to close connections, so close them via Faraday and drop the thread-local. Refs #951.
3078273 to
32db330
Compare
dgsuarez
approved these changes
Jun 8, 2026
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.
Chewy.clientis memoized per thread, so every thread that touches Chewy builds its own client holding its own connections to Elasticsearch. Neither elasticsearch-ruby nor elastic-transport expose a way to close those connections, so they only get released when the GC reclaims the dead thread's client. In long-lived multi-threaded processes that churn threads — Sidekiq replaces a thread on job failure — dead-but-not-yet-collected clients pile up and leak file descriptors. See #951.This adds
Chewy.close_client, which closes the current thread's connections (via Faraday, guarded byrespond_to?so it's adapter-agnostic) and drops the thread-local so the nextChewy.clientrebuilds fresh. Users wire it into a Sidekiq death handler to release connections immediately instead of waiting for GC. Note the defaultnet_httpadapter opens/closes per request and doesn't leak; this matters for persistent adapters (httpclient, typhoeus, patron).Testing
bundle exec rspec spec/chewy_spec.rb -e '.close_client' spec/chewy/elastic_client_spec.rb -e '#close'— all green. Specs use doubles, no live cluster needed.Before submitting the PR make sure the following are checked:
[Fix #issue-number](if the related issue exists).master(if not - rebase it).