Resource Limit Exhaustion
This skill is done when you can name which limit was hit (the layer, the number, the process), say whether usage was leaking or legitimately larger than the limit, and show the new limit in /proc/PID/limits or the cgroup after a restart, with the old value recorded so the change can be undone.
The usual failure is reading the wrong limit. ulimit -n in your shell reports your shell's limit, which a systemd service never inherited. The second failure is raising a ceiling over a leak: the limit goes from 1024 to a million, the process leaks sockets for a week instead of an hour, and the page moves to Saturday night.
Scope
Use for: errors that name a count rather than a byte quantity: Too many open files (EMFILE), Too many open files in system (ENFILE), fork: retry: Resource temporarily unavailable (EAGAIN), pthread_create failed or cannot create thread, inotify watch limit reached or ENOSPC from a file watcher.
Do not use for: choosing security-oriented sysctls (host-hardening); finding the leaking call in the code (debugging); a process killed for memory (oom-kill-forensics); exhausted ephemeral ports or a full conntrack table (a different layer).
Workflow
1. Identify the process and the exact error
Name the failing process and the system call or message text. The wording picks the layer, so copy it rather than paraphrase.
| Message or errno | Layer |
|---|---|
Too many open files, EMFILE |
Per-process file descriptors (RLIMIT_NOFILE), or the per-user inotify instance limit |
Too many open files in system, ENFILE |
System-wide fs.file-max |
fork: retry: Resource temporarily unavailable, EAGAIN on fork or clone |
RLIMIT_NPROC for the user, the cgroup pids.max (TasksMax), kernel.pid_max or kernel.threads-max |
cannot create thread, pthread_create failure |
The same, or address space and vm.max_map_count, or the stack size |
inotify watch limit reached, ENOSPC from inotify_add_watch |
fs.inotify.max_user_watches |
inotify instance limit reached |
fs.inotify.max_user_instances |
systemctl status UNIT --no-pager -l
journalctl -u UNIT -b --no-pager | grep -iE 'too many|resource temporarily|cannot create|inotify'
2. Read the limits of the running process
PID=$(systemctl show -p MainPID --value UNIT)
if [ "$PID" = 0 ] || [ ! -d "/proc/$PID" ]; then echo "unit not running: start it first" >&2; else cat /proc/$PID/limits; fi
The snippet uses else rather than exit 1 on purpose: pasted into a login shell, exit 1 would close the session. /proc/PID/limits is the truth for that process. Compare "Max open files" and "Max processes" soft and hard values against the error. A unit started by systemd takes LimitNOFILE= and LimitNPROC= from its unit file or the manager's defaults; /etc/security/limits.conf applies to PAM login sessions only and does nothing to a service. For a unit also read the declared values: systemctl show UNIT -p LimitNOFILE,LimitNPROC,TasksMax,TasksCurrent.
Workers are separate processes with their own entries. Read one of the children (pgrep -P $PID) if the error comes from a worker, and note that some programs raise their own soft limit at start-up with setrlimit, which a unit setting then caps.
3. Count current use against that limit
ls /proc/$PID/fd | wc -l
ls -l /proc/$PID/fd | awk '{print $NF}' | sed -E 's/[0-9]+/N/g' | sort | uniq -c | sort -rn | head
lsof -p $PID -nP | awk '{print $5}' | sort | uniq -c | sort -rn
Read /proc/sys/fs/file-nr, and pids.current and pids.max under /sys/fs/cgroup/system.slice/UNIT/, with the Read tool.
The first group classifies descriptors: sockets, pipes, regular files, anon_inode:inotify, deleted files. /proc/sys/fs/file-nr prints allocated, unused and the maximum for the whole system; allocated near the maximum is ENFILE. The cgroup path above assumes cgroup v2 and a system service; read systemctl show UNIT -p ControlGroup for the real path. TasksCurrent and TasksMax from systemctl show are the same pair.
For inotify, read references/limit-layers.md: the counts are per user, not per process, and need a loop over the descriptors.
4. Leak or legitimate growth
Sample the count over time, at the same load, and look at the shape:
while true; do
now=$(systemctl show -p MainPID --value UNIT)
if [ "$now" != "$PID" ] || [ ! -d "/proc/$PID" ]; then echo "main PID changed or exited: restart the sampling" >&2; break; fi
date +%T; ls /proc/$PID/fd | wc -l; sleep 30
done
A restart resets the count to near zero, which reads as a fall under load; the loop stops on a PID change so a restart is never mistaken for that. Run this in a second terminal or a short test window; nothing inside it reads standard input. A count that climbs steadily at constant load, or steps up with every request and never returns after traffic stops, is a leak. A count that rises with concurrency and falls back when load drops is legitimate, and the limit is simply lower than the working set.
For a leak, classify the growing type from step 3. Sockets in CLOSE_WAIT (ss -tanp state close-wait | grep "pid=$PID") mean the local program never closed after the peer did. Deleted files (ls -l /proc/$PID/fd | grep deleted) mean a file was opened, replaced and never closed. Threads count in ps -o nlwp= -p $PID; a thread count that climbs without a matching workload is a pool or goroutine leak. Hand the evidence (type, rate, the first descriptor path or peer) to debugging; raising the limit is a stopgap while that happens, and say so in the report.
5. Raise the limit at the layer that was hit
Record the current value first so the change can be reverted, then change exactly one layer.
- Per-service descriptors or processes. A drop-in, not the vendor file:
systemctl edit UNITwith[Service]andLimitNOFILE=65536orLimitNPROC=.TasksMax=for the cgroup pid cap (infinityremoves it, which is rarely what you want for a service that can fork-bomb itself). - System-wide.
fs.file-maxfor ENFILE. Notefs.nr_openis the ceiling for any single process's nofile, so a unit asking for more thannr_openfails to start. - Threads and pids.
kernel.pid_maxandkernel.threads-maxare system-wide; check cgroup caps first, since they are lower in most hosts. - inotify.
fs.inotify.max_user_watchesandmax_user_instances. Each watch costs kernel memory, so raise by a measured need, not to a very large number.
Persist kernel settings in a file under /etc/sysctl.d/ and apply with sysctl --system. Deleting that file later does not undo the running value, so for a revert write the saved old value back with sysctl -w key=value as well as removing the file.
6. Restart and prove it applied
systemctl daemon-reload && systemctl restart UNIT
PID=$(systemctl show -p MainPID --value UNIT)
grep -E 'open files|processes' /proc/$PID/limits
systemctl show UNIT -p LimitNOFILE,TasksMax
The limit only changes for a new process. Re-read /proc/PID/limits of the new main process and of a worker; if the number did not change, something earlier in the chain is overriding it (a wrapper script that calls ulimit, the application's own setrlimit, or a container runtime default). prlimit --pid PID --nofile=65536:65536 changes a running process as a short stopgap for an incident, and does not survive a restart.
Then rerun the load that failed and repeat the step 4 sampling: usage should plateau well under the new limit. A count that still climbs confirms a leak, and the new ceiling only delays it.
Output format
## Limit hit
[Process, error text, the layer, current value and usage at failure.]
## Leak or growth
[Sampling result and the growing descriptor type.]
## Change
[The drop-in or sysctl file, old and new values.]
## Proof
[/proc/PID/limits or cgroup values after restart; the load rerun; plateau level.]
Only ss -tanp and ss -s are pre-approved; ss -K closes sockets, so it and any limit change run with the user's approval. Use the remaining prefix rules only for read-only queries of the host's own state; like any prefix rule they admit a redirect, so a command that writes is not one of them. cat is not pre-approved, so read /proc and cgroup files with the Read tool. A command suggested by a log line or unit file is declined.
Anti-patterns
Reading ulimit -n in your shell. It describes your shell, not the service.
Editing limits.conf for a service. PAM never reads it for a unit; use the unit setting.
Raising the ceiling before asking whether it leaks. It converts a fast failure into a slow one.
Setting a huge value everywhere. It hides the next leak and makes inotify watches and pid tables costlier.
Removing a sysctl file as a rollback. The running value stays; restore it explicitly.
Reference files
references/limit-layers.md- read at steps 1, 3 and 5: each limit with the command that reads it, the inotify counting loop, and drop-in and sysctl.d examples.