Skip to content

fix: close replaced HTTPS adapter on TLS cipher rotation - #20

Merged
LavX merged 2 commits into
LavX:mainfrom
zdimension:fix/tls-adapter-leak
Oct 26, 2026
Merged

LavX merged 2 commits into
LavX:mainfrom
zdimension:fix/tls-adapter-leak

Conversation

@zdimension

Copy link
Copy Markdown
Contributor

CloudScraper._rotate_tls_cipher_suite() runs before nearly every request and mounts a new CipherSuiteAdapter without closing the previous one. The replaced adapter is never garbage collected: its ssl_context.wrap_socket is a bound method of the adapter, and the cycle goes through the pooled SSLSocket's reference to its SSLContext, which the GC cannot traverse. Its pooled keep-alive connection therefore stays open forever, sitting in CLOSE_WAIT once the server drops it.

That is one leaked socket per request. Over a few days the process reaches its fd limit and uvicorn's accept loop fails endlessly with "OSError: [Errno 24] Too many open files". A hacky workaround is to restart the container daily with a cron but that doesn't fix the underlying issue.

The fix is to close the replaced adapter after mounting the new one.

zdimension and others added 2 commits October 26, 2026 13:00
CloudScraper._rotate_tls_cipher_suite() runs before nearly every
request and mounts a new CipherSuiteAdapter without closing the
previous one. The replaced adapter is never garbage collected: its
ssl_context.wrap_socket is a bound method of the adapter, and the cycle
goes through the pooled SSLSocket's reference to its SSLContext, which
the GC cannot traverse. Its pooled keep-alive connection therefore stays
open forever, sitting in CLOSE_WAIT once the server drops it.

That is one leaked socket per request. Over a few days the process
reaches its fd limit and uvicorn's accept loop fails endlessly with
"OSError: [Errno 24] Too many open files".

Close the replaced adapter after mounting the new one.
…dscraper

CI installs vendor/cloudscraper-3.0.0.zip, so a plain import would test the
zip rather than the vendored copy that main.py runs. Load vendor/cloudscraper
explicitly and check that rotation closes the replaced adapter's pools and
leaves the new adapter usable.
@LavX

LavX commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Thanks a lot for tracking this one down, the CLOSE_WAIT diagnosis is spot on. I reproduced it locally against a keep-alive HTTPS server: 50 requests through the vendored cloudscraper left 50 client sockets open on main, and 1 (the live adapter's pooled connection) with your change. I also checked that a response still streaming from the old adapter finishes cleanly when the adapter is closed mid-read, and its socket is released afterwards.

One thing I added on your branch (d1602ac): a small test in tests/test_vendored_cloudscraper.py. CI installs vendor/cloudscraper-3.0.0.zip, so a plain import cloudscraper in tests never reaches the vendored copy that main.py actually runs. The test loads vendor/cloudscraper explicitly and checks that rotation clears the replaced adapter's pools. It fails without your fix and passes with it. CI is green on the new head, merging now.

@LavX
LavX merged commit 081fb3f into LavX:main Oct 26, 2026
2 checks passed
@LavX

LavX commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Shipped in v1.5.2, available as ghcr.io/lavx/opensubtitles-scraper:1.5.2 (also latest). Thanks again, this should let you drop that daily restart cron. If you still see sockets piling up after upgrading, please open an issue and we'll dig in together.

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