Skip to content

Commit

Permalink
fix: wait for all child processes to terminate (fixes issue #1476)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelkottmann committed Jul 3, 2019
1 parent 5124ae9 commit adc7c5d
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions lib/monitor/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ var psTree = require('pstree.remy');
var path = require('path');
var signals = require('./signals');

function waitForOldKids(oldKids, callback) {
if (oldKids) {
// check if all previous kids have been terminated
exec('kill -0 ' + oldKids.join(' '), (error) => {
const returnCode = error ? error.code : 0;
if (returnCode < 126) { // ignore command not found error
const stillRunningKids = oldKids.length - returnCode;
if (stillRunningKids > 0) {
utils.log.status('still waiting for ' + stillRunningKids +
' child process(es) to finish...');
setTimeout(waitForOldKids.bind(this, oldKids, callback), 100);
return;
}
}
callback();
});
} else {
callback();
}
}

function run(options) {
var cmd = config.command.raw;

Expand Down Expand Up @@ -343,15 +364,21 @@ function kill(child, signal, callback) {
const sig = signal.replace('SIG', '');
psTree(child.pid, function (err, kids) {
if (psTree.hasPS) {
spawn('kill', ['-s', sig, child.pid].concat(kids))
.on('close', callback);
spawn('kill', ['-s', sig].concat(kids))
.on('close', waitForOldKids.bind(this, kids, function () {
spawn('kill', ['-s', sig, child.pid])
.on('close', callback);
}));
} else {
// make sure we kill from smallest to largest
const pids = kids.concat(child.pid).sort();
const pids = kids.slice().sort();
pids.forEach(pid => {
exec('kill -' + signals[signal] + ' ' + pid, () => { });
});
callback();
waitForOldKids(kids, function () {
exec('kill -' + signals[signal] + ' ' + child.pid, () => { });
callback();
})
}
});

Expand Down

0 comments on commit adc7c5d

Please sign in to comment.