joyent/node — Issue #2479: EMFILE error when using fs.watch on many items
Author: TrevorBurnham
Created: 2012-01-06 22:50:22+00:00
State: open

Test case:

var fs = require('fs');

for (var i = 1; i <= 500; i++) {
fs.writeFileSync("" + i + ".tmp", '');
}

for (i = 1; i <= 500; i++) {
try {
fs.watch("" + i + ".tmp", function() {});
} catch (e) {
console.log(i);
process.exit(1);
}
}

With Node 0.6.2 under OS X, I consistently get output of `250`, meaning that 249 files were successfully watched, but an `EMFILE` error occurred when trying to watch the 250th. The pertinent part of the stack trace is

Error: watch EMFILE
at errnoException (fs.js:605:11)
at FSWatcher.start (fs.js:632:11)
at Object.watch (fs.js:660:11)

Several folks have experienced the error when using CoffeeScript's watch mode, particularly in a directory that's a git repository (see [CoffeeScript issue #1537](https://github.com/jashkenas/coffee-script/issues/1537)).

Adding a 100ms interval between each file, I get the same result. So it seems that there's a hard limit on how many files one can watch simultaneously from a Node process on some systems. Is there some way that we can increase or avoid this limit? Pinging @bnoordhuis.