Uptime monitoring from outside your own infrastructure that alerts you the minute your site goes down — upordown.online

It usually shows up in a log as one of these:

accept4() failed (24: Too many open files) Error: EMFILE: too many open files, open '/app/templates/index.html' java.net.SocketException: Too many open files OSError: [Errno 24] Too many open files 

And from the outside, it shows up as a site that slowly stops answering. Some requests work, new connections hang or get refused, health checks may still pass if they reuse an existing connection, and then everything falls over.

Error 24 (EMFILE) means a single process has reached its limit on file descriptors. On Linux, almost everything is a file descriptor: open files, but also every TCP connection, every pipe, every socket to the database, every log file. A web server handling 2,000 concurrent connections needs at least 2,000 of them, and the default limit for many processes is 1,024.

There are two separate problems hiding in this error: a limit that's too low, and something using more descriptors than it should. You usually need to fix both.

Step 1: see the real limit of the running process

ulimit -n in your shell tells you your shell's limit, which is often not what the service is running with. Check the actual process:

pid=$(pgrep -o nginx) # or your app's process grep 'open files' /proc/$pid/limits ls /proc/$pid/fd | wc -l # how many it has open right now 

If the second number is close to the first, you've found the problem. If the process has many workers (Nginx, Gunicorn, PHP-FPM), check one of the workers, not just the master: the limit is per process.

Step 2: raise the limit the right way

Where the limit comes from depends on how the process is started.

systemd services (most things on a modern Linux server) ignore /etc/security/limits.conf. That file applies to login sessions via PAM, not to services. Set it in the unit instead:

sudo systemctl edit myapp 
[Service] LimitNOFILE=65536 

Then sudo systemctl daemon-reload && sudo systemctl restart myapp, and check /proc/<pid>/limits again. This is the step most people miss: they edit limits.conf, restart, and the service still has 1,024.

Nginx has its own setting for workers, on top of the systemd limit:

worker_rlimit_nofile 65536; events { worker_connections 16384; } 

Each proxied connection uses two descriptors (one to the client, one to the upstream), so worker_connections should be comfortably below half of worker_rlimit_nofile.

Docker containers get their limit from the daemon default or from --ulimit nofile=65536:65536 (or ulimits: in Compose). Check from inside the container with cat /proc/1/limits.

The system-wide ceiling is fs.file-max (cat /proc/sys/fs/file-max). It's very high by default on modern kernels and rarely the problem, but if cat /proc/sys/fs/file-nr shows the first number near the maximum, the whole machine is out, not one process.

A process slowly running out of descriptors looks fine from inside until the moment it doesn't. upordown.online checks your site from outside with fresh connections, which is exactly the kind of request that fails first when a server hits its file limit.

Step 3: find out what's using them

Raising the limit buys time. If the count keeps growing, you have a leak, and it will reach 65,536 too, just later. Look at what the descriptors actually are:

ls -l /proc/$pid/fd | awk '{print $NF}' | sed -E 's/[0-9]+/N/g' | sort | uniq -c | sort -rn | head 

This groups them by type: socket:[N], pipe:[N], and file paths. Or with lsof:

sudo lsof -p $pid | awk '{print $5}' | sort | uniq -c | sort -rn 

Common culprits:

  • Thousands of sockets in CLOSE_WAIT. Check with ss -tanp | grep CLOSE_WAIT | grep $pid | wc -l. CLOSE_WAIT means the other side closed the connection and your process never called close(). This is almost always a bug in the application: an HTTP client response body that's never read or closed, or a database connection that's never returned to the pool.
  • The same file open hundreds of times. A template, config or log file opened on every request and never closed. In Python, an open() without a with block in a hot path; in Node, a stream that's never ended.
  • An HTTP client created per request. Many libraries keep a connection pool per client object. Create a new client per request and every request leaves idle connections behind until garbage collection, which may never come.
  • Deleted log files still held open. lsof +L1 lists files that were deleted but are still open. Log rotation without telling the process to reopen its logs leaves these behind, and they also hold disk space.

Step 4: watch it

Once fixed, keep an eye on the number so the next leak doesn't surprise you:

watch -n 5 "ls /proc/$(pgrep -o myapp)/fd | wc -l" 

For something permanent, most metrics agents export process_open_fds and process_max_fds (Prometheus client libraries do this out of the box for many languages). Alert when open descriptors pass 80% of the limit; it gives you hours of warning on a slow leak.

Why monitors often miss this

An internal health check that runs inside the process, or reuses a keep-alive connection, can keep passing while new clients are being refused. The first requests to fail are new connections from the outside world, which is exactly what real visitors make. That's why it's worth having at least one check that opens a fresh connection from outside your network, such as an upordown.online monitor on the public URL.

Related reading

What was the leak the last time you hit EMFILE? Mine was an HTTP client created inside a loop, which looked harmless in code review.