Linux EMFILE/ENFILE (errno 24/23) — Too Many Open Files
About Linux EMFILE/ENFILE (errno 24/23)
Linux EMFILE and ENFILE errors occur when a process or the entire system has reached its maximum limit of open file descriptors. This guide covers everything you need to know about this topic, including common causes, step-by-step solutions, and answers to frequently asked questions.
Here are the key things to understand: EMFILE (errno 24): per-process file descriptor limit reached (ulimit -n, default 1024). ENFILE (errno 23): system-wide file descriptor limit reached (fs.file-max). File descriptors include files, sockets, pipes, and device handles. Network servers can exhaust file descriptors with many concurrent connections. Docker containers inherit ulimits from the host or can set their own. Understanding these fundamentals will help you diagnose and resolve this issue more effectively.
The most common reasons this occurs include: Application not closing file descriptors (file descriptor leak). Too many concurrent network connections (each socket is a file descriptor). Per-process limit (ulimit -n) set too low for the application's needs. System-wide limit (fs.file-max) too low for high-connection servers. File descriptor leak in long-running daemons accumulating over time. Identifying the root cause is the first step toward finding the right solution.
To resolve this, follow these recommended steps: Check current limits: ulimit -n (per-process) and cat /proc/sys/fs/file-max (system-wide). Check current usage: ls /proc/PID/fd | wc -l to count open FDs for a specific process. Increase per-process limit: ulimit -n 65536 or edit /etc/security/limits.conf. Increase system limit: echo 'fs.file-max = 1000000' >> /etc/sysctl.conf && sysctl -p. For systemd services: add LimitNOFILE=65536 in the service unit file. If these steps do not resolve the issue, consider consulting additional resources or a qualified professional.
This article is part of our Linux Error Codes collection on Error Codes Wiki. We provide comprehensive, up-to-date information to help you find solutions quickly.
Quick Answer
How do I find file descriptor leaks?
Track FD count over time: watch 'ls /proc/PID/fd | wc -l'. If it increases continuously, the process has a leak. Use lsof -p PID to see what is open.
Overview
Linux EMFILE and ENFILE errors occur when a process or the entire system has reached its maximum limit of open file descriptors.
Key Details
- EMFILE (errno 24): per-process file descriptor limit reached (ulimit -n, default 1024)
- ENFILE (errno 23): system-wide file descriptor limit reached (fs.file-max)
- File descriptors include files, sockets, pipes, and device handles
- Network servers can exhaust file descriptors with many concurrent connections
- Docker containers inherit ulimits from the host or can set their own
Common Causes
- Application not closing file descriptors (file descriptor leak)
- Too many concurrent network connections (each socket is a file descriptor)
- Per-process limit (ulimit -n) set too low for the application's needs
- System-wide limit (fs.file-max) too low for high-connection servers
- File descriptor leak in long-running daemons accumulating over time
Steps
- 1Check current limits: ulimit -n (per-process) and cat /proc/sys/fs/file-max (system-wide)
- 2Check current usage: ls /proc/PID/fd | wc -l to count open FDs for a specific process
- 3Increase per-process limit: ulimit -n 65536 or edit /etc/security/limits.conf
- 4Increase system limit: echo 'fs.file-max = 1000000' >> /etc/sysctl.conf && sysctl -p
- 5For systemd services: add LimitNOFILE=65536 in the service unit file