python system_error ai_generated true

OSError: [Errno 24] Too many open files

ID: python/aiohttp-too-many-open-files

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-09-11First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8+ active
3.9+ active
3.10+ active
3.11+ active

Root Cause

aiohttp ClientSession leaked connections or was created per request without closing, exhausting the process file descriptor limit.

generic

中文

aiohttp ClientSession 泄漏连接,或每个请求都创建且未关闭,耗尽了进程文件描述符上限。

Workarounds

  1. 95% success
    connector = aiohttp.TCPConnector(limit=100, limit_per_host=20)
    async with aiohttp.ClientSession(connector=connector) as session:
        await run_all(session)
  2. 93% success
    async with session.get(url) as resp:
        data = await resp.read()  # response released on exit

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Delays the failure but the leak continues; process eventually hits the new limit.

  2. 85% fail

    Sessions hold OS resources until explicitly closed; GC does not close sockets.