Skip to content

Commit 2b3af8d

Browse files
betegonclaude
andcommitted
fix(telemetry): close previous Sentry client before re-init to remove stale beforeExit listeners
LightNodeClient.startClientReportTracking() and enableLogs both register process.on('beforeExit', ...) handlers that are only removed by calling client.close(). Without closing the previous client before Sentry.init(), re-calling initSentry (e.g. initSentry(false) in test afterEach) accumulates stale listeners that fire async work (HTTP sends) on beforeExit, preventing the bun process from exiting after tests complete. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c2c9655 commit 2b3af8d

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

src/lib/telemetry.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,15 @@ export function initSentry(
355355
const libraryMode = options?.libraryMode ?? false;
356356
const environment = getEnv().NODE_ENV ?? "development";
357357

358+
// Close the previous client before re-initializing. LightNodeClient registers
359+
// process.on('beforeExit', ...) listeners for client-report and log flushing
360+
// via startClientReportTracking() and enableLogs. These are only removed by
361+
// calling client.close(). Without this, re-initializing (e.g. initSentry(false)
362+
// in test afterEach) accumulates stale listeners that keep the event loop alive
363+
// after all tests complete, preventing the bun process from exiting.
364+
// close(0) removes the listeners synchronously; we don't need to await the flush.
365+
Sentry.getClient()?.close(0);
366+
358367
const client = Sentry.init({
359368
dsn: SENTRY_CLI_DSN,
360369
enabled,

0 commit comments

Comments
 (0)