utils/processUtils: check if the process is running before killing
Check ChildProcess.exitCode before starting to kill the process.
According to https://nodejs.org/api/child_process.html#child_process_subprocess_exitcode
this should be null if it's still running.
(note the exitCode can be still null after a process terminates.)
And remove some logging messages that looked too verbose and
flooded the console.
Fixes golang/vscode-go#334
Change-Id: I04161806ba6160f9152ce17618b38f96f82b732c
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/244359
Reviewed-by: Eli Bendersky <eliben@google.com>
diff --git a/src/utils/processUtils.ts b/src/utils/processUtils.ts
index 5c6d343..ddd12ae 100644
--- a/src/utils/processUtils.ts
+++ b/src/utils/processUtils.ts
@@ -13,16 +13,13 @@
if (!logger) {
logger = console.log;
}
- if (!p || !p.pid) {
- logger(`no process to kill`);
+ if (!p || !p.pid || p.exitCode !== null) {
return Promise.resolve();
}
return new Promise((resolve) => {
kill(p.pid, (err) => {
if (err) {
logger(`Error killing process ${p.pid}: ${err}`);
- } else {
- logger(`killed process ${p.pid}`);
}
resolve();
});
@@ -39,11 +36,11 @@
// See https://go-review.googlesource.com/c/vscode-go/+/242518/ for more
// details and background.
export function killProcess(p: ChildProcess) {
- if (p) {
+ if (p && p.pid && p.exitCode === null) {
try {
p.kill();
} catch (e) {
- console.log('Error killing process: ' + e);
+ console.log(`Error killing process ${p.pid}: ${e}`);
}
}
}