Skip to content

Error explained

Too many open files (EMFILE)

Every file, socket and pipe uses a file descriptor. When a process hits its limit, new connections and files fail. Raise the limit if it is too low, and look for leaks.

3 min read9 sectionsUpdated

On this page

Key points

  • Sockets count as files: busy network services hit the limit first.
  • The default soft limit is often 1024, too low for servers.
  • systemd and containers set their own limits.
  • A count that only grows is a leak.

Check usage and limits

bash
cat /proc/<pid>/limits | grep -i "open files"     # the process's actual limit
ls /proc/<pid>/fd | wc -l                         # descriptors in use now
lsof -p <pid> | awk '{print $5}' | sort | uniq -c | sort -rn   # by type (REG, IPv4, sock...)
ulimit -n                                         # the current shell's soft limit

Raise the limit where the process starts

limits.confplaintext
# systemd service: /etc/systemd/system/myapp.service.d/limits.conf
[Service]
LimitNOFILE=65536
bash
docker run --ulimit nofile=65536:65536 myimage
plaintext
# Kubernetes uses the container runtime's defaults; set them on the node or runtime,
# and check inside the pod with: cat /proc/1/limits

Finding leaks

If the descriptor count keeps rising under steady traffic, something is not being closed: HTTP responses whose bodies are never read, files opened without a context manager or finally, database connections created per request, or a new HTTP client per call. Compare lsof output a few minutes apart to see which kind of descriptor grows, and the target addresses for sockets.

Catching it in production

Export the process's open-descriptor count as a metric and alert at 80% of the limit. When the limit is hit, errors spike on every endpoint that opens connections, which traces will show as failed client spans.

VitalRix error tracking groups exceptions recorded on OpenTelemetry spans into issues with counts, first and last seen, affected endpoints and versions, and marks an issue as regressed if it comes back after being resolved. Alert rules on error rate per service or endpoint route the problem to the right team before customers report it.

A worked example

A Node.js service calling a partner API creates a new HTTPS agent per request and never destroys it. Each request leaves a socket open until the partner closes it. Under steady load the descriptor count rises by about 30 a minute and the process hits its 1024 limit after half an hour, failing every new connection with EMFILE. Reusing a single keep-alive agent with a bounded maxSockets keeps the count flat at around 50.

Frequently asked questions

Is it safe to set a very high limit?

High limits such as 65536 or more are normal for servers. They do not fix leaks, which will still hit any limit eventually.

Why does ulimit -n show a high number but the app still fails?

The app may run under systemd, a container or a different user with its own limits. Check /proc//limits for the real value.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.