Skip to content

Commit

Permalink
fix: optimize windows-kill execution and silence wmic errors (remy#1720)
Browse files Browse the repository at this point in the history
This commit silences all errors from the wmic.exe process. Which is save,
because the error case is handled by falling back to the child.pid.

It also optimizes the windows-kill invocation by minimizing (hiding)
the terminal window and keeping it open until the process has finished.
  • Loading branch information
countzero committed Apr 12, 2021
1 parent ee99a83 commit 1424afc
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions lib/monitor/run.js
Expand Up @@ -5,6 +5,7 @@ var bus = utils.bus;
var childProcess = require('child_process');
var spawn = childProcess.spawn;
var exec = childProcess.exec;
var execSync = childProcess.execSync;
var fork = childProcess.fork;
var watch = require('./watch').watch;
var config = require('../config');
Expand Down Expand Up @@ -313,25 +314,37 @@ function kill(child, signal, callback) {
if (utils.isWindows) {

// We are using the Windows Management Instrumentation Command-line
// (wmic) to resolve the sub-child process identifier, because the
// (wmic.exe) to resolve the sub-child process identifier, because the
// 'child' process in this context is actually a cmd.exe wrapper.
// We want to send the termination signal directly to the node process.
const resultBuffer = childProcess.execSync(
`wmic process where (ParentProcessId=${child.pid}) get ProcessId`
// The '2> nul' silences the no process found error message.
const resultBuffer = execSync(
`wmic process where (ParentProcessId=${child.pid}) get ProcessId 2> nul`
);
const result = resultBuffer.toString().match(/^[0-9]+/m);

// If there is no sub-child process we fall back to the child process.
const processId = Array.isArray(result) ? result[0] : child.pid;

debug('sending kill signal SIGINT to process: %s', processId);

// We are using the standalone 'windows-kill' executable to send the
// standard POSIX signal 'SIGINT' to the process group. This fixes #1720.
// standard POSIX signal 'SIGINT' to the node process. This fixes #1720.
const windowsKill = path.normalize(
`${process.cwd()}/node_modules/nodemon/bin/windows-kill.exe`
);

// We have to detach the 'windows-kill' execution completely from this
// process group to avoid terminating the nodemon process itself.
// See: https://github.com/alirdn/windows-kill#how-it-works--limitations
//
// We have to detach this binary execution completely via 'start',
// because otherwise the nodemon process itself would be terminated:
// https://github.com/alirdn/windows-kill#how-it-works--limitations
const windowsKill = path.normalize(`${process.cwd()}/node_modules/nodemon/bin/windows-kill.exe`);
exec(`start "" ${windowsKill} -SIGINT ${processId}`, callback);
// Therefore we are using 'start' to create a new cmd.exe context.
// The '/min' option hides the new terminal window and the '/wait'
// option lets the process wait for the command to finish.
execSync(
`start "windows-kill" /min /wait "${windowsKill}" -SIGINT ${processId}`
);
callback();

} else {
// we use psTree to kill the full subtree of nodemon, because when
Expand Down

0 comments on commit 1424afc

Please sign in to comment.