`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.
More on Runtime, file, subprocess & encoding errors
`KeyError: 'Tags'` while processing `describe_instances` output.Beginner`TypeError: can't compare offset-naive and offset-aware datetimes` in a snapshot-age check.Beginner`UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte` reading a log file.Intermediate`UnicodeEncodeError: 'ascii' codec can't encode character '✓'` when printing in a container.Intermediate`PermissionError: [Errno 13] Permission denied: '/var/log/app/report.json'`Intermediate`subprocess.CalledProcessError: Command '['terraform', 'plan']' returned non-zero exit status 1.` with no further detail.Beginner