Node.js/Express app hits EMFILE under high concurrency — many sockets/files left open
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
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
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
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
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?
Verified community troubleshooting
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.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Programming29% match
Node.js/Express server CPU spikes and event loop stalls during concurrent multipart image uploads due to synchronous image processing
Open0 answers - Web Development28% match
Next.js API processes hit 'Too many open files' under load behind PM2 — 500s only in production
Open0 answers - Linux23% match
Docker containers report 'no space left on device' after systemd tmpfiles cleaned /var/lib/docker metadata despite free disk space
Open1 answer - Cloud Computing22% match
Docker image pulls fail with 'no space left on device' though df shows plenty of disk — overlayfs inode exhaustion on Ubuntu 22.04
Open1 answer - Web Development21% match
NGINX returns 504 Gateway Timeout for long-running uploads through a Node backend — how to diagnose?
Open1 answer