I am gettng too many files open error after running for several minutes

Author: keystroke3Created Jun 18, 2025Updated May 5, 2026

I have the following function that creates crates a new CloudScrape instance:

python
def new_requester(proxy_uri: str = "") -> CloudScraper:
    params = dict(
        interpreter="js2py",
        enable_stealth=True,
        stealth_options={
            "min_delay": 2.0,
            "max_delay": 6.0,
            "human_like_delays": True,
            "randomize_headers": True,
            "browser_quirks": True,
        },
        browser="chrome",
    )
    requester = CloudScraper(**params)
    if proxy_uri:
        requester.proxies = {"https": proxy_uri, "http": proxy_uri}
    return requester

The same instance is maintained throughout the requesting thread's life cycle and is used like this:

python
        with requester.get(url) as resp:
            if resp.status_code != 200:
                resp.raise_for_status()
            return resp.content

After running it for some time (about 10-15 min), I get this error with and without the context manager:

python
 HTTPSConnectionPool(host='www.example.com', port=443): Max retries exceeded with url: /foo/bar/4723403 (Caused by ProxyError('Unable to connect to proxy', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fee404c5810>: Failed to establish a new connection: [Errno 24] Too many open files'))

The problem seems to go away when I restart the program. Working behind a vpn without proxies causes 104 ConnectionResetError after some time. With the proxy, majority of the 104 errors are recoverable after a delayed retry, but without a proxy, they are not.

I have another project that uses requests.Session to make requests, and can run for days on end. What am I doing wrong here?