A Python loop opening the same small file over and over stopped on its 98th try with [Errno 24] Too many open files. The limit had been set to 100. Ninety-seven opens succeeded. The other three slots were taken before the loop began, by standard input, output and error. Every test in this post was run on a Linux machine, and the numbers are the ones it printed.
What a file descriptor is
Not only files
A file descriptor is the small integer a Linux process gets back when it opens something. Despite the name, that something does not have to be a file. Pipes, network sockets, directories and devices all hand out descriptors from the same pool. In the same test, opening five sockets in a fresh Python process returned descriptors 3, 4, 5, 6 and 7, the first numbers free after the three standard streams.
That shared pool is why the error shows up in places that seem to have nothing to do with files. A web server that cannot accept another connection, a database that cannot open another table, and a build tool that cannot spawn another child process can all be the same problem: the process ran out of numbers.
Two different errors
The Linux manual page for open() lists two separate errors that both read as “too many open files” in plain English. EMFILE means the per-process limit on the number of open file descriptors has been reached. ENFILE means the system-wide limit on the total number of open files has been reached. The first is one program hitting its own ceiling. The second is the whole machine running out.
Errno 24, the error in the opening test, is EMFILE. That is the common case, and it is almost never fixed by touching the system-wide setting.

The per-process limit: RLIMIT_NOFILE
What the number means
The per-process ceiling is the resource limit called RLIMIT_NOFILE. Its manual page describes it as a value one greater than the maximum file descriptor number that the process can open, and says that attempts through open(), pipe(), dup() and similar calls to exceed it yield EMFILE.
“One greater than the maximum descriptor number” explains the 98. With a limit of 100, the highest usable descriptor is 99. Descriptors 0 through 99 make 100 slots. The process already held 0, 1 and 2, so the loop got the remaining 97 and failed on the 98th.
Soft and hard
Every resource limit has two values. The same manual page says the soft limit is the value that the kernel enforces, while the hard limit acts as a ceiling for the soft one. An unprivileged process may set its soft limit anywhere from 0 up to the hard limit, and may lower its hard limit, irreversibly. A process with the CAP_SYS_RESOURCE capability can change either value freely.
Those rules were tested as the unprivileged nobody user, starting from a soft limit of 1,024 and a hard limit of 4,096:
| Action | Result |
|---|---|
ulimit -Sn 4096 (soft up to hard) |
Allowed |
ulimit -Sn 8192 (soft above hard) |
cannot modify limit: Invalid argument |
ulimit -Hn 2048 (hard down) |
Allowed |
ulimit -Hn 4096 (hard back up) |
cannot modify limit: Operation not permitted |
The last row is the one that catches people. Lowering the hard limit in a shell to test something cannot be undone in that shell without privilege.
Reading the current limits
Three places to look
ulimit -Sn and ulimit -Hn print the current shell’s soft and hard values. For a running process, /proc/<pid>/limits has a “Max open files” line with both. prlimit reads and changes them from outside the process:
$ prlimit --pid "$PID" --nofile
RESOURCE DESCRIPTION SOFT HARD UNITS
NOFILE max number of open files 20000 20000 files
$ prlimit --pid "$PID" --nofile=4096:8192
$ grep "open files" /proc/$PID/limits
Max open files 4096 8192 files
That change took effect on a running sleep process without restarting it, which makes prlimit the tool for a production process that cannot be bounced right now. The program will not notice unless it checks; it will simply stop failing.
Counting what is open
To see how close a process is to its ceiling, count its descriptors. ls /proc/<pid>/fd | wc -l gives the number; ls -l /proc/<pid>/fd shows what each one points to, files by path and sockets as socket:[inode]. lsof -p <pid> gives a more readable version of the same list. A count that keeps rising while the workload stays flat is the signature of a leak.

