From 9b08a9ba5b2ea199af895e2eb1c5eee61e1c25a5 Mon Sep 17 00:00:00 2001 From: Pasi Tuominen Date: Wed, 24 Feb 2021 16:27:56 +0200 Subject: [PATCH 1/2] fix: change kill to signal the whole process group to terminate immediately --- src/node/BrowserRunner.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/node/BrowserRunner.ts b/src/node/BrowserRunner.ts index e4b4aaca9600e..44db707865438 100644 --- a/src/node/BrowserRunner.ts +++ b/src/node/BrowserRunner.ts @@ -183,9 +183,16 @@ export class BrowserRunner { // If the process failed to launch (for example if the browser executable path // is invalid), then the process does not get a pid assigned. A call to // `proc.kill` would error, as the `pid` to-be-killed can not be found. - if (this.proc && this.proc.pid && !this.proc.killed) { + if (this.proc && this.proc.pid && pidExists(this.proc.pid)) { try { - this.proc.kill('SIGKILL'); + if (process.platform === 'win32') { + childProcess.execSync(`taskkill /pid ${this.proc.pid} /T /F`); + } else { + // on linux the process group can be killed with the group id prefixed with + // a minus sign. The process group id is the group leader's pid. + const processGroupId = -this.proc.pid; + process.kill(processGroupId, 'SIGKILL'); + } } catch (error) { throw new Error( `${PROCESS_ERROR_EXPLANATION}\nError cause: ${error.stack}` @@ -294,3 +301,15 @@ function waitForWSEndpoint( } }); } + +function pidExists(pid: number): boolean { + try { + return process.kill(pid, 0); + } catch (error) { + if (error && error.code && error.code === 'ESRCH') { + return false; + } else { + throw error; + } + } +} From 7b5cccc5debabd5ea2a2e242448cb072bd0ae2d8 Mon Sep 17 00:00:00 2001 From: Jan Scheffler Date: Tue, 21 Sep 2021 12:59:36 +0200 Subject: [PATCH 2/2] chore: ignore taskkill errors --- src/node/BrowserRunner.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/BrowserRunner.ts b/src/node/BrowserRunner.ts index 44db707865438..19e0ed386dfd7 100644 --- a/src/node/BrowserRunner.ts +++ b/src/node/BrowserRunner.ts @@ -186,7 +186,7 @@ export class BrowserRunner { if (this.proc && this.proc.pid && pidExists(this.proc.pid)) { try { if (process.platform === 'win32') { - childProcess.execSync(`taskkill /pid ${this.proc.pid} /T /F`); + childProcess.exec(`taskkill /pid ${this.proc.pid} /T /F`, () => {}); } else { // on linux the process group can be killed with the group id prefixed with // a minus sign. The process group id is the group leader's pid.