Skip to content

Commit

Permalink
Merge pull request #732 from srguiwiz/fix-issue-730-better
Browse files Browse the repository at this point in the history
Prevent watcher.close() crashing - fix Issue #730
  • Loading branch information
es128 committed Jun 14, 2018
2 parents f0ffb1d + 2a2bcf2 commit 9ac1ffd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/nodefs-handler.js
Expand Up @@ -87,6 +87,7 @@ function setFsWatchListener(path, fullPath, options, handlers) {
if (!watcher) return;
var broadcastErr = fsWatchBroadcast.bind(null, fullPath, 'errHandlers');
watcher.on('error', function(error) {
container.watcherUnusable = true; // documented since Node 10.4.1
// Workaround for https://github.com/joyent/node/issues/4337
if (process.platform === 'win32' && error.code === 'EPERM') {
fs.open(path, 'r', function(err, fd) {
Expand Down Expand Up @@ -118,7 +119,9 @@ function setFsWatchListener(path, fullPath, options, handlers) {
delete container.errHandlers[listenerIndex];
delete container.rawEmitters[listenerIndex];
if (!Object.keys(container.listeners).length) {
container.watcher.close();
if (!container.watcherUnusable) { // check to protect against issue #730
container.watcher.close();
}
delete FsWatchInstances[fullPath];
}
};
Expand Down
21 changes: 21 additions & 0 deletions test.js
Expand Up @@ -7,6 +7,7 @@ var should = chai.should();
var sinon = require('sinon');
var rimraf = require('rimraf');
var fs = require('graceful-fs');
var rimraf = require('rimraf');
var sysPath = require('path');
var upath = require("upath");
var cp = require('child_process');
Expand Down Expand Up @@ -464,6 +465,26 @@ function runTests(baseopts) {
});
});
});
it('should emit two `unlinkDir` event when two nested directories were removed', function(done) {
var spy = sinon.spy();
var testDir = getFixturePath('subdir');
var testDir2 = getFixturePath('subdir/subdir2');
var testDir3 = getFixturePath('subdir/subdir2/subdir3');
fs.mkdirSync(testDir, 0x1ed);
fs.mkdirSync(testDir2, 0x1ed);
fs.mkdirSync(testDir3, 0x1ed);
watcher.on('unlinkDir', spy).on('ready', function() {
rimraf(testDir2, simpleCb); // test removing in one
waitFor([spy], function() {
spy.should.have.been.calledWith(testDir2);
spy.should.have.been.calledWith(testDir3);
expect(spy.args[0][1]).to.not.be.ok; // no stats
rawSpy.should.have.been.called;
if (!osXFsWatch010) spy.should.have.been.calledTwice;
done();
});
});
});
it('should emit `unlink` and `add` events when a file is renamed', function(done) {
var unlinkSpy = sinon.spy(function unlink(){});
var addSpy = sinon.spy(function add(){});
Expand Down

0 comments on commit 9ac1ffd

Please sign in to comment.