Interview Prep/Python for DevOps/Runtime, file, subprocess & encoding errors

`OSError: [Errno 24] Too many open files` in a long-running Python service.

IntermediateTroubleshootingRuntime, file, subprocess & encoding errors
Answer

Likely causes → File or socket leaks (files opened without with, a new requests.Session or boto3 client per request never closed), or the limit is genuinely too low for the concurrency.

Diagnose →

ls /proc/<pid>/fd | wc -l; cat /proc/<pid>/limits | grep "open files"
lsof -p <pid> | awk '{print $5}' | sort | uniq -c | sort -rn | head

Fix → Use with for files; create one Session/client and reuse it; close responses (with s.get(..., stream=True) as r:). Raise LimitNOFILE in systemd or ulimit -n only if usage is legitimate.

Prevent → Track process_open_fds from prometheus_client default metrics and alert on growth.

All Python for DevOps questions Roadmap: Python for automation

More on Runtime, file, subprocess & encoding errors