Skip to content

Commit c4113ab

Browse files
betegonclaude
andcommitted
fix(telemetry): always remove beforeExit handler on re-init
The handler removal was inside if (client?.getOptions().enabled), so calling initSentry(false) in test afterEach hooks never cleaned up the handler registered by the prior initSentry(true) call. The stale handler kept the event loop alive after all tests finished, causing the bun process to hang indefinitely and the CI Unit Tests step to never complete. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent dcca225 commit c4113ab

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/lib/telemetry.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,17 @@ export function initSentry(
405405
},
406406
});
407407

408+
// Always remove the previous handler on re-init. The removal must happen
409+
// unconditionally — not only when enabled=true — so that calling
410+
// initSentry(false) to disable telemetry (e.g. in test afterEach) actually
411+
// cleans up the handler registered by a prior initSentry(true) call.
412+
// Without this, the stale handler keeps the event loop alive after all tests
413+
// finish, preventing the process from exiting.
414+
if (currentBeforeExitHandler) {
415+
process.removeListener("beforeExit", currentBeforeExitHandler);
416+
currentBeforeExitHandler = null;
417+
}
418+
408419
if (client?.getOptions().enabled) {
409420
const isBun = typeof process.versions.bun !== "undefined";
410421
const runtime = isBun ? "bun" : "node";
@@ -442,13 +453,7 @@ export function initSentry(
442453
// Skipped in library mode — the host owns the process lifecycle.
443454
// The library entry point calls client.flush() manually after completion.
444455
//
445-
// Replace previous handler on re-init (e.g., auto-login retry calls
446-
// withTelemetry → initSentry twice) to avoid duplicate handlers with
447-
// independent re-entry guards and stale client references.
448456
if (!libraryMode) {
449-
if (currentBeforeExitHandler) {
450-
process.removeListener("beforeExit", currentBeforeExitHandler);
451-
}
452457
currentBeforeExitHandler = createBeforeExitHandler(client);
453458
process.on("beforeExit", currentBeforeExitHandler);
454459
}

0 commit comments

Comments
 (0)