ee100354da
* fix: replace domain-specific exit handler with generic signal handler registry Rewrite exitHandler.ts as a generic, domain-agnostic exit signal module that exports onExitSignal(handler) returning a dispose function. - subprocess.ts now registers via onExitSignal instead of direct process.on(SIGINT/SIGTERM) calls - resolveTokens registers a signal handler that captures tokens by closure, fixing the race condition where the exit handler would read the wrong token after disposal - Remove setupExitHandler and runCleanup — domain cleanup is handled by post.ts + await using Co-authored-by: Cursor <cursoragent@cursor.com> * tweaks * simplify handler installation * extract to util * fix race in dispose * wrap dispose body in try/finally to ensure disposingRef always settles --------- Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: pullfrog[bot] <226033991+pullfrog[bot]@users.noreply.github.com>
36 lines
955 B
TypeScript
36 lines
955 B
TypeScript
import os from "node:os";
|
|
|
|
type ExitSignalHandler = (signal: "SIGINT" | "SIGTERM") => void | Promise<void>;
|
|
|
|
const handlers = new Set<ExitSignalHandler>();
|
|
let installed = false;
|
|
|
|
/**
|
|
* Register a handler to run when the process receives SIGINT or SIGTERM.
|
|
* Returns a dispose function that removes the handler.
|
|
*/
|
|
export function onExitSignal(handler: ExitSignalHandler): () => void {
|
|
installSignalHandlers();
|
|
handlers.add(handler);
|
|
return () => {
|
|
handlers.delete(handler);
|
|
};
|
|
}
|
|
|
|
function installSignalHandlers(): void {
|
|
if (installed) return;
|
|
installed = true;
|
|
|
|
async function handleSignal(signal: "SIGINT" | "SIGTERM") {
|
|
await Promise.allSettled([...handlers].map((h) => Promise.try(h, signal)));
|
|
exitWithSignal(signal);
|
|
}
|
|
|
|
process.on("SIGINT", handleSignal);
|
|
process.on("SIGTERM", handleSignal);
|
|
}
|
|
|
|
export function exitWithSignal(signal: "SIGINT" | "SIGTERM") {
|
|
process.exit(128 + os.constants.signals[signal]);
|
|
}
|