Repository navigation
Description
Is there an existing issue for this?
- I have checked for existing issues
- I have reviewed the documentation
- I am using the latest SDK release
How do you use Sentry?
Sentry SaaS (sentry.io)
Which SDK are you using?
@sentry/node (we run @sentry/tanstackstart-react on the server)
SDK Version
11.11.0 (first seen on 11.8.0)
Framework Version
Node.js 26.13.0
Reproduction Example/SDK Setup
// npm i @sentry/node@11.11.0
// node --expose-gc repro.cjs
const http = require("node:http");
const Sentry = require("@sentry/node");
const errorSpanIds = [];
Sentry.init({
dsn: "https://public@example.invalid/1",
tracesSampleRate: 1,
enableOpenTelemetrySetup: true,
beforeSend(event) {
errorSpanIds.push(event.contexts?.trace?.span_id);
return null;
},
});
const tick = () => new Promise((resolve) => setImmediate(resolve));
const server = http.createServer((_req, res) => res.end("hello"));
server.listen(0, "127.0.0.1", async () => {
try {
await Sentry.startSpan({ name: "parent" }, async (parent) => {
const childParentId = () => {
const child = Sentry.startInactiveSpan({ name: "child" });
child.end();
return Sentry.spanToJSON(child).parent_span_id;
};
console.log("parent span id:", parent.spanContext().spanId);
console.log("child parent before HTTP:", childParentId());
await new Promise((resolve, reject) => {
http
.get(`http://127.0.0.1:${server.address().port}/`, { agent: false }, (res) => {
res.resume();
res.on("end", resolve);
res.on("error", reject);
})
.on("error", reject);
});
console.log("child parent after HTTP, before GC:", childParentId());
await tick();
globalThis.gc();
await tick();
console.log("child parent after GC:", childParentId());
console.log("getActiveSpan() === parent:", Sentry.getActiveSpan() === parent);
Sentry.captureException(new Error("boom"));
await Sentry.flush(1000);
console.log("error event span_id:", errorSpanIds[0]);
});
} finally {
server.close();
await Sentry.close(1000);
}
});This reproduction does not call bindScopeToEmitter. The SDK's HTTP client instrumentation binds the response automatically. Forced GC makes the failure easy to reproduce. We also reproduced it without --expose-gc, using ordinary object allocation between event-loop turns.
Steps to Reproduce
npm i @sentry/node@11.11.0node --expose-gc repro.cjs
Expected Result
The child span and the error event stay attached to parent after the HTTP response has been handled:
parent span id: a1dfb6ececa2d31c
child parent before HTTP: a1dfb6ececa2d31c
child parent after HTTP, before GC: a1dfb6ececa2d31c
child parent after GC: a1dfb6ececa2d31c
getActiveSpan() === parent: true
error event span_id: a1dfb6ececa2d31c
That is the output when globalThis.gc() is replaced with a no-op.
Actual Result
parent span id: 8850fa2541397537
child parent before HTTP: 8850fa2541397537
child parent after HTTP, before GC: 8850fa2541397537
child parent after GC: undefined
getActiveSpan() === parent: true
error event span_id: 9eb4ab36a0ba6c1a
After a GC, startInactiveSpan creates a new root span, and the error event carries a span id that is not parent's. getActiveSpan() with no argument still returns parent. With enableOpenTelemetrySetup: false, attribution stays correct.
When a trace is continued from a sentry-trace header, the new local root gets the propagated parent span id instead of the active local span's id. We reproduced that variant too. We investigated this after seeing many one-span transactions and inconsistent parent relationships in our application. Those symptoms are consistent with this bug, but the reproduction does not show that it explains every affected record.
Additional Context
We think the cause is this sequence:
bindListenerToScopeinpackages/core/src/tracing/bindScopeToEmitter.tsruns each listener withwithScope(scope, ...), passing the captured scope object itself.packages/core/src/integrations/http/client-subscriptions.tsbinds outgoing HTTP responses this way.- With OpenTelemetry setup on,
withSetScopeinpackages/opentelemetry/src/asyncContextStrategy.tsbuilds a new context for that scope.buildContextWithSentryScopesreuses the same scope object and sets its context link to the new context. The link is aWeakRef(setContextOnScopeinpackages/opentelemetry/src/utils/contextData.ts). - The listener's context is short-lived. Once it is collected,
getActiveSpan(scope)inpackages/opentelemetry/src/utils/getActiveSpan.tsfinds no context for the scope and returnsundefined. The parent span and the original context are still alive; only the scope's link to its context has been overwritten. startInactiveSpanwithout an explicit parent resolves its parent throughgetActiveSpan(scope)(packages/core/src/tracing/trace.ts). Event preparation also callsgetActiveSpan(finalScope)(packages/core/src/utils/prepareEvent.ts). After collection, both find no span even though the ambient OpenTelemetry context still has the parent. In this reproduction,startSpanandstartSpanManualstill find the correct parent through their scope-forking path.
Cloning the scope for each listener invocation fixes these reproductions on 11.8.0 and 11.11.0. It changes the API's existing scope-sharing behavior, so we are treating it as a local workaround rather than a behavior-preserving fix:
function bindListenerToScope(listener, scope) {
return function (...args) {
- return withScope(scope, () => listener.apply(this, args));
+ return withScope(scope.clone(), () => listener.apply(this, args));
};
}With this change, each listener invocation gets a new current-scope object. Changes made through getCurrentScope() inside a listener no longer persist to the caller, to other listeners or to later invocations. It also changes scope identity, which the existing bindScopeToEmitter unit tests assert. The isolation scope stays shared, so Sentry.setTag() and Sentry.addBreadcrumb() still affect it. Cloning is shallow and adds an allocation and copy per invocation. We have not run the SDK's test suite against it or measured the overhead. A fix in the context-management code that keeps the existing binding semantics may be better.
Metadata
Metadata
Assignees
Projects
- StatusShow more project fieldsNo status
Activity
linear-code commentedon Nov 9, 2026
JS-4090
msonnb commentedon Nov 10, 2026
Hey @chrisolszewski thanks for reporting! I have a PR open to fix.
fix(opentelemetry): Keep live context link when passing a scope to wi…
github-actions commentedon Nov 16, 2026
A PR closing this issue has just been released 🚀
This issue was referenced by PR #26234, which was included in the 11.13.0 release.