Skip to content

Commit bf9b7a6

Browse files
committed
fix: if no ps, walk /proc to kill child fully
Originally used ps-tree which relied on `ps` on *nix. But if the system didn't have `ps` at all, we'd try to kill the child process, but alas this would not always work, as we're spawning `sh` and _then_ node, so the kill would only kill the `sh` process, and not the running node process. The new @remy/pstree lib sniffs for `ps` and defers to ps-tree, otherwise it will walk /proc and map the PPID to the child process allowing nodemon to fully clean up.
1 parent 10ded94 commit bf9b7a6

File tree

3 files changed

+27
-33
lines changed

3 files changed

+27
-33
lines changed

lib/monitor/run.js

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,9 @@ var child = null; // the actual child process we spawn
1111
var killedAfterChange = false;
1212
var noop = function () { };
1313
var restart = null;
14-
var psTree = require('ps-tree');
15-
var hasPS = true;
14+
var psTree = require('@remy/pstree');
1615
var path = require('path');
1716

18-
// discover if the OS has `ps`, and therefore can use psTree
19-
exec('ps', function (error) {
20-
if (error) {
21-
hasPS = false;
22-
}
23-
});
24-
2517
function run(options) {
2618
var cmd = config.command.raw;
2719

@@ -291,23 +283,17 @@ function kill(child, signal, callback) {
291283
exec('taskkill /pid ' + child.pid + ' /T /F');
292284
callback();
293285
} else {
294-
if (hasPS) {
295-
// we use psTree to kill the full subtree of nodemon, because when
296-
// spawning processes like `coffee` under the `--debug` flag, it'll spawn
297-
// it's own child, and that can't be killed by nodemon, so psTree gives us
298-
// an array of PIDs that have spawned under nodemon, and we send each the
299-
// configured signal (defaul: SIGUSR2) signal, which fixes #335
300-
psTree(child.pid, function (err, kids) {
301-
spawn('kill', ['-s', signal, child.pid].concat(kids.map(function (p) {
302-
return p.PID;
303-
}))).on('close', callback);
304-
});
305-
} else {
306-
exec('kill -s ' + signal + ' ' + child.pid, function () {
307-
// ignore if the process has been killed already
308-
callback();
309-
});
310-
}
286+
// we use psTree to kill the full subtree of nodemon, because when
287+
// spawning processes like `coffee` under the `--debug` flag, it'll spawn
288+
// it's own child, and that can't be killed by nodemon, so psTree gives us
289+
// an array of PIDs that have spawned under nodemon, and we send each the
290+
// configured signal (default: SIGUSR2) signal, which fixes #335
291+
// note that psTree also works if `ps` is missing by looking in /proc
292+
psTree(child.pid, function (err, kids) {
293+
spawn('kill', ['-s', signal, child.pid].concat(kids.map(function (p) {
294+
return p.PID;
295+
}))).on('close', callback);
296+
});
311297
}
312298
}
313299

package-lock.json

Lines changed: 14 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@
5252
"should": "~4.0.0"
5353
},
5454
"dependencies": {
55+
"@remy/pstree": "^1.0.0",
5556
"chokidar": "^1.7.0",
5657
"debug": "^2.6.8",
5758
"es6-promise": "^3.3.1",
5859
"ignore-by-default": "^1.0.1",
5960
"lodash.defaults": "^3.1.2",
6061
"minimatch": "^3.0.4",
61-
"ps-tree": "^1.1.0",
6262
"touch": "^3.1.0",
6363
"undefsafe": "0.0.3",
6464
"update-notifier": "^2.3.0"

0 commit comments

Comments
 (0)