Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions packages/engine/src/utils/shutdown.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { describe, expect, it, vi } from "vitest";
import { clearGracefulShutdown, gracefulShutdown } from "./shutdown";

describe("graceful shutdown handlers", () => {
it("clears only signal handlers registered by Sidequest", () => {
const hostSigintHandler = vi.fn();
const hostSigtermHandler = vi.fn();
process.on("SIGINT", hostSigintHandler);
process.on("SIGTERM", hostSigtermHandler);

try {
const sigintListenersBefore = process.listeners("SIGINT");
const sigtermListenersBefore = process.listeners("SIGTERM");

gracefulShutdown(vi.fn(), "Test", true);

expect(process.listenerCount("SIGINT")).toBe(sigintListenersBefore.length + 1);
expect(process.listenerCount("SIGTERM")).toBe(sigtermListenersBefore.length + 1);

clearGracefulShutdown();

expect(process.listeners("SIGINT")).toEqual(sigintListenersBefore);
expect(process.listeners("SIGTERM")).toEqual(sigtermListenersBefore);
} finally {
clearGracefulShutdown();
process.off("SIGINT", hostSigintHandler);
process.off("SIGTERM", hostSigtermHandler);
}
});

it("does not remove host handlers when Sidequest signal handling is disabled", () => {
const hostSigintHandler = vi.fn();
const hostSigtermHandler = vi.fn();
process.on("SIGINT", hostSigintHandler);
process.on("SIGTERM", hostSigtermHandler);

try {
const sigintListenersBefore = process.listeners("SIGINT");
const sigtermListenersBefore = process.listeners("SIGTERM");

gracefulShutdown(vi.fn(), "Test", false);
clearGracefulShutdown();

expect(process.listeners("SIGINT")).toEqual(sigintListenersBefore);
expect(process.listeners("SIGTERM")).toEqual(sigtermListenersBefore);
} finally {
clearGracefulShutdown();
process.off("SIGINT", hostSigintHandler);
process.off("SIGTERM", hostSigtermHandler);
}
});
});
29 changes: 22 additions & 7 deletions packages/engine/src/utils/shutdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { logger } from "@sidequest/core";

let shuttingDown = false;

interface RegisteredSignalHandler {
signal: "SIGINT" | "SIGTERM";
handler: () => Promise<void>;
}

const registeredSignalHandlers: RegisteredSignalHandler[] = [];

/**
* Handles the shutdown process, ensuring it only runs once and logs appropriately.
* @param fn The async function to run during shutdown.
Expand Down Expand Up @@ -33,17 +40,25 @@ async function shutdown(fn: () => void | Promise<void>, tag: string, signal: str
*/
export function gracefulShutdown(fn: () => void | Promise<void>, tag: string, enabled: boolean) {
if (enabled) {
process.on("SIGINT", async () => {
const sigintHandler = async () => {
await shutdown(fn, tag, "SIGINT");
});

process.on("SIGTERM", async () => {
};
const sigtermHandler = async () => {
await shutdown(fn, tag, "SIGTERM");
});
};

process.on("SIGINT", sigintHandler);
process.on("SIGTERM", sigtermHandler);
registeredSignalHandlers.push(
{ signal: "SIGINT", handler: sigintHandler },
{ signal: "SIGTERM", handler: sigtermHandler },
);
}
}

export function clearGracefulShutdown() {
process.removeAllListeners("SIGINT");
process.removeAllListeners("SIGTERM");
for (const { signal, handler } of registeredSignalHandlers) {
process.off(signal, handler);
}
registeredSignalHandlers.length = 0;
}
Loading