Programming

Node.js worker_threads leak file descriptors when using fs.watch on large directories leading to EMFILE and process OOM

Environment

Environment

Node.js 16.20 app (Linux x86_64), heavy background processing using worker_threads; parent thread spawns workers that create fs.watch watchers on user-upload directories containing thousands of files; app runs in Docker container on Ubuntu 22.04

Symptoms

Symptoms

After sustained runtime the Node process exhausts file descriptors and memory grows until OOM or EMFILE errors appear; creating new worker threads fails; graceful shutdown hangs

Observations

Observations

lsof shows thousands of inotify fd entries created by workers; /proc/<pid>/limits shows fd count increasing over time; worker thread exit handlers sometimes not invoked; explicit watcher.close() is invoked in code paths, but after rapid restarts watchers remain in kernel list; increasing ulimit temporarily postpones failure

Already attempted

Already attempted

Added explicit watcher.close() in error/finally blocks, upgraded Node minor versions, moved some watchers to parent thread, increased fs.inotify.max_user_watches and ulimit -n; behavior improves but leak still accrues under high churn

Question

Question

How to eliminate the fd/inotify watcher leak and design a robust watcher strategy with worker threads for directories with thousands of files?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 4w ago• Trouble ID

Most likely causes

Creating many fs.watch (inotify) watchers across worker threads exceeds kernel inotify resources and races leave watchers orphaned 90%
Node's watcher objects are garbage-collected without explicit close during worker termination, leaving kernel fds until process exit 55%
Underlying filesystem (NFS/overlayfs in container) does not support inotify reliably, forcing fallback that leaks handles under churn 30%

Summary

This looks like both an API misuse and an environmental limitation. fs.watch maps to inotify on Linux and creating many watchers (especially across worker threads) consumes kernel inotify resources and fds; rapid creation/destruction can expose race conditions where watchers are not fully closed before worker termination, leaving kernel resources until process exit. Node's fs.FSWatcher must be closed, and worker_threads sharing of watchers can be problematic. Using a single watcher with event aggregation or a polling-based watcher (chokidar with polling) for network/massive trees, and reusing watchers across workers avoids large fd counts. Also ensure workers are terminated gracefully and join before watchers are closed.

Suggested Fixes

Diagnostic step 1
Refactor: use a single watcher (or a small pool) in the parent thread that broadcasts events to workers, avoiding per-worker watchers.
Diagnostic step 2
Use a battle-tested library such as chokidar with polling when watching very large trees or on non-local filesystems; configure usePolling and interval.
Diagnostic step 3
Ensure every fs.FSWatcher.close() is called synchronously before terminating a worker; add explicit handshake to confirm close completion.

Continue troubleshooting

Matched from the title, description and tags