fix(coding-agent): fire session shutdown on TERM and HUP closes #3212
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user