Skip to content

Chewy.close_client closes Elasticsearch connections#1036

Merged
AlfonsoUceda merged 1 commit into
masterfrom
close-es-connections
Jun 8, 2026
Merged

Chewy.close_client closes Elasticsearch connections#1036
AlfonsoUceda merged 1 commit into
masterfrom
close-es-connections

Conversation

@AlfonsoUceda

@AlfonsoUceda AlfonsoUceda commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Chewy.client is 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 by respond_to? so it's adapter-agnostic) and drops the thread-local so the next Chewy.client rebuilds fresh. Users wire it into a Sidekiq death handler to release connections immediately instead of waiting for GC. Note the default net_http adapter 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:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the changelog if the new code introduces user-observable changes. See changelog entry format for details.

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.
@AlfonsoUceda AlfonsoUceda force-pushed the close-es-connections branch from 3078273 to 32db330 Compare June 8, 2026 07:27
@AlfonsoUceda AlfonsoUceda merged commit e3f46f0 into master Jun 8, 2026
13 checks passed
@AlfonsoUceda AlfonsoUceda deleted the close-es-connections branch June 8, 2026 09:43
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