Skip to content

Commit

Permalink
ensure subprocesses are getting terminated b ctrl-c
Browse files Browse the repository at this point in the history
  • Loading branch information
GGonryun committed Feb 29, 2024
1 parent e334985 commit fc51654
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/plugins/aws/ssm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ const subcommandManager = (credentials: AwsCredentials) => {
...credentials,
},
stdio: ["inherit", "pipe", "pipe"],
// Using a detached process group ensures that the child's subprocesses can be killed when the parent process is terminated
detached: true,
});

Expand All @@ -197,7 +198,8 @@ const subcommandManager = (credentials: AwsCredentials) => {
killProcesses: () => {
streamClosers.forEach((closer) => closer());
children.forEach((child) => {
if (child.pid) {
if (child.pid && !child.killed) {
// Tells the parent process to kill the child process and all of it's descendants.
process.kill(-child.pid);
} else {
// Emergency attempt to kill the child process,
Expand Down Expand Up @@ -235,7 +237,7 @@ const sessionOutputStream = (
}

if (!options?.suppressStartSessionMessage || !match) {
// Pass the original chunk through
// Pass the original chunk through to the terminal
this.push(chunk);
}

Expand Down Expand Up @@ -311,10 +313,16 @@ const spawnSsmNode = async (
return;
}

subprocesses.killProcesses();
print2("SSH session terminated");
subprocesses.killProcesses();
resolve(code);
});

// Ensure that the child process is killed when the parent process is terminated by pressing Ctrl+C
process.on("SIGINT", () => {
subprocesses.killProcesses();
process.exit();
});
});

/** Convert an SshCommandArgs into an SSM document "command" parameter */
Expand Down

0 comments on commit fc51654

Please sign in to comment.