python os_error ai_generated true

OSError: [Errno 24] Too many open files

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

Also available as: JSON · Markdown
85%Fix Rate
88%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
311 active

Root Cause

Process exceeded file descriptor limit. Common in servers and data pipelines.

generic

Workarounds

  1. 95% success Use context managers (with statement) to ensure files are closed
    with open(path) as f: data = f.read()

    Sources: https://docs.python.org/3/reference/compound_stmts.html#the-with-statement

  2. 85% success Find leaked file descriptors with lsof or /proc/self/fd
    ls -la /proc/self/fd | wc -l

    Sources: https://docs.python.org/3/library/os.html#os.listdir

Dead Ends

Common approaches that don't work:

  1. Increase ulimit to very high value 60% fail

    Band-aid — the real issue is file descriptor leak

  2. Ignore the error and retry 80% fail

    Makes the leak worse over time

Error Chain

Frequently confused with: