fix(cloudflare): Instrument DO RPC methods on the prototype, not a Proxy - #23057
Open
JPeer264 wants to merge 1 commit into
Open
fix(cloudflare): Instrument DO RPC methods on the prototype, not a Proxy#23057JPeer264 wants to merge 1 commit into
JPeer264 wants to merge 1 commit into
Conversation
Contributor
size-limit report 📦
|
Base automatically changed from
jp/cloudflare-remove-instrument-prototype-methods
to
develop
August 5, 2026 14:57
JPeer264
force-pushed
the
jp/cloudflare-agent-rpc-proxy-private-fields
branch
4 times, most recently
from
August 6, 2026 08:10
32f9837 to
55d51ed
Compare
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
force-pushed
the
jp/cloudflare-agent-rpc-proxy-private-fields
branch
from
August 6, 2026 08:20
55d51ed to
cee3b62
Compare
JPeer264
marked this pull request as ready for review
August 6, 2026 08:20
andreiborza
requested review from
andreiborza,
chargome and
nicohrubec
and removed request for
isaacs and
mydea
August 6, 2026 12:51
andreiborza
approved these changes
Aug 6, 2026
andreiborza
left a comment
Member
There was a problem hiding this comment.
Bit hard to follow, but didn't spot anything 😅
chargome
reviewed
Aug 6, 2026
| { 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 }); |
Member
There was a problem hiding this comment.
q: I think this can easily crash at runtime? Would trycatch here
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.
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
instrumentPrototypeMethodsand 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
resolveFrameworkManagedMethodsto it, that detects if the library is changing the prototypes. There is e.g. a case inagentswhere they change thecallablefields and update it on prototype level. They later check oncallableMetadata.getand 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).