fix(coding-agent): fire session shutdown on TERM and HUP closes #3212

This commit is contained in:
Mario Zechner
2026-04-15 16:36:30 +02:00
parent 3a13fa80c8
commit 5d440b055c
6 changed files with 92 additions and 7 deletions

View File

@@ -33,6 +33,34 @@ export async function runPrintMode(runtimeHost: AgentSessionRuntime, options: Pr
let exitCode = 0;
let session = runtimeHost.session;
let unsubscribe: (() => void) | undefined;
let disposed = false;
const signalCleanupHandlers: Array<() => void> = [];
const disposeRuntime = async (): Promise<void> => {
if (disposed) return;
disposed = true;
unsubscribe?.();
await runtimeHost.dispose();
};
const registerSignalHandlers = (): void => {
const signals: NodeJS.Signals[] = ["SIGTERM"];
if (process.platform !== "win32") {
signals.push("SIGHUP");
}
for (const signal of signals) {
const handler = () => {
void disposeRuntime().finally(() => {
process.exit(signal === "SIGHUP" ? 129 : 143);
});
};
process.on(signal, handler);
signalCleanupHandlers.push(() => process.off(signal, handler));
}
};
registerSignalHandlers();
const rebindSession = async (): Promise<void> => {
session = runtimeHost.session;
@@ -128,8 +156,10 @@ export async function runPrintMode(runtimeHost: AgentSessionRuntime, options: Pr
console.error(error instanceof Error ? error.message : String(error));
return 1;
} finally {
unsubscribe?.();
await runtimeHost.dispose();
for (const cleanup of signalCleanupHandlers) {
cleanup();
}
await disposeRuntime();
await flushRawStdout();
}
}