How to fix 'Too many open files' (EMFILE) before it takes your site down: ulimit, systemd and the leak behind it
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…
Too many open files, typically signaled as EMFILE, can bring down a website when a single process hits its file descriptor limit. This error occurs when handling many concurrent connections, as each connection consumes a file descriptor. Linux systems have a default limit of 1,024 descriptors per process, which is often insufficient for high-traffic applications. Two primary issues usually cause this error: a limit that's too low and a process utilizing more descriptors than necessary.
To diagnose and resolve the issue, first, check the actual limit of the running process using the ulimit -n command. This shows the process's limit, which may differ from the system-wide default. Next, inspect the process using the grep 'open files' /proc/$pid/limits command, replacing $pid with the process ID of the service (e.g., nginx, Gunicorn, or PHP-FPM).
If the number of open files closely matches the limit, the problem has been identified. In multi-worker setups (e.g., Nginx, Gunicorn, PHP-FPM), it's crucial to examine individual workers rather than just the master process, as the limit applies per process.
To raise the limit correctly, the method depends on how the process is started. For systemd services, the /etc/security/limits.conf file does not apply to services, so it must be configured within the unit file. Use the following command to edit the unit file: sudo systemctl edit myapp. Then, add the following line inside the [Service] section: LimitNOFILE=65536. Reload the daemon with sudo systemctl daemon-reload, restart the service with sudo systemctl restart myapp, and verify the new limit with /proc/$pid/limits.
When working with Nginx, it has its own setting for workers, which should be set in the Nginx configuration file: worker_rlimit_nofile 65536; events { worker_connections 16384; }. Each proxied connection consumes two descriptors, so worker_connections should be below half of worker_rlimit_nofile. For Docker containers, the limit comes from the daemon default or the --ulimit nofile=65536:65536 option.
Check the container's limit using cat /proc/1/limits. The system-wide ceiling is fs.file-max, which is typically high on modern kernels and rarely the problem, but if cat /proc/sys/fs/file-nr shows the first number near its maximum, the entire machine is affected.
To identify what's using the descriptors, inspect the file descriptors associated with the process. Use ls -l /proc/$pid/...
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to