Skip to content

@sentry/cloudflare: withSentry proxies env and ctx.exports, so a Durable Object method called on a proxied receiver throws on #private fields #27046

Done

Description

@kazukiiwas

What happens

Wrapping a Worker entry with withSentry makes every Durable Object method call fail when the object uses ECMAScript private fields:

TypeError: Cannot access invalid private field (evaluating 'this.#db')
    at db (services/api/src/durable-objects/content.ts:525:8)

The Worker works without the wrapper and fails with it, with no DSN configured as well as with one.

Why

withSentry (@sentry/cloudflare, build/esm/prod/withSentry.js:21) wraps the handler, and the request path instruments the bindings the handler receives:

  • instrumentEnv (build/esm/prod/instrumentations/worker/instrumentEnv.js:34) returns new Proxy(env, …), and its get trap replaces D1, Queues, KV, R2, Workers AI and Durable Object namespaces with instrumented proxies (:34-95), plus instrumentServiceStub (:96) which proxies the stub object itself.
  • utils/instrumentContext.js:53 runs ctx.exports through the same instrumentEnv.

A Durable Object method invoked through a proxied stub therefore runs with a proxy as this. ECMAScript private fields (#db) are only accessible on the real instance, so the method throws before the body runs. Any class-based DO that keeps its state in #fields is affected; the docs' caveat about instrumented receivers covers RPC trace arguments, not this.

Minimal reproduction

// worker.ts
class Counter {
  #n = 0;                                   // private field is the trigger
  increment() { return ++this.#n; }
}
export { Counter };

export default Sentry.withSentry(
  (env) => ({ dsn: env.SENTRY_DSN, tracesSampleRate: 1 }),
  {
    async fetch(request, env) {
      const id = env.COUNTER.idFromName('a');
      // env.COUNTER is the SDK's proxy; get() returns a proxied stub
      return new Response(String(await env.COUNTER.get(id).increment()));
    },
  },
);

Symptom: TypeError: Cannot access invalid private field. Same Worker without withSentry → 1.

A non-Proxied control: the same DO with this.n (a public field) works, which is why simple examples in the docs do not show it.

What would fix it

Either, in preference order:

  1. An option on withSentry / CloudflareOptions that skips instrumentEnv for named bindings (or entirely), e.g. instrumentBindings: false. rpcTracePropagationBindings defaults to [], so nothing in the current options controls the proxying itself.
  2. Instrumentation that preserves the receiver: wrap the methods on the stub with Reflect.apply(target, receiver), or copy the namespace's get() result with a bound this, so the DO still sees the real instance.

Impact we hit

A Hono API Worker (Durable Object for content, private #db field) could not ship exception tracking at all: its save integration tests failed with SAVE_UNAVAILABLE, and we had to leave the Worker unwrapped. Repo: waccele/favhub, PR #3277, issue #3428.

Activity

  1. linear-code commented on Dec 2, 2026

    @linear-code
  2. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Dec 2, 2026
  3. self-assigned this
    on Dec 2, 2026
  4. andreiborza commented on Dec 2, 2026

    @andreiborza
    Member

    Hi @kazukiiwas, thank you for filing this. We'll look into it.

  5. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Dec 2, 2026
  6. added a commit that references this issue on Dec 2, 2026
    97e65a4
  7. github-actions commented on Dec 2, 2026

    @github-actions
    Contributor

    A PR closing this issue has just been released 🚀

    This issue was referenced by PR #27057, which was included in the 11.23.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions