From cf8a96b98cf1f1ff7aba9e99329a114639422a03 Mon Sep 17 00:00:00 2001 From: Abhishek B R Date: Wed, 25 Nov 2026 13:50:00 +0530 Subject: [PATCH] fix: let the process exit on SIGINT and SIGTERM after the span cache is used The span cache registers SIGINT and SIGTERM listeners the first time it creates its temp file. Node drops its default exit behaviour for a signal once anything listens for it, so after the first Eval() a long running process (a server, a watch loop, a container receiving SIGTERM) ignored both signals for the rest of its life. Register the signal listeners with once() and re-raise the signal after cleanup when nothing else listens for it, so the default exit still happens. When the application has its own listener it keeps handling the signal as before. --- .changeset/span-cache-signal-exit.md | 5 +++ js/src/span-cache-signals.test.ts | 58 ++++++++++++++++++++++++++++ js/src/span-cache.ts | 13 ++++++- 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 .changeset/span-cache-signal-exit.md create mode 100644 js/src/span-cache-signals.test.ts 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); } }