FULL AI ANALYSIS

Node.js/Express app hits EMFILE under high concurrency — many sockets/files left open

Generated Oct 20, 2026, 9:41:02 AM

Analysis summary

This type of EMFILE pattern is typically a code-level resource leak: file streams or socket streams not closed on all paths (most likely), especially when using manual stream piping or custom error handling. Third-party middleware (multer, custom proxy clients) that don't properly destroy streams on error is a common contributor. OS ulimit increases merely postpone failure. Less likely causes are a Node core bug or socket keep-alive misconfiguration causing many half-open sockets.

Possible causes

Application code not closing/destroying file streams or response streams on error paths (e.g., missing handlers for 'error' or 'aborted')

85%

Third-party middleware (file upload/proxy library) leaking streams or not cleaning temp files on abort

60%

Too many long‑lived keep-alive connections exhausting FD table under high concurrency

40%

OS/kernel bug or misconfigured container ulimits (less likely here since raising ulimit only deferred failure)

15%

Suggested fixes

1

Diagnostic step 1

Instrument FD growth: run lsof -p <PID> | wc -l over a reproducer to confirm leak rate and capture lsof output to identify FD types and paths.

2

Diagnostic step 2

Audit upload/streaming handlers and replace manual piping with stream.pipeline(...) which auto-closes on errors; ensure you attach 'error' and 'aborted' handlers that call stream.destroy().

3

Diagnostic step 3

Wrap third-party streaming libraries with a small wrapper that ensures .destroy() is called on request 'close' and 'error'.

4

Diagnostic step 4

Temporarily enable more conservative keepAliveTimeout or disable keep-alive in the HTTP server to reduce FD retention while debugging.

5

Diagnostic step 5

Deploy changes to a staging environment and verify with a concurrency load test; monitor open FD count and ensure it stabilizes.