Fix some more thread leaks

This commit is contained in:
Radon Rosborough 2023-02-12 19:04:21 -08:00
parent fb166f4115
commit 964ed080e8
4 changed files with 24 additions and 3 deletions

View File

@ -50,6 +50,9 @@ export function watchPods() {
podExists: (podName) => { podExists: (podName) => {
return podName in pods; return podName in pods;
}, },
close: () => {
informer.stop();
},
}; };
} }
@ -361,6 +364,9 @@ export async function initUserSession({ watcher, podName, proxyInfo }) {
}) })
), ),
}, },
close: () => {
conn.close();
},
}; };
}, },
}); });

View File

@ -60,13 +60,15 @@ async function main() {
// pty. // pty.
const outputQueue = new PQueue({ concurrency: 1, autoStart: false }); const outputQueue = new PQueue({ concurrency: 1, autoStart: false });
let handlePtyOutput, handlePtyExit; let handlePtyOutput, handlePtyExit;
const exec = await session.exec(["bash"], { let exec = await session.exec(["bash"], {
pty: true, pty: true,
on: { on: {
stdout: (data) => outputQueue.add(() => handlePtyOutput(data)), stdout: (data) => outputQueue.add(() => handlePtyOutput(data)),
stderr: (data) => process.stderr.write(data), stderr: (data) => process.stderr.write(data),
exit: (status) => { exit: (status) => {
handlePtyExit(); handlePtyExit();
watcher.close();
exec.close();
process.exit(status); process.exit(status);
}, },
error: (err) => process.stderr.write(`riju: error: ${err}\n`), error: (err) => process.stderr.write(`riju: error: ${err}\n`),

View File

@ -187,7 +187,7 @@ export function asBool(value, def) {
// cluster. // cluster.
export function deptyify({ handlePtyInput }) { export function deptyify({ handlePtyInput }) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const done = false; let done = false;
let triggerDone = () => { let triggerDone = () => {
// Calling the function stored in this variable should have the // Calling the function stored in this variable should have the
// effect of terminating the tmp-promise callback and getting // effect of terminating the tmp-promise callback and getting
@ -251,13 +251,18 @@ export function deptyify({ handlePtyInput }) {
// SIGTERM, wait for proc to exit, if it doesn't, // SIGTERM, wait for proc to exit, if it doesn't,
// then SIGKILL. // then SIGKILL.
proc.kill("SIGTERM"); proc.kill("SIGTERM");
let timeout = null;
try { try {
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
proc.on("exit", resolve); proc.on("exit", resolve);
setTimeout(reject, 1000); timeout = setTimeout(reject, 250);
}); });
} catch (err) { } catch (err) {
proc.kill("SIGKILL"); proc.kill("SIGKILL");
} finally {
if (timeout) {
clearTimeout(timeout);
}
} }
} catch (err) { } catch (err) {
logError(err); logError(err);

View File

@ -95,6 +95,10 @@ int main(int argc, char **argv)
if (exec_pid < 0) if (exec_pid < 0)
die("fork failed"); die("fork failed");
else if (exec_pid == 0) { else if (exec_pid == 0) {
if (signal(SIGTERM, SIG_DFL) == SIG_ERR)
die("signal failed");
if (signal(SIGINT, SIG_DFL) == SIG_ERR)
die("signal failed");
if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0) if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
die("prctl failed"); die("prctl failed");
if (getppid() != orig_ppid) if (getppid() != orig_ppid)
@ -131,6 +135,10 @@ int main(int argc, char **argv)
} }
return WEXITSTATUS(wstatus); return WEXITSTATUS(wstatus);
} }
if (signal(SIGTERM, SIG_DFL) == SIG_ERR)
die("signal failed");
if (signal(SIGINT, SIG_DFL) == SIG_ERR)
die("signal failed");
if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0) if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
die("prctl failed"); die("prctl failed");
if (getppid() != orig_ppid) if (getppid() != orig_ppid)