Short answer: the process used up its file descriptor limit
Too many open files is the kernel error EMFILE (errno 24): one process already holds as many fds as its own limit (RLIMIT_NOFILE) allows. Sockets are fds too, so under load new connections fail first. The cause is usually one of two:
- The limit is too low — a systemd service or container runs with the default of 1024
- fds are leaking — unclosed files or sockets keep growing over time
Hitting the system-wide limit (fs.file-max) gives a different message: Too many open files in system (ENFILE).
Same problem, different message per runtime
- Go:
accept tcp [::]:8080: accept4: too many open files - nginx:
accept4() failed (24: Too many open files),socket() failed (24: Too many open files) - Java:
java.net.SocketException: Too many open files - Node.js:
Error: EMFILE: too many open files, open '/app/data/report.json'
Diagnosis: check the process's limit, not your shell's
ulimit -n in your shell shows your shell's limit only. The service runs with whatever systemd or the container runtime gave it.
PID=$(systemctl show -p MainPID --value api)
grep "Max open files" /proc/$PID/limits
ls /proc/$PID/fd | wc -l
Max open files 1024 524288 files
1021
- Open fds sit right at the soft limit (1024) → it really is hitting the limit
- After raising the limit, the count keeps climbing over time → a leak (cause 2)
Cause 1: the systemd service limit — LimitNOFILE
/etc/security/limits.conf applies to login sessions (PAM) only, not to systemd services. Raise it in the unit.
# /etc/systemd/system/api.service.d/override.conf (the same file systemctl edit api creates)
[Service]
LimitNOFILE=65536
systemctl daemon-reload
systemctl restart api
grep "Max open files" /proc/$(systemctl show -p MainPID --value api)/limits
The new limit applies only after a restart. For nginx, raise the worker limit with worker_rlimit_nofile and keep worker_connections within it.
In containers: Docker and Kubernetes
docker run --ulimit nofile=65536:65536 api:1.4.2
docker exec api sh -c 'ulimit -n'
The Kubernetes pod spec has no ulimit field. Containers inherit the defaults of the node's container runtime (containerd, CRI-O), so check the real value with cat /proc/1/limits inside the pod and change it in the node's runtime configuration.
Cause 2: an fd leak
If the same error returns hours or days after raising the limit, it is a leak. See what is piling up.
lsof -p $PID | awk '{print $5}' | sort | uniq -c | sort -rn | head
ss -tanp state close-wait | grep -c "pid=$PID"
Growing REG entries mean unclosed files. Growing IPv4, IPv6 or sock entries with CLOSE_WAIT piling up mean HTTP or DB connections that were read but never closed. Look for a missing close or a connection not returned to the pool.
The system-wide limit comes last
cat /proc/sys/fs/file-nr # allocated · 0 · maximum (fs.file-max)
Touch fs.file-max only for Too many open files in system. It does nothing for EMFILE.
What not to do
- Raising
ulimit -nin your shell and calling it fixed — the service limit has not changed. - Hiding a leak behind a higher limit — it only delays the outage.
The order to work in
- Compare the limit and usage of that process:
/proc/<pid>/limitsandls /proc/<pid>/fd | wc -l - systemd:
LimitNOFILE=→ daemon-reload → restart; containers:--ulimitor runtime settings - After the restart, confirm the new value in
/proc/<pid>/limits - Record the fd count over time; if it keeps rising, trace the leak with
lsof -p
To practise the same situation as a problem, start with the one-minute quiz Too many open files: new connections fail under load.
Quick diagnostic checklist
- Tell
Too many open files(per process) fromToo many open files in system(system-wide). - Read the service's limit in
/proc/<pid>/limits, notulimit -nin your shell. - Compare the open fd count from
ls /proc/<pid>/fd | wc -lwith the limit. - For a systemd service, set
LimitNOFILE=in an override.conf, run daemon-reload and restart. - Remember that
/etc/security/limits.confdoes not apply to systemd services. - For containers, change the limit with
docker run --ulimit nofile=or the node's runtime settings. - Record the fd count over time and, if it keeps rising, use
lsof -pto see what leaks. - Adjust
fs.file-maxonly forToo many open files in system.