joyent/node — Issue #2105: Multiple fs.watch-es on the same file cause process to crash
Author: TrevorBurnham
Created: 2011-11-14 18:01:28+00:00
State: open

Running 0.6.1 under OS X, I find that I can't `fs.watch` a file, close that `FSWatcher`, and then later `fs.watch` it again. Here's a test case that does that (with an arbitrary delay between each step to avoid timing issues):

var fs = require('fs');
var delay = function(func) {
setTimeout(func, 50);
};

fs.writeFileSync('test.txt', '');
delay(function() { // after a delay, watch the file
var fswatcher1 = fs.watch('test.txt', function() {
console.log(arguments);
});
delay(function() { // after a delay, stop watching
fswatcher1.close();
delay(function() { // after a delay, watch it again
fs.watch('test.txt', function() {
console.log(arguments);
});
delay(function() { // after a delay, modify the file
fs.writeFileSync('test.txt', 'second write');
});
});
});
});

The bug isn't deterministic; running this file, about 50% the time I get

Assertion failed: (wrap->object_.IsEmpty() == false), function OnEvent, file ../src/fs_event_wrap.cc, line 134.
Abort trap: 6

(Note that this assertion comes from a different file than the one in #2094.) About 25% of the time, I get

Segmentation fault: 11

And the other 25% of the time, I just get no output (meaning that the second `fs.watch` failed to report the change event).