Repository navigation
Build no trace frame for unobserved stateless requests - #8651
Conversation
b2cb350 to
70391a8
Compare
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. Two readers relied on every request having a frame. tracing.getActiveSpan() returned undefined with no entry, so an unobserved request's own code would have had no active span. It now follows the same rule as the other readers: with no entry, inside an invocation, it returns the current request's root span. The request's frame holds the same per-request user trace scope value, so code inside and outside the frame gets the same span object. This changes what a function bound outside every request, as with AsyncLocalStorage.bind() at module scope, gets during a request: the request's root span rather than undefined. getActiveSpan() now returns undefined only outside an invocation, and the doc comments no longer say it does when execution is detached into the root async context. AsyncLocalStorage.bind() tied a function to the request that bound it only when a frame was current, as it always was in a request. AsyncContextFrame::wrapRoot() now takes a validator like wrap() and wrapSnapshot(), and bind() passes the request check in the root frame too, as snapshot() already did. AsyncResource is unchanged: a resource that captured no frame holds no request's state and can run in any request. A frame made inside an unobserved stateless request has no trace entries. If it runs in another request, it resolves to that request's spans, and getActiveSpan() returns that request's root 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>
70391a8 to
167e4c8
Compare
There was a problem hiding this comment.
Superseded by the review of
aba5435c.
This review, of 167e4c86
✅ Approved — No issues found. The frame-skip condition, the fallbacks in the span readers and the new root-frame validator for AsyncLocalStorage.bind() all behave as described, and tests cover them.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
No security concerns.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md updates needed.
📋 Code Quality: ✅ LGTM
- When a frame is built:
RequestTraceScopestill builds a frame for actors, for requests with an observed span, and for requests that start inside another frame. It skips the frame only in the unobserved stateless case. - Span readers:
getCurrentUserTraceSpan()falls back to the request's root user span.getCurrentTraceSpan()falls back togetMetrics().getSpan(), and the newincomingRequests.empty()guard keeps it from callinggetMetrics()when there is no current request. getActiveSpan(): it reads the same cached root user trace scope that the frame holds, so code inside and outside the frame gets the sameSpan.AsyncLocalStorage.bind(): in the root frame it now passes the request validator, which matchessnapshot(). The existingbound-als-testruns this path.AsyncResource: unchanged, asasync-resource-request-context-testexpects.
⚡ Performance: ✅ LGTM
- Unobserved stateless requests no longer allocate a frame, two opaque wrappers or global handles.
- The root user trace scope is built once per request and cached.
- The new validator closure in root-frame
bind()costs one allocation per call, on a path that runs rarely.
📝 Documentation: ✅ LGTM
tracing.h and tracing.d.ts now match the new getActiveSpan() behavior.
Reviewed commit: 167e4c86
There was a problem hiding this comment.
✅ Approved — No issues found. The patches are the same as at the last review, and the only new commit merges main.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
No security concerns.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md updates needed.
📋 Code Quality: ✅ LGTM
No issues found. Both wrapRoot() callers in async-hooks.c++ pass the new validator. The callers of makeRequestTraceScope() keep the returned scope in a local, which works with the new RequestTraceScope class.
⚡ Performance: ✅ LGTM
Unobserved stateless requests no longer allocate a frame, two opaque wrappers or their global handles. The root user trace scope is built once per request and cached. The validator closure in root-frame bind() adds one allocation per call, on a path that runs rarely.
📝 Documentation: ✅ LGTM
tracing.h and tracing.d.ts match the new getActiveSpan() behavior.
Reviewed commit: aba5435c
makeRequestTraceScope()built a new async context frame for every request, with two opaque wrappers and global handles, even when neither span was observed. All of it was garbage once the request ended.When neither span is observed, a stateless request that starts in the root frame now builds no frame at all. With no entry,
getCurrentTraceSpan()andgetCurrentUserTraceSpan()fall back to the current request's spans. Those are the same unobserved spans the entries held, because a stateless IoContext runs no other request. Actors, and requests that start inside another frame, still build their own frame.Two readers relied on every request having a frame.
tracing.getActiveSpan(): one ruleIt returned
undefinedwhen it found no entry, so an unobserved request's own code would have had no active span. It now follows the same rule as the other readers: inside an invocation, with no entry, it returns the current request's root span. The request's frame holds the same per-request value, so code inside and outside the frame gets the sameSpanobject.undefinednow means only "outside an invocation", andtracing.handtracing.d.tssay so.This changes what code bound outside every request gets during a request:
tracing-helpers-test.jsnow expects that.tracing-helpers-untraced-test.jscovers the same case in an untraced request.AsyncLocalStorage.bind()in the root frameWith
bind_asynclocalstorage_snapshot_to_request,bind()tied a function to the request that bound it only when a frame was current, which used to be always.AsyncContextFrame::wrapRoot()now takes a validator, aswrap()andwrapSnapshot()do.bind()passes the request check in the root frame too, matching whatsnapshot()already did;bound-als-testcovers it.AsyncResourceis unchanged. A resource that captured no frame holds no request's state, so it can still run in any request, asasync-resource-request-context-testexpects.The commit message lists two smaller changes:
getCurrentTraceSpan()with no current request returns an unobserved span.Measurement
Measured on an AMD EPYC Linux VM, with the server pinned to one core and
ohadriving 200k requests per round, against a hello-worldfetchWorker at the binary's newest compatibility date. All builds use--config=release_linux --config=thin-ltoand were interleaved over three rounds. The sync handler returnsnew Response("Hello World!"); the async one awaits first. Instructions per request (user space) repeat within about ±0.5k. Requests per second varied by up to ±5% between rounds.main(e3f67c7b2e) → thismain→ thisThese numbers are from the request path as of the earlier revision of this PR. The
getActiveSpan()andbind()changes don't touch that path. An earlier version entered one shared frame with null entries instead; it keptgetActiveSpan()unchanged and measured 183.0 / 184.0 kinstr.Split out of #8643, where every commit of that stack is measured on its own.
🤖 Generated with Claude Code