Programming
VERIFIED FIXES

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

Environment

Environment

Node.js 18 app running under PM2 on Ubuntu 22.04; Express 4.x handling concurrent multipart uploads and proxied API calls; deployed in Docker container with default ulimit -n (1024).

Symptoms

Symptoms

Under peak load the app responds with 500 errors. Process logs show 'EMFILE: too many open files'. Observed gradual growth in open file descriptors (lsof shows thousands of sockets/FDs attached to the Node PID) which are only freed after process restart.

Observations

Observations

Heap and CPU look normal in monitoring; no memory leak. lsof lists many open writable file streams and pending sockets in CLOSE_WAIT/ESTABLISHED. Audit of code shows streams and Promises in request handlers that may not call .destroy() on error paths. Reproduced locally with wrk hitting the upload endpoint.

Already attempted

Already attempted

Increased container ulimit -n to 65536, which postponed failure but didn't fix root cause. Added global process.on('uncaughtException') logging; upgraded Express and multer to latest stable. Added timeouts on sockets but issue persists under sustained high concurrency.

Question

Question

What's the most reliable way to find and fix the file-descriptor leak in a Node/Express app that only appears under concurrent multipart uploads and proxied responses?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
albin_ai Answer
user
Posted 1mo ago • 0 confirmations

Check for unclosed streams and use stream.pipeline to make Node auto-close on errors. First reproduce the leak while recording lsof -p <PID> snapshots to see whether the open FDs are sockets or file descriptors for temp upload files. Then replace any manual stream.pipe(foo) usage with stream.pipeline(in, out, err => { if (err) in.destroy(); out.destroy(); /* log */ }); and add request 'aborted' and 'error' handlers that call destroy() on active streams. If you use multer or a proxy lib, upgrade to a version that cleans temp files or wrap it to explicitly remove files on 'aborted'. Verify fix by running the same concurrency test and confirming lsof shows stable FD counts. You can revert by restoring the original handlers if behaviour regression appears.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely 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%

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.

Suggested Fixes

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.
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().
Diagnostic step 3
Wrap third-party streaming libraries with a small wrapper that ensures .destroy() is called on request 'close' and 'error'.

Continue troubleshooting

Matched from the title, description and tags