diff --git a/.changeset/span-cache-signal-exit.md b/.changeset/span-cache-signal-exit.md new file mode 100644 index 000000000..63038e3f1 --- /dev/null +++ b/.changeset/span-cache-signal-exit.md @@ -0,0 +1,5 @@ +--- +"braintrust": patch +--- + +fix: Let the process exit on SIGINT and SIGTERM after an eval has used the span cache diff --git a/js/src/span-cache-signals.test.ts b/js/src/span-cache-signals.test.ts new file mode 100644 index 000000000..d66a87c74 --- /dev/null +++ b/js/src/span-cache-signals.test.ts @@ -0,0 +1,58 @@ +import { + afterEach, + beforeAll, + describe, + expect, + test, + vi, +} from "vite-plus/test"; +import { SpanCache } from "./span-cache"; +import { configureNode } from "./node/config"; + +// Node only exits on SIGINT and SIGTERM by default while nothing listens for +// them. The span cache adds listeners to remove its temp file, so they must +// not keep the process alive after Eval() has used the cache once. +describe("SpanCache signal handlers", () => { + const signals = ["SIGINT", "SIGTERM"] as const; + let cache: SpanCache | undefined; + + beforeAll(() => { + configureNode(); + }); + + afterEach(() => { + vi.restoreAllMocks(); + cache?.stop(); + cache?.dispose(); + cache = undefined; + }); + + test("re-raises the signal after cleanup when nothing else handles it", async () => { + const existing = Object.fromEntries( + signals.map((signal) => [signal, process.rawListeners(signal)]), + ); + for (const signal of signals) process.removeAllListeners(signal); + const kill = vi.spyOn(process, "kill").mockImplementation(() => true); + try { + cache = new SpanCache(); + cache.start(); + cache.queueWrite("root", "span", { span_id: "span" }); + await cache.waitForPendingWrites(); + + for (const signal of signals) { + const listeners = process.rawListeners(signal); + expect(listeners).toHaveLength(1); + listeners[0](signal); + expect(process.listenerCount(signal)).toBe(0); + expect(kill).toHaveBeenCalledWith(process.pid, signal); + } + } finally { + for (const signal of signals) { + process.removeAllListeners(signal); + for (const listener of existing[signal]) { + process.on(signal, listener as (...args: unknown[]) => void); + } + } + } + }); +}); diff --git a/js/src/span-cache.ts b/js/src/span-cache.ts index 5ac7d0cf4..e87104f18 100644 --- a/js/src/span-cache.ts +++ b/js/src/span-cache.ts @@ -232,10 +232,19 @@ export class SpanCache { } }; + // A signal listener replaces Node's default exit, so re-raise the signal + // after cleanup unless the application listens for it too. + const cleanupAndExit = (signal: NodeJS.Signals) => { + cleanupAllCaches(); + if (process.listenerCount(signal) === 0) { + process.kill(process.pid, signal); + } + }; + // Register for multiple exit scenarios process.on("exit", cleanupAllCaches); - process.on("SIGINT", cleanupAllCaches); - process.on("SIGTERM", cleanupAllCaches); + process.once("SIGINT", cleanupAndExit); + process.once("SIGTERM", cleanupAndExit); process.on("beforeExit", cleanupAllCaches); } }