Skip to content

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

Open
JPeer264 wants to merge 1 commit into
developfrom
jp/cloudflare-agent-rpc-proxy-private-fields
Open

fix(cloudflare): Instrument DO RPC methods on the prototype, not a Proxy#23057
JPeer264 wants to merge 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.12 kB - -
@sentry/browser - with treeshaking flags 28.32 kB - -
@sentry/browser (incl. Tracing) 47.53 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 47.54 kB - -
@sentry/browser (incl. Tracing, Profiling) 52.29 kB - -
@sentry/browser (incl. Tracing, Replay) 86.91 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 76.33 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 91.6 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 104.25 kB - -
@sentry/browser (incl. Feedback) 47.45 kB - -
@sentry/browser (incl. sendFeedback) 34.96 kB - -
@sentry/browser (incl. FeedbackAsync) 40.1 kB - -
@sentry/browser (incl. Metrics) 31.2 kB - -
@sentry/browser (incl. Logs) 31.41 kB - -
@sentry/browser (incl. Metrics & Logs) 32.11 kB - -
@sentry/react 31.93 kB - -
@sentry/react (incl. Tracing) 49.79 kB - -
@sentry/vue 35.2 kB - -
@sentry/vue (incl. Tracing) 49.51 kB - -
@sentry/svelte 30.14 kB - -
CDN Bundle 32.13 kB - -
CDN Bundle (incl. Tracing) 47.81 kB - -
CDN Bundle (incl. Logs, Metrics) 33.67 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.19 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 73.02 kB - -
CDN Bundle (incl. Tracing, Replay) 85.45 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 86.77 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 91.27 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.59 kB - -
CDN Bundle - uncompressed 95.27 kB - -
CDN Bundle (incl. Tracing) - uncompressed 142.78 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 99.9 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 146.75 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 224.6 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 262.03 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 266 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 275.74 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 279.69 kB - -
@sentry/nextjs (client) 52.36 kB - -
@sentry/sveltekit (client) 47.99 kB - -
@sentry/core/server 65.54 kB - -
@sentry/core/browser 51.82 kB - -
@sentry/node 120.46 kB - -
@sentry/node/import (ESM hook with diagnostics-channel injection) 0 B added added
@sentry/node - without tracing 84.11 kB -0.01% -1 B 🔽
@sentry/aws-serverless 92.61 kB +0.01% +2 B 🔺
@sentry/cloudflare (withSentry) - minified 219.27 kB +0.68% +1.46 kB 🔺
@sentry/cloudflare (withSentry) 540.68 kB +0.68% +3.62 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 4 times, most recently from 32f9837 to 55d51ed Compare August 6, 2026 08:10
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 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

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