Skip to content

Commit

Permalink
Interrupt child procs when interrupting kernel (#16319)
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne authored Dec 20, 2024
1 parent bb17a03 commit dca1dad
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/kernels/raw/launcher/kernelProcess.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export class KernelProcess extends ObservableDisposable implements IKernelProces
) {
logger.info('Interrupting kernel via SIGINT');
if (this._process.pid) {
await this.interruptChildProcesses(this._process.pid);
kill(this._process.pid, 'SIGINT');
}
} else if (
Expand Down Expand Up @@ -407,6 +408,33 @@ export class KernelProcess extends ObservableDisposable implements IKernelProces
}
}

private async interruptChildProcesses(pid: number) {
// Do not remove this code, in in unit tests we end up running this,
// then we run into the danger of kill all of the processes on the machine.
// because calling `pidtree` without a pid will return all pids and hence everything ends up getting killed.
if (!ProcessService.isAlive(pid)) {
return;
}
try {
if (this.platform.isWindows) {
return;
} else {
await new Promise<void>((resolve) => {
pidtree(pid, (ex: unknown, pids: number[]) => {
if (ex) {
logger.warn(`Failed to interrupt children for ${pid}`, ex);
} else {
pids.forEach((procId) => kill(procId, 'SIGINT'));
}
resolve();
});
});
}
} catch (ex) {
logger.warn(`Failed to interrupt children for ${pid}`, ex);
}
}

private sendToOutput(data: string) {
if (this.outputChannel) {
this.outputChannel.append(data);
Expand Down

0 comments on commit dca1dad

Please sign in to comment.