Skip to content

Metrics flush timer should use .unref() to not block process exit #19171

@BYK

Description

@BYK

Problem

The metrics flush timer created in setupWeightBasedFlushing() (in packages/core/src/client.ts) does not call .unref() on the setTimeout timer. This causes the Node.js event loop to stay alive for up to 5 seconds after the main work is complete, preventing CLI tools and short-lived processes from exiting promptly.

Current behavior

// packages/core/src/client.ts, setupWeightBasedFlushing()
flushTimeout = setTimeout(() => {
  flushFn(client);
}, DEFAULT_FLUSH_INTERVAL);  // 5000ms

This timer keeps the process alive even after client.flush() is called (though flush() does clear the timer via the flushHook event, the timer may be recreated if metrics are captured during flushing).

Expected behavior

The timer should use .unref() so it doesn't prevent the process from exiting:

flushTimeout = setTimeout(() => {
  flushFn(client);
}, DEFAULT_FLUSH_INTERVAL);
if (typeof flushTimeout !== 'number' && flushTimeout.unref) {
  flushTimeout.unref();
}

This pattern is already used elsewhere in the SDK:

  • client.ts line ~655: _isClientDoneProcessing()
  • promisebuffer.ts: drain timeout

Impact

This affects any short-lived Node.js process using Sentry metrics:

  • CLI tools
  • Serverless functions
  • Scripts

Workaround

We're currently patching the SDK in our project: https://github.com/getsentry/cli/blob/main/patches/%40sentry%2Fcore%4010.38.0.patch

Environment

  • @sentry/bun / @sentry/node: 10.38.0
  • Node.js / Bun: All versions

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions