Skip to content

Commit c716ffd

Browse files
satazores128
authored andcommitted
Fix process not exiting calling .close() right after watching. (#600)
* Fix process not exiting calling .close() right after watching. * Attempt to fix failing tests on linux. * Fix tests on windows.
1 parent f03f332 commit c716ffd

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ FSWatcher.prototype.add = function(paths, _origAdd, _internal) {
624624
}.bind(this));
625625
}.bind(this), function(error, results) {
626626
results.forEach(function(item) {
627-
if (!item) return;
627+
if (!item || this.closed) return;
628628
this.add(sysPath.dirname(item), sysPath.basename(_origAdd || item));
629629
}, this);
630630
}.bind(this));

lib/fsevents-handler.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ function(path, transform, forceAdd, priorDepth) {
374374

375375
if (this.options.persistent && forceAdd !== true) {
376376
var initWatch = function(error, realPath) {
377+
if (this.closed) return;
377378
var closer = this._watchWithFsEvents(
378379
wh.watchPath,
379380
sysPath.resolve(realPath || wh.watchPath),

test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var sinon = require('sinon');
88
var rimraf = require('rimraf');
99
var fs = require('graceful-fs');
1010
var sysPath = require('path');
11+
var cp = require('child_process');
1112
chai.use(require('sinon-chai'));
1213
var os = process.platform;
1314

@@ -1820,6 +1821,22 @@ function runTests(baseopts) {
18201821
});
18211822
});
18221823
});
1824+
it('should not prevent the process from exiting', function(done) {
1825+
var scriptFile = getFixturePath('script.js');
1826+
var scriptContent = '\
1827+
var chokidar = require("' + __dirname.replace(/\\/g, '\\\\') + '");\n\
1828+
var watcher = chokidar.watch("' + scriptFile.replace(/\\/g, '\\\\') + '");\n\
1829+
watcher.close();\n\
1830+
process.stdout.write("closed");\n';
1831+
fs.writeFile(scriptFile, scriptContent, function (err) {
1832+
if (err) throw err;
1833+
cp.exec('node ' + scriptFile, function (err, stdout) {
1834+
if (err) throw err;
1835+
expect(stdout.toString()).to.equal('closed');
1836+
done();
1837+
});
1838+
});
1839+
});
18231840
});
18241841
describe('env variable option override', function() {
18251842
describe('CHOKIDAR_USEPOLLING', function() {

0 commit comments

Comments
 (0)