Skip to content

Give code outside a trace frame the request's active span - #8802

Closed
danlapid wants to merge 2 commits into
mainfrom
dlapid/active-span-single-rule
Closed

danlapid wants to merge 2 commits into
mainfrom
dlapid/active-span-single-rule

Conversation

@danlapid

@danlapid danlapid commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #8651, whose branch is the base here, so the diff is this change alone.

With no trace entry in the current async context frame, getCurrentTraceSpan() and getCurrentUserTraceSpan() fall back to the current request's spans. After #8651, tracing.getActiveSpan() used that fallback only while an unobserved stateless request was current, and otherwise returned undefined. So a function bound outside every request gave a different answer depending on whether the request was traced:

const fromModuleScope = AsyncLocalStorage.bind(() => tracing.getActiveSpan());

export default {
  fetch() {
    fromModuleScope();
    // main:   undefined
    // #8651:  undefined if traced, the request's no-op span if not
    // this:   the request's root span, traced or not
  },
};

getActiveSpan() now follows the same rule as the other readers. Inside an invocation, with no entry, it returns the current request's root span. It returns undefined only outside an invocation. The request's trace frame now holds the same per-request user trace scope value the fallback uses, so code inside and outside the frame gets the same Span object.

This changes documented behavior. tracing.h and tracing.d.ts said getActiveSpan() also returns undefined "when execution is detached into the root async context". Only AsyncLocalStorage.bind() or AsyncResource with no current frame does that, as at module scope. That clause is removed, and tracing-helpers-test.js now expects the invocation span there.

This doesn't change the hot path. An unobserved stateless request still builds no frame. A request that builds a frame now reuses the per-request value rather than making a new one.

🤖 Generated with Claude Code

danlapid and others added 2 commits November 21, 2026 22:01
makeRequestTraceScope() built a new async context frame for every
request, holding two opaque wrappers and global handles, even when
neither span was observed. On a hello-world benchmark that was about
7% of CPU per request, all of it garbage.

When neither span is observed, a stateless request that starts in
the root frame now builds no frame at all. With no entry,
getCurrentTraceSpan() and getCurrentUserTraceSpan() fall back to the
current request's spans, which are the same unobserved spans the
entries held, since the IoContext of a stateless request runs no
other request. Actors and requests that start inside another frame
still build their own frame, so an unobserved actor continuation
keeps masking a newer observed request, and a nested request doesn't
inherit its parent's entries.

tracing.getActiveSpan() returns no span when it finds no entry, as
for code bound outside every request. While an unobserved stateless
request is current, it now returns that request's no-op span
instead, cached on a user trace scope value made once per request.
Such code can't be told apart from the request's own code, so it
gets the no-op span too, where it got no span before. Traced
requests are unaffected.

A frame made inside an unobserved stateless request has no trace
entries. If it runs in another, observed request, its spans resolve
to that request's, as they already did for a frame captured by an
unobserved request, and getActiveSpan() returns no span rather than
throwing.

getCurrentTraceSpan() now returns an unobserved span instead of
throwing when no frame entry applies and the IoContext has no
current request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With no trace entry in the current async context frame,
getCurrentTraceSpan() and getCurrentUserTraceSpan() fall back to the
current request's spans. tracing.getActiveSpan() fell back to them
only during an unobserved stateless request, which runs with no
trace frame, and otherwise returned undefined. So a function bound
outside every request, for example with AsyncLocalStorage.bind() at
module scope, got the request's no-op span in an untraced request
and undefined in a traced one.

getActiveSpan() now follows the same rule as the other readers: with
no entry, inside an invocation, it returns the current request's
root span. It returns undefined only outside an invocation, so what
it returns no longer depends on whether the request is traced. The
doc comments said it also returned undefined when execution is
detached into the root async context, which only such a binding
does; that clause is removed.

The request's trace frame now holds the same per-request user trace
scope value that getActiveSpan() falls back to, so code inside and
outside the frame gets the same span object.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@danlapid
danlapid changed the base branch from dlapid/shared-trace-frame to main November 22, 2026 03:27
@danlapid danlapid closed this Nov 22, 2026
@danlapid

Copy link
Copy Markdown
Collaborator Author

Folded into #8651, which now includes this change as part of a single commit (167e4c8).

@danlapid
danlapid deleted the dlapid/active-span-single-rule branch November 22, 2026 03:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant