Skip to content

Commit

Permalink
fix: wait for all subprocesses to terminate (fixes issue #1476)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelkottmann authored and remy committed Nov 20, 2019
1 parent b58cf7d commit 0e6ba3c
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions lib/monitor/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ var psTree = require('pstree.remy');
var path = require('path');
var signals = require('./signals');

function waitForSubprocesses(subprocesses, callback) {
if (Array.isArray(subprocesses) && subprocesses.length > 0) {
// check if all old subprocesses have been terminated
exec('kill -0 ' + subprocesses.join(' '), (error) => {
const returnCode = error ? error.code : 0;
if (returnCode < 126) { // ignore command not found error
const stillRunning = subprocesses.length - returnCode;
if (stillRunning > 0) {
utils.log.status('still waiting for ' + stillRunning +
' subprocess(es) to finish...');
setTimeout(waitForSubprocesses.bind(this, subprocesses, callback),
100);
return;
}
}
callback();
});
} else {
callback();
}
}

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

Expand Down Expand Up @@ -341,17 +363,24 @@ function kill(child, signal, callback) {
// configured signal (default: SIGUSR2) signal, which fixes #335
// note that psTree also works if `ps` is missing by looking in /proc
const sig = signal.replace('SIG', '');
psTree(child.pid, function (err, kids) {
psTree(child.pid, function (err, subprocesses) {
if (psTree.hasPS) {
spawn('kill', ['-s', sig, child.pid].concat(kids))
.on('close', callback);
spawn('kill', ['-s', sig].concat(subprocesses))
.on('close', waitForSubprocesses.bind(this, subprocesses,
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 = subprocesses.slice().sort();
pids.forEach(pid => {
exec('kill -' + signals[signal] + ' ' + pid, () => { });
});
callback();
waitForSubprocesses(subprocesses, function () {
exec('kill -' + signals[signal] + ' ' + child.pid, () => { });
callback();
})
}
});

Expand Down

0 comments on commit 0e6ba3c

Please sign in to comment.