Skip to content

Commit

Permalink
fix: wait for _all_ child processes to terminate (fixes issue remy#1476)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelkottmann committed Jun 7, 2019
1 parent 5124ae9 commit 9e7a2e2
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions lib/monitor/run.js
Expand Up @@ -15,6 +15,25 @@ var restart = null;
var psTree = require('pstree.remy');
var path = require('path');
var signals = require('./signals');
var oldKids;

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

function run(options) {
var cmd = config.command.raw;
Expand All @@ -25,7 +44,14 @@ function run(options) {
}

/*jshint validthis:true*/
restart = run.bind(this, options);
var runbinded = run.bind(this, options);

if (utils.isWindows) {
restart = runbinded;
} else {
restart = waitForOldKids.bind(this, runbinded);
}

run.restart = restart;

config.lastStarted = Date.now();
Expand Down Expand Up @@ -80,7 +106,7 @@ function run(options) {
var inBinPath = false;
try {
inBinPath = statSync(`${binPath}/${executable}`).isFile();
} catch (e) {}
} catch (e) { }

// hasStdio allows us to correctly handle stdin piping
// see: https://git.io/vNtX3
Expand Down Expand Up @@ -342,6 +368,7 @@ function kill(child, signal, callback) {
// note that psTree also works if `ps` is missing by looking in /proc
const sig = signal.replace('SIG', '');
psTree(child.pid, function (err, kids) {
oldKids = kids;
if (psTree.hasPS) {
spawn('kill', ['-s', sig, child.pid].concat(kids))
.on('close', callback);
Expand Down

0 comments on commit 9e7a2e2

Please sign in to comment.