mirror of
https://github.com/SrIzan10/nodemon.git
synced 2026-05-01 10:55:09 +00:00
chore: Merge branch 'pepe79-master'
* pepe79-master: fix: wait for all subprocesses to terminate (fixes issue #1476)
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user