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
# systemd service: /etc/systemd/system/myapp.service.d/limits.conf
[Service]
LimitNOFILE=65536docker run --ulimit nofile=65536:65536 myimage# Kubernetes uses the container runtime's defaults; set them on the node or runtime,
# and check inside the pod with: cat /proc/1/limitsFinding 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.
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.
Related reading
- EMFILE: too many open files in Node.js: Fix EMFILE too many open files in Node.js: leaked sockets and streams, unbounded concurrent file reads, ulimits and file watchers, with lsof commands.
- MySQL: Too many connections: Fix MySQL error 1040 Too many connections: check max_connections and current threads, find sleeping or leaked connections, size application pools.
- Nginx "worker_connections are not enough": how to fix it: Fix Nginx worker_connections are not enough: why proxying uses two connections per client, how to size limits, raise descriptors and add upstream keepalive.
- PostgreSQL "too many clients already": how to fix it: Fix PostgreSQL FATAL: sorry, too many clients already. See who holds connections, stop leaks, add PgBouncer and size max_connections the right way.
- Too many TIME_WAIT connections: what to do: Understand Linux TIME_WAIT sockets, when they cause ephemeral port exhaustion, why keep-alive is the real fix and which kernel settings not to touch.
- ERR_TOO_MANY_REDIRECTS: how to fix a redirect loop: Fix ERR_TOO_MANY_REDIRECTS: trace the chain with curl, find the loop between CDN, server and app, such as HTTPS or www rules, and prevent it.
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.