Skip to content

fix(cloudflare): Instrument DO RPC methods on the prototype, not a Proxy - #23057

Merged
JPeer264 merged 1 commit into
developfrom
jp/cloudflare-agent-rpc-proxy-private-fields
Aug 7, 2026
Merged

fix(cloudflare): Instrument DO RPC methods on the prototype, not a Proxy#23057
JPeer264 merged 1 commit into
developfrom
jp/cloudflare-agent-rpc-proxy-private-fields

Conversation

@JPeer264

@JPeer264 JPeer264 commented Aug 5, 2026

Copy link
Copy Markdown
Member

closes #23040
closes #23040

This is getting rid completely of the Proxy for methods on the DO entirely. It seems private calls were not working properly with Proxies, therefore we need to switch entirely to a prototype-wrapping. This was in already before in instrumentPrototypeMethods and moved over to a Proxy.

I also did a benchmark on a deployed worker instance and it seems that there is no significant performance difference - they're even mostly the same in performance. So there is only a win doing this.

The main difference is that before every call was over the Proxy, which had the overhead, and now the overhead is on the instance creation of the DurableObject once.

The big downside

We had to add resolveFrameworkManagedMethods to it, that detects if the library is changing the prototypes. There is e.g. a case in agents where they change the callable fields and update it on prototype level. They later check on callableMetadata.get and check if the method is there - if we would overwrite it: 💥

Not this check could have been solely done for that workaround in their library, but I don't think this is a one-off and tried to keep it generic. The good part is that this only happens when the instance of the DO is created (which is not once per request)

https://github.com/cloudflare/agents/blob/3172a232d2c663db6bff126c7ca1ccddb5beb45d/packages/agents/src/index.ts#L3618-L3626


Clanker description:

The RPC instrumentation returned a Proxy of the instance. The runtime stores that object and, on dispatch, resolves the method on the prototype and invokes it with the stored object as receiver. A stored Proxy has no private-field brand, so classes with native private fields throw. The Proxy traps cannot help, the method is read from the prototype, so no trap is consulted.

Wrap the RPC methods on the class prototype instead (once per class), with per-instance state in a WeakMap keyed on the instance. The prototype is also the only interception point the runtime accepts for class targets: it rejects own instance properties and dispatches prototype properties only (workerd's tryGetProperty).

@JPeer264 JPeer264 self-assigned this Aug 5, 2026
@linear-code

linear-code Bot commented Aug 5, 2026

Copy link
Copy Markdown

JS-3283

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

⚠️ Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

Path Size % Change Change
@sentry/browser 30.15 kB - -
@sentry/browser - with treeshaking flags 28.35 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 26.66 kB - -
@sentry/browser (incl. Tracing) 47.56 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 47.57 kB - -
@sentry/browser (incl. Tracing, Profiling) 52.31 kB - -
@sentry/browser (incl. Tracing, Replay) 86.93 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 76.36 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 91.66 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 104.29 kB - -
@sentry/browser (incl. Feedback) 47.48 kB - -
@sentry/browser (incl. sendFeedback) 34.98 kB - -
@sentry/browser (incl. FeedbackAsync) 40.13 kB - -
@sentry/browser (incl. Metrics) 31.22 kB - -
@sentry/browser (incl. Logs) 31.44 kB - -
@sentry/browser (incl. Metrics & Logs) 32.13 kB - -
@sentry/react 31.95 kB - -
@sentry/react (incl. Tracing) 49.81 kB - -
@sentry/vue 35.25 kB - -
@sentry/vue (incl. Tracing) 49.57 kB - -
@sentry/svelte 30.17 kB - -
CDN Bundle 32.16 kB - -
CDN Bundle (incl. Tracing) 47.83 kB - -
CDN Bundle (incl. Logs, Metrics) 33.7 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.21 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 73.03 kB - -
CDN Bundle (incl. Tracing, Replay) 85.47 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 86.79 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 91.29 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.61 kB - -
CDN Bundle - uncompressed 95.33 kB - -
CDN Bundle (incl. Tracing) - uncompressed 142.84 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 99.96 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 146.82 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 224.66 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 262.09 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 266.06 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 275.8 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 279.75 kB - -
@sentry/nextjs (client) 52.39 kB - -
@sentry/sveltekit (client) 48.01 kB - -
@sentry/core/server 65.58 kB - -
@sentry/core/browser 51.82 kB - -
@sentry/node 119.28 kB -0.01% -1 B 🔽
@sentry/node/import (ESM hook with diagnostics-channel injection) 0 B added added
@sentry/node - without tracing 83.3 kB - -
@sentry/aws-serverless 92.61 kB -0.01% -1 B 🔽
@sentry/cloudflare (withSentry) - minified 214.15 kB +0.73% +1.54 kB 🔺
@sentry/cloudflare (withSentry) 528.8 kB +0.75% +3.91 kB 🔺

View base workflow run

Base automatically changed from jp/cloudflare-remove-instrument-prototype-methods to develop August 5, 2026 14:57
@JPeer264
JPeer264 force-pushed the jp/cloudflare-agent-rpc-proxy-private-fields branch 5 times, most recently from 55d51ed to cee3b62 Compare August 6, 2026 08:20
@JPeer264
JPeer264 marked this pull request as ready for review August 6, 2026 08:20
@JPeer264
JPeer264 requested a review from a team as a code owner August 6, 2026 08:20
@JPeer264
JPeer264 requested review from isaacs and mydea and removed request for a team August 6, 2026 08:20
Comment thread packages/cloudflare/src/durableobject.ts
@andreiborza
andreiborza requested review from andreiborza, chargome and nicohrubec and removed request for isaacs and mydea August 6, 2026 12:51

@andreiborza andreiborza left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Bit hard to follow, but didn't spot anything 😅

{ options, context, spanName: prop, spanOp: 'rpc', origin: 'auto.faas.cloudflare.durable_object' },
boundMethod,
const wrapped = createRpcPrototypeWrapper(methodName, descriptor.value as UncheckedMethod);
Object.defineProperty(prototype, methodName, { ...descriptor, value: wrapped });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

q: I think this can easily crash at runtime? Would trycatch here

@JPeer264 JPeer264 Aug 7, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch. Will follow up on that 👀

follow-up: #23132

The RPC instrumentation returned a Proxy of the instance. The runtime stores
that object and, on dispatch, resolves the method on the prototype and invokes
it with the stored object as receiver. A stored Proxy has no private-field
brand, so classes with native private fields throw. The Proxy traps cannot help
— the method is read from the prototype, so no trap is consulted.

Wrap the RPC methods on the class prototype instead (once per class), with
per-instance state in a WeakMap keyed on the instance. The prototype is also the
only interception point the runtime accepts for class targets: it rejects own
instance properties and dispatches prototype properties only (workerd's
`tryGetProperty`).
@JPeer264
JPeer264 force-pushed the jp/cloudflare-agent-rpc-proxy-private-fields branch from cee3b62 to 33c3003 Compare August 7, 2026 06:36
@JPeer264
JPeer264 merged commit 454a4c9 into develop Aug 7, 2026
81 checks passed
@JPeer264
JPeer264 deleted the jp/cloudflare-agent-rpc-proxy-private-fields branch August 7, 2026 07:18
JPeer264 added a commit that referenced this pull request Aug 7, 2026
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.

Cloudflare Agent RPC proxy breaks native private fields

3 participants