Skip to content

Bun: unhandled rejections lose the request's isolation scope and trace id #27178

Backlog

Description

@rodion-m

Is there an existing issue for this?

How do you use Sentry?

Self-hosted/on-premise (the reproduction below needs no server; events are read in beforeSend)

Which SDK are you using?

@sentry/bun

SDK Version

11.23.0 (@sentry/bun and @sentry/node)

Framework Version

Bun 1.4.5 (latest release), Node 24.21.0 as the control; the original finding was in Next.js 16 standalone (@sentry/nextjs)

Link to Sentry event

No response

Reproduction Example/SDK Setup

import * as Sentry from "@sentry/bun"; // same result with "@sentry/node" on Bun

const events = [];
Sentry.init({
  dsn: "http://public@127.0.0.1:9/1",
  beforeSend(event) {
    events.push({
      request: event.tags?.request ?? null,
      traceId: event.contexts?.trace?.trace_id ?? null,
    });
    return null;
  },
});

// Request A finishes normally.
Sentry.withIsolationScope((scope) => {
  scope.setTag("request", "A");
});

// Request B rejects a promise that nobody handles.
let traceB = null;
Sentry.withIsolationScope((scope) => {
  scope.setTag("request", "B");
  Sentry.startSpan({ name: "request B" }, (span) => {
    traceB = span.spanContext().traceId;
    Promise.reject(new Error("unhandled rejection in request B"));
  });
});

setTimeout(async () => {
  await Sentry.flush(500);
  console.log(`expected: request=B traceId=${traceB}`);
  for (const e of events) console.log(`got:      request=${e.request} traceId=${e.traceId}`);
}, 200);

Steps to Reproduce

  1. npm i @sentry/bun@11.23.0
  2. bun repro.mjs (Bun 1.4.5)
  3. For the control, change the import to @sentry/node and run node repro.mjs (Node 24.21.0)

Expected Result

The unhandled rejection is reported with the scope of the request that rejected it, as on Node:

node v24.21.0 @sentry/node 11.23.0
expected: request=B traceId=04b1c333f50f4e028b9d65adc2d6c8b2
got:      request=B traceId=04b1c333f50f4e028b9d65adc2d6c8b2

Actual Result

On Bun the event has no request tags and an unrelated trace id:

bun 1.4.5 @sentry/bun 11.23.0
expected: request=B traceId=466d692e17fa4f438e233960b3f2d90a
got:      request=null traceId=a7f354c8ae6948759385f093a4a9c699

@sentry/node 11.23.0 under Bun gives the same result.

In a Next.js 16 standalone server under Bun (@sentry/nextjs), the same mechanism gives an unhandled rejection a trace id other than its request's, and contexts.nextjs.request_path of the previous request. A route-handler error is attributed correctly.

Additional Context

The root cause is in Bun: it does not restore the rejecting promise's AsyncLocalStorage context in process.on("unhandledRejection") listeners, so the isolation scope the handler reads is the default one. That is tracked in oven-sh/bun#40223, which is still open; its fix, oven-sh/bun#31721, was closed as stale on 2026-11-17 without being merged.

Filing it here because the Next.js on Bun guide and @sentry/bun present Bun as supported, and nothing warns that unhandled rejections lose their request context there. Could this be noted as a known limitation, and linked from the Bun tracking issue #19707, until Bun fixes it? If the SDK can capture the scope when the rejection happens instead of when the listener runs, that would also solve it, though I don't know if that is possible on Bun.

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions