How to Fix Linux 'Too Many Open Files' Errors
'Too many open files' (Errno 24, EMFILE) means one process used up its table of open files: log files and network connections each take a slot, 1024 by default. On a real Ubuntu server we count what a payment app holds, show why limits.conf doesn't reach a systemd service, raise LimitNOFILE the right way, and then fix the leak that was filling the table.
Payment 1,020 fails
Your server says: too many open files, and the fix most answers give you won't touch it. At a laundromat, the card readers on twelve washers send each payment to one small server. Jim, who looks after it, sends eleven hundred test payments. Payment one thousand and twenty fails. The server's log says why: error number twenty-four. Twelve washers, and somehow more than a thousand open files. Let's find where they come from, why the usual fix fails, and the one line that really fixes it.
./pay.sh 1100payment 1020 (washer-12): failed, HTTP 000 1019 of 1100 payments went through
journalctl -u washpay -n 2 --no-pager -o catOSError: [Errno 24] Too many open files: '/var/log/washpay/washer-12.log' ----------------------------------------
What counts as an open file?
What counts as an open file? On Linux, almost everything a program opens: a log file it writes to, and every network connection, like each payment coming in. Each one gets a number in the program's own table, called a file descriptor, and that table has a limit. When it's full, the next open fails, with error twenty-four.

Count what the server holds
How full is it? Jim gets the payment server's process ID, its number among running programs, from systemctl, the tool that manages background programs. Linux keeps a folder for every running program, with one entry per open file, so counting that folder counts its open files: 1,023. A file beside it lists the program's limit: one thousand and twenty-four. And lsof, which lists open files by name, shows what fills it: each washer's log, opened about 85 times.
PID=$(systemctl show -p MainPID --value washpay)
sudo ls /proc/$PID/fd | wc -l1023
grep 'open files' /proc/$PID/limitsMax open files 1024 524288 files
sudo lsof -p $PID | awk '{print $NF}' | sort | uniq -c | sort -rn | head -485 /var/log/washpay/washer-11.log 85 /var/log/washpay/washer-10.log 85 /var/log/washpay/washer-09.log 85 /var/log/washpay/washer-08.log
The usual fix: raise the limit
The usual fix, in most answers online: raise the limit in limits.conf. Jim adds a line that sets nofile, the open-file limit, to eight thousand one hundred and ninety-two for every user. A fresh login as Jim shows the new limit took effect. Guess first: after a restart, what's the payment server's limit? Still one thousand and twenty-four.
echo '* soft nofile 8192' | sudo tee -a /etc/security/limits.conf* soft nofile 8192
sudo -iu jim ulimit -n8192
sudo systemctl restart washpay
PID=$(systemctl show -p MainPID --value washpay)
grep 'open files' /proc/$PID/limitsMax open files 1024 524288 files
Why? limits.conf is read when someone logs in: a terminal, ssh, or sudo. A service never logs in. Services are started by systemd, the manager that systemctl talks to. It starts them directly, with its own default: one thousand and twenty-four. The manual page for limits.conf says it plainly: it does not apply to system services.

The service's own limit
So what does a service get? Every process carries two limits. The soft limit is the one that's enforced. The hard limit is a ceiling: a program may raise its own soft limit up to it, but no higher. systemctl show prints both for the payment server. Soft, one thousand and twenty-four. The hard limit is listed simply as LimitNOFILE: five hundred and twenty-four thousand. The payment server is a small Python program, and Python never raises its soft limit by itself, so one thousand and twenty-four is all it gets.
systemctl show -p LimitNOFILESoft -p LimitNOFILE washpayLimitNOFILE=524288 LimitNOFILESoft=1024
Every service has a unit file: the settings systemd reads before starting that service. The limit there is called LimitNOFILE. Jim puts it in a small override file, in a folder named after the service, so a package update won't undo it. Then Jim tells systemd to reread its settings, and restarts the payment server so it starts with the new limit. Its limits file now says eight thousand one hundred and ninety-two, for the soft limit and the hard one: one number sets both. systemctl show agrees.
printf '[Service]\nLimitNOFILE=8192\n' | sudo tee /etc/systemd/system/washpay.service.d/nofile.conf[Service] LimitNOFILE=8192
sudo systemctl daemon-reload && sudo systemctl restart washpay
grep 'open files' /proc/$PID/limitsMax open files 8192 8192 files
systemctl show -p LimitNOFILESoft -p LimitNOFILE washpayLimitNOFILE=8192 LimitNOFILESoft=8192
Three thousand payments now go through. Guess first: how many files is the server holding? 3,004. It just has more room to fill.
./pay.sh 30003000 of 3000 payments went through
sudo ls /proc/$PID/fd | wc -l3004
It's like a coat check with numbered tickets. Each guest who checks a coat takes a ticket, and should hand it back on the way out. If the tickets never come back, a bigger roll only means it runs out later. The limit isn't the problem. The tickets are.

The real fix: close what you open
So where do the tickets go? Follow washer one. Its first payment opens its log file, and keeps it open to write to. Twelve payments later, washer one pays again, and the app opens the same log file a second time, without closing the first. After 85 rounds, that's 85 copies of one file, and twelve washers fill the table.

Here's the code. In Python, a logger is what the app writes its messages to, and a file handler is the part that holds a log file open. For every payment, the app adds another handler to the washer's logger, and the old ones never go away. The fix is one new line: add the handler only if the logger has none.

Restart, three thousand payments again, and the server holds 16 open files. Twelve of them are the washer logs: one each, opened once. However many payments come in now, that number stays flat.
sudo systemctl restart washpay
PID=$(systemctl show -p MainPID --value washpay)
./pay.sh 30003000 of 3000 payments went through
sudo ls /proc/$PID/fd | wc -l16
sudo lsof -p $PID | grep -c 'washpay/washer-'12
Before you call it fixed
A bigger limit can hide a leak for weeks, until it fails again on a busy day. So before you call it fixed: count the open files, run the busy part, and count again. A number that keeps climbing is a leak, whatever the limit. Sometimes, though, the number climbs and then levels off high. Busy programs like web servers and databases often really do need many files, one for each connection. For those, raise LimitNOFILE in an override file for the service, like Jim did, not in limits.conf.

Recap
So, too many open files: count what the process holds, raise a service's limit in its own unit file, and close what you open.

If your server takes its payments again, subscribing brings more fixes, each one tried on a real machine.
Every number in this video came from a real Ubuntu server, and every command and the lab files are linked in the description. Thanks for watching Cloud Explainers.
Sources
- open(2): EMFILE, the per-process limit on open file descriptors was reached
- getrlimit(2): RLIMIT_NOFILE, soft and hard limits
- proc_pid_fd(5): /proc/PID/fd, one entry per open file descriptor
- limits.conf(5): applied at login by pam_limits; not for system services
- systemd.exec(5): LimitNOFILE= and systemd's default for services (soft 1024, hard 524288)
- Python logging, FileHandler (opens its file when created)
Demos: a container on our own Mac standing in for a laundromat's payment server; the laundromat and its staff are made up. Every error and count was real. Watch the full video on YouTube.