Skip to content

Commit f157443

Browse files
fix: wait for _all_ child processes to terminate (fixes issue #1476)
1 parent 5124ae9 commit f157443

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

Diff for: lib/monitor/run.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,28 @@ var restart = null;
1515
var psTree = require('pstree.remy');
1616
var path = require('path');
1717
var signals = require('./signals');
18+
var oldKids;
19+
20+
function waitForOldKids(callback) {
21+
if (oldKids) {
22+
// check if all previous kids have been terminated
23+
exec('kill -0 ' + oldKids.join(' '), (error) => {
24+
const returnCode = error ? error.code : 0;
25+
if (returnCode < 126) { // ignore command not found error
26+
const stillRunningKids = oldKids.length - returnCode;
27+
if (stillRunningKids > 0) {
28+
utils.log.status('still waiting for ' + stillRunningKids +
29+
' child process(es) to finish...');
30+
setTimeout(waitForOldKids.bind(this, callback), 100);
31+
return;
32+
}
33+
}
34+
callback();
35+
});
36+
} else {
37+
callback();
38+
}
39+
}
1840

1941
function run(options) {
2042
var cmd = config.command.raw;
@@ -25,7 +47,14 @@ function run(options) {
2547
}
2648

2749
/*jshint validthis:true*/
28-
restart = run.bind(this, options);
50+
var runbinded = run.bind(this, options);
51+
52+
if (utils.isWindows) {
53+
restart = runbinded;
54+
} else {
55+
restart = waitForOldKids.bind(this, runbinded);
56+
}
57+
2958
run.restart = restart;
3059

3160
config.lastStarted = Date.now();
@@ -80,7 +109,7 @@ function run(options) {
80109
var inBinPath = false;
81110
try {
82111
inBinPath = statSync(`${binPath}/${executable}`).isFile();
83-
} catch (e) {}
112+
} catch (e) { }
84113

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

0 commit comments

Comments
 (0)