Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix process not exiting calling .close() right after watching. #600

Merged
merged 3 commits into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ FSWatcher.prototype.add = function(paths, _origAdd, _internal) {
}.bind(this));
}.bind(this), function(error, results) {
results.forEach(function(item) {
if (!item) return;
if (!item || this.closed) return;
this.add(sysPath.dirname(item), sysPath.basename(_origAdd || item));
}, this);
}.bind(this));
Expand Down
1 change: 1 addition & 0 deletions lib/fsevents-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ function(path, transform, forceAdd, priorDepth) {

if (this.options.persistent && forceAdd !== true) {
var initWatch = function(error, realPath) {
if (this.closed) return;
var closer = this._watchWithFsEvents(
wh.watchPath,
sysPath.resolve(realPath || wh.watchPath),
Expand Down
17 changes: 17 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var sinon = require('sinon');
var rimraf = require('rimraf');
var fs = require('graceful-fs');
var sysPath = require('path');
var cp = require('child_process');
chai.use(require('sinon-chai'));
var os = process.platform;

Expand Down Expand Up @@ -1820,6 +1821,22 @@ function runTests(baseopts) {
});
});
});
it('should not prevent the process from exiting', function(done) {
var scriptFile = getFixturePath('script.js');
var scriptContent = '\
var chokidar = require("' + __dirname.replace(/\\/g, '\\\\') + '");\n\
var watcher = chokidar.watch("' + scriptFile.replace(/\\/g, '\\\\') + '");\n\
watcher.close();\n\
process.stdout.write("closed");\n';
fs.writeFile(scriptFile, scriptContent, function (err) {
if (err) throw err;
cp.exec('node ' + scriptFile, function (err, stdout) {
if (err) throw err;
expect(stdout.toString()).to.equal('closed');
done();
});
});
});
});
describe('env variable option override', function() {
describe('CHOKIDAR_USEPOLLING', function() {
Expand Down