1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Simplify and fix stopShell conditional, fix shell processes not being killed

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-11-17 11:32:40 -05:00
parent 5e30aa9dae
commit 62d68d91e3

View File

@ -191,14 +191,18 @@ export abstract class ShellSession {
this.running = true;
shellProcess.onData(data => this.send({ type: TerminalChannels.STDOUT, data }));
shellProcess.onExit(({ exitCode }) => {
logger.info(`[SHELL-SESSION]: shell has exited for ${this.terminalId} closed with code=${exitCode}`);
this.running = false;
logger.info(`[SHELL-SESSION]: shell has exited for ${this.terminalId} closed with exitcode=${exitCode}`);
if (exitCode > 0) {
this.send({ type: TerminalChannels.STDOUT, data: "Terminal will auto-close in 15 seconds ..." });
setTimeout(() => this.exit(), 15 * 1000);
} else {
this.exit();
// This might already be false because of the kill() within the websocket.on("close") handler
if (this.running) {
this.running = false;
if (exitCode > 0) {
this.send({ type: TerminalChannels.STDOUT, data: "Terminal will auto-close in 15 seconds ..." });
setTimeout(() => this.exit(), 15 * 1000);
} else {
this.exit();
}
}
});
@ -231,25 +235,27 @@ export abstract class ShellSession {
}
})
.on("close", code => {
logger.info(`[SHELL-SESSION]: websocket for ${this.terminalId} closed with code=${code}`);
logger.info(`[SHELL-SESSION]: websocket for ${this.terminalId} closed with code=${WebSocketCloseEvent[code]}(${code})`, { cluster: this.cluster.getMeta() });
const stopShellSession = this.running
&& !(
code === WebSocketCloseEvent.AbnormalClosure
|| (
code === WebSocketCloseEvent.GoingAway
&& !this.cluster.disconnected
&& (
(
code !== WebSocketCloseEvent.AbnormalClosure
&& code !== WebSocketCloseEvent.GoingAway
)
|| this.cluster.disconnected
);
if (stopShellSession) {
try {
logger.info(`[SHELL-SESSION]: Killing shell process for ${this.terminalId}`);
process.kill(shellProcess.pid);
ShellSession.processes.delete(this.terminalId);
} catch {}
this.running = false;
try {
logger.info(`[SHELL-SESSION]: Killing shell process (pid=${shellProcess.pid}) for ${this.terminalId}`);
shellProcess.kill();
ShellSession.processes.delete(this.terminalId);
} catch (error) {
logger.warn(`[SHELL-SESSION]: failed to kill shell process (pid=${shellProcess.pid}) for ${this.terminalId}`, error);
}
}
});