Lorixen.
EN

'Too many open files': fix file descriptor limits and leaks

Short answer: "Too many open files" (error EMFILE) means a process has hit its limit on file descriptors, which cover not just files but network connections, sockets and pipes. Busy web apps, proxies and databases hit it under load, or because of a leak where connections or files are opened and never closed. Check the process's current limit and how many descriptors it's using, fix any leak, then raise the limit where the service is defined, such as LimitNOFILE= in its systemd unit. Raising ulimit in your own shell doesn't change a running service.

What the error means

On Linux, every open file, network connection, socket and pipe a process uses is tracked by a number called a file descriptor. Each process has a cap on how many it can hold. The Linux manual page for resource limits describes RLIMIT_NOFILE as one greater than the highest descriptor number the process can open, and says attempts to go past it, through calls such as open, pipe or dup, fail with the error EMFILE.

Each limit has two values: a soft limit, which is the one enforced, and a hard limit, the ceiling the soft limit can be raised to without extra privileges.

In application logs, the error shows up in many forms:

  • "Too many open files" or "EMFILE" from Node.js, Python, Java or Go.
  • nginx error log entries about failing to accept connections or open files.
  • Database servers refusing new connections.
  • Random failures to read templates, write logs or open uploads under load.

Two very different causes

Genuine load. A busy service holds many connections at once: each visitor's connection, each connection to the database, each upstream call. The default soft limit on many systems is modest, and a popular sale or a slow upstream can push a healthy app over it.

A leak. The app opens files or connections and never closes them. Usage climbs steadily over hours or days, regardless of traffic, until the process hits the limit. Raising the limit only delays the next failure.

Tell them apart by watching the count over time. Steady growth during quiet periods points to a leak; spikes that match traffic point to load.

Measuring what's happening

For a running process with ID 1234:

  • Current limits: cat /proc/1234/limits and look for "Max open files".
  • Descriptors in use: ls /proc/1234/fd | wc -l.
  • What they are: ls -l /proc/1234/fd or lsof -p 1234 shows files, sockets and pipes, which often makes a leak obvious, for example thousands of connections to the same API, or the same log file opened repeatedly.

Record the count every few minutes during normal operation to see the trend.

Fixing leaks

Common leak patterns in business apps:

  • Opening files in a loop without closing them; use your language's "with" or "using" pattern so files close automatically.
  • Creating a new HTTP client or database connection per request instead of reusing a pool.
  • Not closing response bodies from outbound HTTP calls, so connections stay open.
  • Spawning child processes and never cleaning up their pipes.
  • File watchers in development tools left running in production.

After a fix, the descriptor count should rise and fall with traffic rather than climbing.

Raising the limit properly

If the load is real, raise the limit where the process is started:

  • systemd services: set LimitNOFILE= in the unit file, or an override created with systemctl edit. The systemd documentation lists it among the resource limit settings that set soft and hard limits for the service. Then reload and restart the service.
  • Docker containers: set the open-files limit with the container runtime's ulimit option or in your compose file.
  • nginx: besides the service limit, nginx has a worker_rlimit_nofile directive in its core module that sets the limit for worker processes, which should be in line with worker_connections.
  • Login sessions: ulimit -n and /etc/security/limits.conf affect shells and programs started from them, not services managed by systemd. This is the most common reason "I raised the limit and nothing changed".

Raise limits to a sensible level for your workload rather than to the maximum. Very high values can hide leaks and use extra memory in some programs.

Alert before it fails

Don't wait for customers to find the limit for you. Most monitoring tools can track open descriptors per process; Prometheus's process metrics, for example, include open and maximum file descriptor counts for many applications. Set an alert when usage passes a comfortable share of the limit, and add the descriptor count to the dashboard you check after deploys. A line that climbs steadily after a release is the earliest sign of a new leak.

Check the whole chain

A single request can consume descriptors in several places: the load balancer, nginx, the app and the database each hold connections. When one of them hits its limit, the error may appear in a different layer's logs. Check limits and usage for each service involved, and remember that the database's own connection limit is a separate setting.

A worked example

Picture a hypothetical Gold Coast surf-lesson booking platform whose Node.js API started failing every few days with "EMFILE: too many open files", always fixed by a restart. The descriptor count, checked through /proc, climbed steadily even overnight. lsof showed thousands of open connections to the weather API used to show conditions on each lesson page: the code created a new HTTP agent per request and never reused it. The developer switched to one shared agent with keep-alive, the count levelled off, and they raised LimitNOFILE modestly for busy weekends anyway.

FAQ

Is "too many open files" a disk problem?

No. It's about the number of open descriptors, not disk space.

Can I change the limit without restarting?

Some tools can change a running process's limits, but the reliable fix is to set it in the service definition and restart.

What's a reasonable limit?

It depends on how many concurrent connections and files the service needs. Measure peak usage and leave headroom.

Why do tests pass but production fails?

Tests rarely run long enough or at enough concurrency to reach the limit, especially with slow leaks.

If your app keeps hitting "too many open files", tell us what it runs on and we'll help find whether it's load or a leak.

Sources

WhatsAppBook a call