Skip to content

Build no trace frame for unobserved stateless requests - #8651

Merged
danlapid merged 2 commits into
mainfrom
dlapid/shared-trace-frame
Nov 24, 2026
Merged

danlapid merged 2 commits into
mainfrom
dlapid/shared-trace-frame

Conversation

@danlapid

@danlapid danlapid commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

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() and getCurrentUserTraceSpan() 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 rule

It returned undefined when 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 same Span object. undefined now means only "outside an invocation", and tracing.h and tracing.d.ts say so.

This changes what code bound outside every request gets during a request:

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

export default {
  fetch() {
    fromModuleScope(); // main: undefined. Now: the request's root span, traced or not.
  },
};

tracing-helpers-test.js now expects that. tracing-helpers-untraced-test.js covers the same case in an untraced request.

AsyncLocalStorage.bind() in the root frame

With 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, as wrap() and wrapSnapshot() do. bind() passes the request check in the root frame too, matching what snapshot() already did; bound-als-test covers it. AsyncResource is unchanged. A resource that captured no frame holds no request's state, so it can still run in any request, as async-resource-request-context-test expects.

The commit message lists two smaller changes:

  • A frame made in an unobserved request that runs in another request gets that request's spans.
  • 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 oha driving 200k requests per round, against a hello-world fetch Worker at the binary's newest compatibility date. All builds use --config=release_linux --config=thin-lto and were interleaved over three rounds. The sync handler returns new 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.

user kinstr/req req/s
sync, main (e3f67c7b2e) → this 198.2 → 181.5 (−16.7k) 14.1k → 14.7k (+4.5%)
async, main → this 197.5 → 182.9 (−14.6k) 13.8k → 14.1k (+2.4%)

These numbers are from the request path as of the earlier revision of this PR. The getActiveSpan() and bind() changes don't touch that path. An earlier version entered one shared frame with null entries instead; it kept getActiveSpan() 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

@danlapid
danlapid force-pushed the dlapid/shared-trace-frame branch from b2cb350 to 70391a8 Compare November 22, 2026 03:12
@danlapid danlapid changed the title Share one trace frame across untraced stateless requests Build no trace frame for unobserved stateless requests Nov 22, 2026
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>
@danlapid
danlapid force-pushed the dlapid/shared-trace-frame branch from 70391a8 to 167e4c8 Compare November 22, 2026 03:48
@danlapid
danlapid marked this pull request as ready for review November 22, 2026 04:06
@danlapid
danlapid requested review from a team as code owners November 22, 2026 04:06

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: RequestTraceScope still 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 to getMetrics().getSpan(), and the new incomingRequests.empty() guard keeps it from calling getMetrics() 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 same Span.
  • AsyncLocalStorage.bind(): in the root frame it now passes the request validator, which matches snapshot(). The existing bound-als-test runs this path.
  • AsyncResource: unchanged, as async-resource-request-context-test expects.

⚡ 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

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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

@danlapid
danlapid merged commit 3cc9a5a into main Nov 24, 2026
23 of 24 checks passed
@danlapid
danlapid deleted the dlapid/shared-trace-frame branch November 24, 2026 18:44
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.

2 participants