fix(cloudflare): Fork the isolation scope for Durable Object methods - #22969
Draft
JPeer264 wants to merge 1 commit into
Draft
fix(cloudflare): Fork the isolation scope for Durable Object methods#22969JPeer264 wants to merge 1 commit into
JPeer264 wants to merge 1 commit into
Conversation
`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>
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.
This came up while trying to reuse one client instead of creating new clients per request. We had the logic with
currentClient ? withScope : withIsolationScopeto identify if a new isolation scope should be created or not. The main issue is that if the client exists we usewithScope, 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:
sentry-javascript/packages/nuxt/src/runtime/plugins/sentry-cloudflare.server.ts
Lines 81 to 83 in 87db4d2
Clanker description
setUser/setTagwrite 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, sowithInvocationIsolationScoperecords 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 Workerfetchpath 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 theinstrumentWorkerEntrypointRPC andwebSocketMessage/alarmconsumers.