Repository navigation
Conversation
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>
Collaborator
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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()andgetCurrentUserTraceSpan()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 returnedundefined. So a function bound outside every request gave a different answer depending on whether the request was traced: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 returnsundefinedonly 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 sameSpanobject.This changes documented behavior.
tracing.handtracing.d.tssaidgetActiveSpan()also returnsundefined"when execution is detached into the root async context". OnlyAsyncLocalStorage.bind()orAsyncResourcewith no current frame does that, as at module scope. That clause is removed, andtracing-helpers-test.jsnow 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