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
5 changes: 5 additions & 0 deletions .changeset/span-cache-signal-exit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"braintrust": patch
---

fix: Let the process exit on SIGINT and SIGTERM after an eval has used the span cache
58 changes: 58 additions & 0 deletions js/src/span-cache-signals.test.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
});
});
13 changes: 11 additions & 2 deletions js/src/span-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down