Skip to content

fix(cloudflare): Fork the isolation scope for Durable Object methods - #22969

Open
JPeer264 wants to merge 2 commits into
developfrom
jp/cloudflare-do-isolation-scope
Open

fix(cloudflare): Fork the isolation scope for Durable Object methods#22969
JPeer264 wants to merge 2 commits into
developfrom
jp/cloudflare-do-isolation-scope

Conversation

@JPeer264

@JPeer264 JPeer264 commented Aug 3, 2026

Copy link
Copy Markdown
Member

This came up while trying to reuse one client instead of creating new clients per request. We had the logic with currentClient ? withScope : withIsolationScope to identify if a new isolation scope should be created or not. The main issue is that if the client exists we use withScope, which actually reuses the wrong isolation scope instead - this results in leaking data in one request into the next. The scopes are independent of clients, and will also be in the future, as the goal is to only have 1 client in the future for multiple requests.

It seems this already came up a long time ago for Nuxt where it got fixed:

const newIsolationScope =
isolationScope === getDefaultIsolationScope() ? isolationScope.clone() : isolationScope;
(so in theory this could be removed, but keeping this wouldn't harm unless we're 100% sure that this can go away cc @s1gr1d)


Clanker description

setUser/setTag write to the isolation scope, and a Durable Object keeps that scope across invocations. Methods only forked the current scope while no client was bound — but disposing a client at the invocation boundary does not unbind it, so from the second invocation onward the still-assigned client made every entry point look reentrant and skip its fork. Data from one invocation thus reappeared on the next, and a user identity could attach itself to an unrelated event.

An instrumented handler is either an invocation's entry point or reentrant (a DO method calling its own fetch, an RPC method reaching a sibling). Only the entry point may fork; a bound client can't distinguish the two, so withInvocationIsolationScope records the fact directly as a marker in SDK processing metadata (stripped before send). The stack fallback doesn't clone, so its scope is left unmarked rather than making every later entry point look reentrant. Forking loses nothing — it clones, inheriting enclosing request data — and matches how the Worker fetch path already behaves.

Covered by integration tests against a real Durable Object (consecutive invocations, a nested direct call, a nested call onto the instrumented fetch) and unit tests for the reentrancy logic plus the instrumentWorkerEntrypoint RPC and webSocketMessage/alarm consumers.

@JPeer264 JPeer264 self-assigned this Aug 3, 2026
@JPeer264
JPeer264 force-pushed the jp/cloudflare-do-isolation-scope branch 2 times, most recently from f0fbaa4 to 8c4579c Compare August 4, 2026 08:49
`setUser`/`setTag` write to the isolation scope, and a Durable Object keeps that scope
across invocations. Methods only forked the current scope while no client was bound — but
disposing a client at the invocation boundary does not unbind it, so from the second
invocation onward the still-assigned client made every entry point look reentrant and
skip its fork. Data from one invocation thus reappeared on the next, and a user identity
could attach itself to an unrelated event.

An instrumented handler is either an invocation's entry point or reentrant (a DO method
calling its own `fetch`, an RPC method reaching a sibling). Only the entry point may fork;
a bound client can't distinguish the two, so `withInvocationIsolationScope` records the
fact directly as a marker in SDK processing metadata (stripped before send). The stack
fallback doesn't clone, so its scope is left unmarked rather than making every later entry
point look reentrant. Forking loses nothing — it clones, inheriting enclosing request data —
and matches how the Worker `fetch` path already behaves.

Covered by integration tests against a real Durable Object (consecutive invocations, a
nested direct call, a nested call onto the instrumented `fetch`) and unit tests for the
reentrancy logic plus the `instrumentWorkerEntrypoint` RPC and `webSocketMessage`/`alarm`
consumers.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 29.91 kB - -
@sentry/browser - with treeshaking flags 28.12 kB - -
@sentry/browser (incl. Tracing) 47.36 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 47.36 kB - -
@sentry/browser (incl. Tracing, Profiling) 52.13 kB - -
@sentry/browser (incl. Tracing, Replay) 86.7 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 76.13 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 91.41 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 104.05 kB - -
@sentry/browser (incl. Feedback) 47.22 kB - -
@sentry/browser (incl. sendFeedback) 34.76 kB - -
@sentry/browser (incl. FeedbackAsync) 39.87 kB - -
@sentry/browser (incl. Metrics) 30.99 kB - -
@sentry/browser (incl. Logs) 31.22 kB - -
@sentry/browser (incl. Metrics & Logs) 31.9 kB - -
@sentry/react 31.73 kB - -
@sentry/react (incl. Tracing) 49.61 kB - -
@sentry/vue 34.98 kB - -
@sentry/vue (incl. Tracing) 49.34 kB - -
@sentry/svelte 29.94 kB - -
CDN Bundle 32.01 kB - -
CDN Bundle (incl. Tracing) 47.72 kB - -
CDN Bundle (incl. Logs, Metrics) 33.55 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.08 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 72.9 kB - -
CDN Bundle (incl. Tracing, Replay) 85.35 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 86.65 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 91.14 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.46 kB - -
CDN Bundle - uncompressed 95.52 kB - -
CDN Bundle (incl. Tracing) - uncompressed 143.1 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 100.23 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 147.08 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 224.99 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 262.36 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 266.33 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 276.07 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 280.02 kB - -
@sentry/nextjs (client) 52.21 kB - -
@sentry/sveltekit (client) 47.81 kB - -
@sentry/core/server 80.01 kB - -
@sentry/core/browser 51.97 kB - -
@sentry/node 120.89 kB - -
@sentry/node/import (ESM hook with diagnostics-channel injection) 0 B added added
@sentry/node - without tracing 84.26 kB -0.01% -1 B 🔽
@sentry/aws-serverless 92.94 kB +0.01% +1 B 🔺
@sentry/cloudflare (withSentry) - minified 219.45 kB +0.04% +80 B 🔺
@sentry/cloudflare (withSentry) 540.76 kB +0.06% +290 B 🔺

View base workflow run

@JPeer264
JPeer264 force-pushed the jp/cloudflare-do-isolation-scope branch from 8c4579c to edb2f4f Compare August 4, 2026 09:10
@JPeer264
JPeer264 marked this pull request as ready for review August 4, 2026 09:46
@JPeer264
JPeer264 requested a review from a team as a code owner August 4, 2026 09:46
@JPeer264
JPeer264 requested review from Lms24, andreiborza and isaacs and removed request for a team and isaacs August 4, 2026 09:46
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