The system-wide limits
file-max and file-nr
The machine-wide ceiling lives in /proc/sys/fs/file-max. Its manual page says this file defines a system-wide limit on the number of open files for all processes, and that system calls failing at this limit fail with ENFILE. The companion file /proc/sys/fs/file-nr holds three numbers: allocated file handles, free file handles, and the maximum. On the test machine it read 167 0 818942, so 167 handles were in use out of a ceiling above 800,000.
That gap is typical. On a modern system, file-max is scaled to memory and is rarely the limit anyone actually hits.
nr_open
A third file, /proc/sys/fs/nr_open, caps how high anyone can raise RLIMIT_NOFILE at all. The manual page says it imposes a ceiling on the value to which the RLIMIT_NOFILE resource limit can be raised, for privileged and unprivileged processes alike, with a default of 1,048,576. The test machine reported exactly that.
Raising the limit the right way
Login sessions: limits.conf
For users who log in, the place for a permanent change is /etc/security/limits.conf or a file in /etc/security/limits.d/. These are read by the pam_limits module, which the manual describes as applying ulimit limits … to user login sessions. The item name is nofile. A hard line sets the ceiling users cannot raise themselves; a soft line sets what they start with and can move within that range:
# /etc/security/limits.d/90-nofile.conf
appuser soft nofile 8192
appuser hard nofile 65536
The key word is “login”. A service started by systemd at boot never passes through a login session, so these lines do not reach it.
Services: LimitNOFILE
For a systemd service, the setting is LimitNOFILE= in the unit file or a drop-in, which maps to ulimit -n. The systemd documentation adds a warning worth reading in full. It says to be careful raising the soft limit above 1,024, since select(2) cannot function with file descriptors above 1023 on Linux, notes that the hard limit nowadays defaults to 524,288, and says applications should typically raise their own soft limit to the hard limit if they do not use select().
That is why the systemd manager’s own default is printed in /etc/systemd/system.conf on the test machine as #DefaultLimitNOFILE=1024:524288: a low soft limit to protect old code, and a high hard limit that modern code can climb to on its own. The commands for applying a drop-in, systemctl daemon-reload and a restart, are covered in the earlier posts on systemctl enable versus start and on Restart= and RestartSec=.

What children inherit
Limits pass down
Resource limits are inherited. A shell that set its soft limit to 512 started a child shell, and the child reported 512. That is why a limit set in the wrong place, a terminal instead of the service’s own configuration, appears to work during testing and then vanishes in production: the program under test inherited the terminal’s value, and the service never had a terminal.
Descriptors can pass down too
Open descriptors can be inherited as well, which matters because a descriptor held by a child counts against that child’s limit and keeps the underlying file or socket alive. In bash, a file opened with exec 7</etc/hostname was still open as descriptor 7 in a child bash process, which listed 0 1 2 7 in its /proc/self/fd.
Python behaves differently by design. PEP 446, adopted in Python 3.4, proposed to make all file descriptors created by Python non-inheritable by default, citing leaks into child processes as a known security problem. In the test, a file opened in Python reported inheritable: False, and a child started with subprocess saw only 0 1 2. A shell script that opens files and then launches long-running children has no such protection unless it closes what it opened.
When raising it is the wrong fix
Leaks
A higher limit buys time. It does not fix a process that opens descriptors and never closes them. In the leak test, the same loop that died at 98 opens ran 200 times without trouble once each open was wrapped in a with block that closes the file at the end. Bash shows the same failure with no Python involved: a loop of exec {fd}</dev/null under ulimit -n 64 failed at iteration 55, because each exec held its descriptor open.
The practical order is to count first, then decide. If the count climbs and never comes down, the limit is not the problem. If it rises with load and falls back, the process simply needs more room, and the soft limit can come up to the hard one.
Sources
- open(2), Linux manual page (man7.org): https://man7.org/linux/man-pages/man2/open.2.html
- getrlimit(2), Linux manual page (man7.org): https://man7.org/linux/man-pages/man2/getrlimit.2.html
- proc_sys_fs(5), Linux manual page (man7.org): https://man7.org/linux/man-pages/man5/proc_sys_fs.5.html
- limits.conf(5), Linux manual page (man7.org): https://man7.org/linux/man-pages/man5/limits.conf.5.html
- PEP 446, Make newly created file descriptors non-inheritable (peps.python.org): https://peps.python.org/pep-0446/
- systemd.exec, systemd documentation (freedesktop.org): https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html
