Skip to content

Commit 0eb9c78

Browse files
petuominjschfflr
andauthored
fix: change kill to signal the whole process group to terminate (#6859)
* fix: change kill to signal the whole process group to terminate immediately * chore: ignore taskkill errors Co-authored-by: Jan Scheffler <[email protected]>
1 parent 6970a97 commit 0eb9c78

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/node/BrowserRunner.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,16 @@ export class BrowserRunner {
183183
// If the process failed to launch (for example if the browser executable path
184184
// is invalid), then the process does not get a pid assigned. A call to
185185
// `proc.kill` would error, as the `pid` to-be-killed can not be found.
186-
if (this.proc && this.proc.pid && !this.proc.killed) {
186+
if (this.proc && this.proc.pid && pidExists(this.proc.pid)) {
187187
try {
188-
this.proc.kill('SIGKILL');
188+
if (process.platform === 'win32') {
189+
childProcess.exec(`taskkill /pid ${this.proc.pid} /T /F`, () => {});
190+
} else {
191+
// on linux the process group can be killed with the group id prefixed with
192+
// a minus sign. The process group id is the group leader's pid.
193+
const processGroupId = -this.proc.pid;
194+
process.kill(processGroupId, 'SIGKILL');
195+
}
189196
} catch (error) {
190197
throw new Error(
191198
`${PROCESS_ERROR_EXPLANATION}\nError cause: ${error.stack}`
@@ -294,3 +301,15 @@ function waitForWSEndpoint(
294301
}
295302
});
296303
}
304+
305+
function pidExists(pid: number): boolean {
306+
try {
307+
return process.kill(pid, 0);
308+
} catch (error) {
309+
if (error && error.code && error.code === 'ESRCH') {
310+
return false;
311+
} else {
312+
throw error;
313+
}
314+
}
315+
}

0 commit comments

Comments
 (0)