Why does my systemd service fail with Too many open files when ulimit is high?
Every open file and socket counts against a process's open-file limit, and going over it fails with Errno 24, Too many open files. A service started by systemd gets its limit from LimitNOFILE in its unit or drop-ins, not from your shell, so ulimit -n in a terminal can be high while the service is capped. systemctl show -p LimitNOFILE and /proc/<pid>/limits show the real value. In this incident a hardening drop-in set the limit to 256 while a report opens 300 files.
What it looks like
- Errors with Errno 24 or Too many open files.
- The same code works when run by hand.
- systemctl show reports a low LimitNOFILE.
- A unit or drop-in changed shortly before.
Commands that find the cause
journalctl -u orders-api -n 6 --no-pagerThe service's log, newest last. Each failed report is one line with the operating system's error and its number.
systemctl show orders-api -p LimitNOFILEThe open-file limit systemd gives the service. Every file and socket counts against it, and the service cannot raise it from inside.
grep 'open files' /proc/<pid>/limitsThe open-file limit the kernel enforces on the running process, soft and hard. This is what the service really has, whatever any config file says.
ulimit -nThe open-file limit of your own shell. A service started by systemd does not inherit it, so a high number here says nothing about the service.
ls /opt/orders/data | wc -lHow many part files the report reads. The report opens them all at once.
What the evidence looked like
Recorded on a real system when this incident was captured for the game.
$ journalctl -u orders-api -n 6 --no-pager
Nov 29 20:29:29 ops-1 python3[4876]: ERROR /report: [Errno 24] Too many open files: '/opt/orders/data/part-251.csv'
Nov 29 20:29:29 ops-1 python3[4876]: ERROR /report: [Errno 24] Too many open files: '/opt/orders/data/part-251.csv'
Nov 29 20:29:33 ops-1 python3[4876]: ERROR /report: [Errno 24] Too many open files: '/opt/orders/data/part-251.csv'
Nov 29 20:29:33 ops-1 python3[4876]: ERROR /report: [Errno 24] Too many open files: '/opt/orders/data/part-251.csv'
Nov 29 20:29:33 ops-1 python3[4876]: ERROR /report: [Errno 24] Too many open files: '/opt/orders/data/part-251.csv'
Nov 29 20:29:33 ops-1 python3[4876]: ERROR /report: [Errno 24] Too many open files: '/opt/orders/data/part-251.csv'Root cause
A hardening drop-in set LimitNOFILE=256 while a report opens 300 files; raising it to 4096 fixed reports.
The fix
Raise the open-file limit in the hardening drop-in and restart the service:
sudo sed -i 's/LimitNOFILE=256/LimitNOFILE=4096/' /etc/systemd/system/orders-api.service.d/hardening.conf && sudo systemctl daemon-reload && sudo systemctl restart orders-apiHow to prevent it
Set LimitNOFILE from what the service really opens at peak, keep hardening drop-ins under review with the service owners, and alert on open file counts.
More incident guides
- Linux disk full: no space left on device
- systemd service keeps restarting after a deploy
- Address already in use: find what holds a port on Linux
- Linux name or service not known after a config change
- CrashLoopBackOff after a config change
- Service returns 503 while every pod is Running
- OOMKilled and exit code 137
- Pods fail to start after a Secret change
- Requests fail after adding a NetworkPolicy
- Healthy pods restarted by a liveness probe
- Slow service with low CPU and no restarts
- Rollout stuck on ImagePullBackOff
- Pods stuck Pending with insufficient CPU
- Docker container exits immediately after a config change
- Docker port mapping not working, empty reply
- Docker container cannot connect to the database host
- Docker volume permission denied
- Docker container exit code 137, killed for memory