Repository navigation
fix: close replaced HTTPS adapter on TLS cipher rotation - #20
Conversation
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.
|
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 |
|
Shipped in v1.5.2, available as |
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.