Skip to content

fix: let the process exit on SIGINT and SIGTERM after the span cache is used - #2846

Open
Abhishek B R (Abhishek-B-R) wants to merge 1 commit into
braintrustdata:mainfrom
Abhishek-B-R:fix-span-cache-signal-exit
Open

Abhishek B R (Abhishek-B-R) wants to merge 1 commit into
braintrustdata:mainfrom
Abhishek-B-R:fix-span-cache-signal-exit

Conversation

@Abhishek-B-R

Copy link
Copy Markdown

The span cache registers SIGINT and SIGTERM listeners the first time it opens its temp file (registerExitHandler in js/src/span-cache.ts). Node only exits on those signals by default while nothing listens for them, so after the first Eval() the process stops responding to Ctrl+C and to SIGTERM for the rest of its life. That matters for anything long running that calls Eval(): a server, a watch loop, or a container that gets SIGTERM on shutdown and is then killed after the grace period.

Repro, run with node --import tsx from js/:

configureNode();
const cache = new SpanCache();
cache.start();
cache.queueWrite("root", "span", { span_id: "span" });
await cache.waitForPendingWrites();
cache.stop();
cache.dispose();

setInterval(() => {}, 1000);
setTimeout(() => process.kill(process.pid, "SIGTERM"), 300);
setTimeout(() => {
  console.log("still alive 2s after SIGTERM");
  process.exit(0);
}, 2300);

On main it prints still alive 2s after SIGTERM. With this change the process exits on the SIGTERM.

The signal listeners are now registered with once(), clean up as before, and then re-raise the signal when no other listener is left, so Node's default exit runs. If the application has its own SIGINT or SIGTERM listener, nothing is re-raised and it keeps handling the signal itself, which is how it behaved without the SDK.

Added js/src/span-cache-signals.test.ts, which fails on main (the listener is still attached after it runs and process.kill is never called) and passes here. span-cache.test.ts and span-cache-flush-failure.test.ts still pass, and pnpm run check:typings and prettier are clean. Changeset included.

…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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant