Skip to content

Commit 366503b

Browse files
committed
fix: occasional runtime errors when killing processes
Fixes an error I saw while debugging where if the `exit` event emits quickly, we would get a `Cannot access <name> before initialization` error.
1 parent f406ef9 commit 366503b

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

runner/utils/kill-gracefully.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ export function killChildProcessGracefully(
2828
throw new Error(`No process ID for processed that should be killed.`);
2929
}
3030

31-
// Watch for exiting, so that we can cancel the timeout(s) then.
31+
// Declare the variables here, because in some cases it was throwing a
32+
// `Cannot access '<name>' before initialization` error, despite hoisting.
33+
let sigkillTimeoutId: ReturnType<typeof setTimeout> | undefined;
34+
let rejectTimeoutId: ReturnType<typeof setTimeout> | undefined;
35+
36+
// Watch for exiting, so that we can cancel the timeouts.
3237
child.on('exit', () => {
3338
clearTimeout(sigkillTimeoutId);
3439
clearTimeout(rejectTimeoutId);
@@ -43,9 +48,10 @@ export function killChildProcessGracefully(
4348
}
4449

4550
// Start a timeout for the SIGKILL fallback
46-
const sigkillTimeoutId = setTimeout(() => treeKill(pid, 'SIGKILL'), timeoutInMs);
51+
sigkillTimeoutId = setTimeout(() => treeKill(pid, 'SIGKILL'), timeoutInMs);
52+
4753
// Start another timeout to reject the promise if the child process never fires `exit` for some reasons.
48-
const rejectTimeoutId = setTimeout(
54+
rejectTimeoutId = setTimeout(
4955
() => reject(new Error('Child process did not exit gracefully within the timeout.')),
5056
timeoutInMs * 2,
5157
);

0 commit comments

Comments
 (0)