Error Codes Wiki

Linux EMFILE/ENFILE (errno 24/23) — Too Many Open Files

Errorerrno

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

  1. 1Check current limits: ulimit -n (per-process) and cat /proc/sys/fs/file-max (system-wide)
  2. 2Check current usage: ls /proc/PID/fd | wc -l to count open FDs for a specific process
  3. 3Increase per-process limit: ulimit -n 65536 or edit /etc/security/limits.conf
  4. 4Increase system limit: echo 'fs.file-max = 1000000' >> /etc/sysctl.conf && sysctl -p
  5. 5For systemd services: add LimitNOFILE=65536 in the service unit file

Tags

linuxemfileenfilefile-descriptorsulimit

More in Errno

Frequently Asked Questions

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.