Practical technical guides / Core IT: Linux reliability

Too Many Open Files: Soft and Hard RLIMIT_NOFILE, systemd LimitNOFILE and Runtimes That Raise It

Why the soft limit is still 1024 under systemd, which runtimes raise it (JVM, Go, Node.js) and which do not (CPython), and how to size and verify LimitNOFILE.

By Sandeep Belgavi · Published 2026-11-29

Why the soft limit is still 1024 under systemd, which runtimes raise it (JVM, Go, Node.js) and which do not (CPython), and how to size and verify LimitNOFILE.

The error that appears only under load

The catalog API of Belgavi's Book Store runs as a systemd service on Linux. It passes every test, then fails during the first busy evening with OSError: [Errno 24] Too many open files. A Java service on the same hosts, with similar traffic, never shows the error. The difference is not the code. It is the limit each process actually runs with, and which runtimes change that limit by themselves at startup.

Every socket, file, pipe and epoll instance a process holds is a file descriptor, and the kernel enforces a per-process maximum through the resource limit RLIMIT_NOFILE. When it is reached, open, socket and accept fail with EMFILE. This guide explains where the limit comes from, why the default soft limit is still 1024 on a modern system, which runtimes raise it, and how to size and verify it for a service. The companion example nofile_limit_planner applies these rules to a unit file and a descriptor budget.

Soft and hard limits

RLIMIT_NOFILE has two values. The soft limit is what the kernel enforces. The hard limit is the ceiling to which an unprivileged process may raise its own soft limit with setrlimit. Children inherit both. Raising the hard limit needs privilege, so the hard limit is set by whoever starts the service, normally systemd.

The kernel's own default is 1024 soft and 4096 hard. The systemd NEWS file for version 240 records the change most systems now run with: systemd raises the hard limit it passes to services to 512K (524288) and deliberately keeps the soft limit at 1024. The reason is select(), which cannot handle descriptor numbers of 1024 or above. A program, or any library it loads, that still calls select() would fail in strange ways if it suddenly received a descriptor numbered 2000. The NEWS entry says that programs which want more descriptors have to opt in by raising their own soft limit.

The systemd.exec manual repeats the advice in its note on LimitNOFILE=. It begins "Do not use", warns against raising the soft limit above 1024 because of select(), and recommends that applications which do not use select() raise their soft limit to the hard limit themselves.

Which runtimes raise the soft limit

Whether a service gets 1024 or 524288 descriptors therefore depends on its runtime:

RuntimeBehaviour at startup on LinuxSource
HotSpot JVMraises soft to hard while -XX:+MaxFDLimit is on, which is the defaultos_linux.cpp and globals.hpp, JDK 23
Go 1.19 and laterprograms that import os raise soft to hardGo 1.19 release notes
Node.jsraises soft to hard during platform initializationsrc/node.cc, v24.22.3
CPythonkeeps the inherited soft limitcall resource.setrlimit yourself
A C programkeeps the inherited soft limitunless it calls setrlimit

That table explains the evening incident. Under the systemd defaults the JVM service started with soft 1024 and raised itself to 524288. The Python API stayed at 1024. The Go 1.19 notes mention the flip side: a Go program now runs child processes with the raised limit, which can break very old C programs that use select(). The fix they suggest is to set the hard limit before starting the Go program.

Size the limit from a budget

Do not pick a number. Count what the process holds at peak:

  • client connections, from the server's maximum concurrent connections, not average traffic;
  • outbound pools: database pool size, HTTP client pool per upstream host, message broker connections;
  • files: log files, temporary files, memory-mapped files, and file handles held by libraries;
  • fixed overhead: listening sockets, epoll or inotify instances, pipes to child processes, standard streams.

For the catalog API: 600 client sockets, a 200-connection upstream pool, 80 files and pipes, and 20 listening and miscellaneous descriptors, which is 900 in total. With the inherited soft limit of 1024 that leaves 124 descriptors of headroom, about 14 percent. One slow upstream that holds its sockets open, or a traffic spike that doubles concurrent clients, is enough to hit EMFILE. The planner flags it as too tight; it treats headroom of less than a quarter of the budget as a warning.

There are two ways to fix it. For a service that never uses select(), raising the soft limit in the program is what systemd recommends:

import resource
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard))

If you cannot change the program, set the limit in the unit. LimitNOFILE=8192 sets soft and hard to 8192; LimitNOFILE=8192:65536 sets them separately.

[Service]
LimitNOFILE=8192:65536

A higher limit is not free, but it is cheap. The systemd 240 NEWS entry notes that descriptor memory is accounted like other memory, so bound the service's total memory with MemoryMax= rather than keeping descriptor limits artificially low.

Pools multiply

The descriptor budget is where most estimates go wrong, because pools are configured per destination. An HTTP client pool of 50 connections per upstream host, with 6 upstream hosts, is up to 300 sockets, not 50. A database pool of 20 per instance is per process. A service that runs 4 worker processes on one host holds four pools, but each process has its own limit, so size per process and multiply only for host-wide limits such as fs.file-max.

Protocol choice changes the count too. HTTP/1.1 needs one connection per request in flight, so concurrency drives sockets directly. HTTP/2 and gRPC multiplex many requests over one connection, so a client that switches protocols can need far fewer descriptors for the same load. That is a reason to remeasure after a client library upgrade rather than to keep an old budget.

Count the outbound side under failure, not under normal latency. When an upstream slows down, requests wait longer, more of them are in flight, and the pool grows to its maximum and stays there. The budget should use the configured maximums, which is what the process will hold on its worst evening.

Verify what the process really has

Configuration is a claim. The kernel's view is in /proc/<pid>/limits:

$ grep 'Max open files' /proc/$(systemctl show -p MainPID --value catalog-api)/limits
Max open files            1024                 524288               files

Read it for the main process after startup, not for your shell: your login shell's ulimit -n has nothing to do with the service. Count the descriptors in use with ls /proc/<pid>/fd | wc -l and export that number as a metric, alongside the soft limit, so a dashboard can show the ratio. The Prometheus guidelines for client libraries list process_open_fds and process_max_fds among the standard process metrics, so a client library that implements them gives you both numbers without extra code.

Several things can change the numbers between hosts:

  • distributions and administrators can change DefaultLimitNOFILE= in systemd's system.conf;
  • containers get their limits from the container runtime's configuration, not from the host's systemd;
  • LimitNOFILE=infinity is capped by the kernel's fs.nr_open, whose documented default is 1048576;
  • a process started by a supervisor, a cron job or su inherits that parent's limits.

Failure modes

  • Leaks look like limits. If the open count climbs steadily without traffic growth, the fix is closing descriptors, not raising the limit. Sockets stuck in CLOSE_WAIT are a common cause: the peer closed and the application never did.
  • Accept loops that spin. When accept fails with EMFILE, the pending connection stays in the backlog, the listening socket stays readable, and a naive event loop retries at full CPU. Servers handle this differently; check that yours backs off.
  • Raising the soft limit past a select() user. A native library that uses select() will fail once descriptors above 1023 are handed out. If you raise the limit for such a process, test the library under load.

How to verify a change

After changing a unit or a program, restart the service and read /proc/<pid>/limits. Then run a load test that opens the budgeted number of connections plus a margin, and watch process_open_fds against the limit. Finally, kill one upstream so its pool sockets hang, and confirm the service degrades with timeouts rather than EMFILE.

Primary references and authorship

Sources checked 2026-11-29. Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/nofile_limit_planner. Views are my own and do not represent any employer, past or present.