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,7 +191,10 @@ export abstract class ShellSession {
this.running = true; this.running = true;
shellProcess.onData(data => this.send({ type: TerminalChannels.STDOUT, data })); shellProcess.onData(data => this.send({ type: TerminalChannels.STDOUT, data }));
shellProcess.onExit(({ exitCode }) => { shellProcess.onExit(({ exitCode }) => {
logger.info(`[SHELL-SESSION]: shell has exited for ${this.terminalId} closed with code=${exitCode}`); logger.info(`[SHELL-SESSION]: shell has exited for ${this.terminalId} closed with exitcode=${exitCode}`);
// This might already be false because of the kill() within the websocket.on("close") handler
if (this.running) {
this.running = false; this.running = false;
if (exitCode > 0) { if (exitCode > 0) {
@ -200,6 +203,7 @@ export abstract class ShellSession {
} else { } else {
this.exit(); this.exit();
} }
}
}); });
this.websocket this.websocket
@ -231,25 +235,27 @@ export abstract class ShellSession {
} }
}) })
.on("close", code => { .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 const stopShellSession = this.running
&& !( && (
code === WebSocketCloseEvent.AbnormalClosure (
|| ( code !== WebSocketCloseEvent.AbnormalClosure
code === WebSocketCloseEvent.GoingAway && code !== WebSocketCloseEvent.GoingAway
&& !this.cluster.disconnected
) )
|| this.cluster.disconnected
); );
if (stopShellSession) { 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; 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);
}
} }
}); });