Founding prices until 15 December: 30% off Pro, locked for life. See pricing

$ … Too many open files

'Too many open files' (EMFILE): raising the limit the right way

The process hit its limit on open file descriptors (files, sockets, pipes).

Linux

What it means

Each process has a limit on open file descriptors (often 1024 by default). Busy servers open a socket per connection and hit it. It can also be a leak: descriptors opened and never closed.

Causes and fixes

Most likely first. Run the check, and if it matches, apply the fix.

1. The limit is too low for the workload

Check:

cat /proc/<pid>/limits | grep 'open files'
ls /proc/<pid>/fd | wc -l

Fix: For a systemd service, set LimitNOFILE= in a drop-in (systemctl edit <unit>); /etc/security/limits.conf doesn't apply to services.

2. A descriptor leak

Check:

ls -l /proc/<pid>/fd | awk '{print $NF}' | sort | uniq -c | sort -n | tail

Fix: Close files and sockets in the code; a higher limit only delays the crash.

Fix it yourself

A live machine in your browser with this problem already on it. The grader checks the result.

Open the lab

No account needed for your first tries.