Failures and antipatterns · Intermediate

File descriptor exhaustion: reaching an open-resource limit

Distinguish per-process limits, system-wide exhaustion and leaked resource lifetimes.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. More than ordinary files
  2. Distinguish exhaustion scopes
  3. Explain the descriptor budget
  4. Test error-path cleanup
  5. Ask Logeia about this articleCheck your understanding

More than ordinary files

Linux file descriptors refer to more than regular disk files. Sockets, pipes and other kernel interfaces also consume descriptor entries. A web server that cannot accept new connections may therefore have exhausted descriptors through connection lifetimes rather than through ordinary file-reading code.

There are per-process descriptor limits and system-wide resource constraints. The exhausted scope determines the investigation. Do not respond to every “too many open files” symptom by changing the same setting without identifying which process, operation and resource limit produced the error.

Distinguish exhaustion scopes

EMFILE points toward the process’s descriptor limit, while ENFILE points toward a system-wide open-file constraint. Preserve the actual error and compare it with the affected process’s limits and descriptor inventory, for example through its /proc/PID/fd view. Scope is part of the diagnosis.

Consider creating a socket per request but failing to close it only on an exception path. Successful requests can appear healthy while repeated failures accumulate descriptors. Inspect ownership and cleanup on both success and failure; a test restricted to the normal path cannot establish bounded resource use.

Explain the descriptor budget

Break down the expected descriptor budget into connections, log files and other resources. Raising a limit can be appropriate capacity planning, but it can also merely postpone a failure if a leak continues. Explain why the new limit matches legitimate bounded demand.

Connection pools need maximum sizes and disposal policies too. Reuse can reduce repeated creation without making an unbounded pool safe. Observe open descriptors over time as load rises and falls, distinguishing a stable pool from a count that increases after every completed or failed request.

Test error-path cleanup

After the change, repeat connection errors, timeouts and cancellations as well as successful operations. Check that descriptor counts return to a baseline or stabilize under a documented pool bound. A lower count immediately after a restart does not verify that the cleanup path was repaired.

Inspecting another process can require privileges, and descriptor inventories can reveal paths or endpoint details. Keep observation scoped to the necessary workload and do not publish raw inventories unnecessarily. Resource diagnosis should establish ownership and lifetime without broadly increasing access or exposing unrelated process information.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 14

Choose a learning path