Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

fix(terminal): stop interactive programs from leaking CPU after close (#12230)

Andras Bacsai10e93c3751b5
diff --git a/docker/coolify-terminal/terminal-server.js b/docker/coolify-terminal/terminal-server.js
index 394cf5277d..92c6b84f11 100755
--- a/docker/coolify-terminal/terminal-server.js
+++ b/docker/coolify-terminal/terminal-server.js
@@ -518,6 +518,8 @@ async function killPtyProcess(userId) {
// session.ptyProcess.kill() wont work here because of https://github.com/moby/moby/issues/9098
// patch with https://github.com/moby/moby/issues/9098#issuecomment-189743947
+ // Ctrl-C first so interactive programs (htop, top, less) quit instead of reading the kill command as keys.
+ session.ptyProcess.write('\x03');
session.ptyProcess.write('set +o history\nkill -TERM -$$ && exit\nset -o history\n');
setTimeout(() => {
@@ -538,10 +540,19 @@ async function killPtyProcess(userId) {
if (killAttempts < maxAttempts) {
attemptKill();
} else {
- logTerminal('warn', 'PTY process still active after maximum termination attempts.', {
+ logTerminal('warn', 'PTY process still active after maximum termination attempts. Killing the local SSH client.', {
userId,
killAttempts,
});
+ // Ending the local ssh client makes sshd hang up the remote session, so nothing keeps running there.
+ try {
+ session.ptyProcess.kill();
+ } catch (error) {
+ logTerminal('error', 'Failed to kill PTY process.', {
+ userId,
+ error: error.message,
+ });
+ }
resolve(false);
}
}, 500);