Skip to content

fix(cloudflare): Keep framework-managed methods when a Durable Object constructor throws - #25851

Merged
JPeer264 merged 2 commits into
getsentry:developfrom
nuntz:fix/cloudflare-framework-managed-methods-after-constructor-throw
Nov 10, 2026
Merged

JPeer264 merged 2 commits into
getsentry:developfrom
nuntz:fix/cloudflare-framework-managed-methods-after-constructor-throw

Conversation

@nuntz

@nuntz nuntz commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Why: When a Durable Object's constructor throws after a framework has already installed its method wrappers, the next construction reads those wrappers as the originals. Nothing then looks framework-managed, so Sentry wraps them again, and the framework's identity-keyed dispatch breaks. With the agents SDK, @callable() methods are wrapped before storage initialization, so a constructor that fails once (for example at a storage quota limit) leaves later RPCs failing with Method callTool is not callable.

The fix keeps the methods collected before the first construction attempt until a construction succeeds, then drops them; after that, the existing frameworkManagedMethodsCache takes over.

How to review: resolveMethodsBeforeFirstConstruction in durableobject.ts, plus the new test in the framework-managed methods block, which fails on develop.

Unsure about: I keep the snapshot until the first successful construction rather than caching the result of a failed one. A framework that installs its wrappers on a later attempt would still be detected.

The same change, applied as a pnpm patch to 11.4.0, fixes the failure in our app's Agent recovery test.

Closes #25728

🤖 Generated with Claude Code

… constructor throws

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nuntz
nuntz requested a review from a team as a code owner October 29, 2026 05:13
@nuntz
nuntz requested review from andreiborza and mydea and removed request for a team October 29, 2026 05:13
@github-actions github-actions Bot added the external PR from an external contributor label Oct 29, 2026
@JosephDoUrden

Copy link
Copy Markdown
Contributor

Read through constructInstrumentedDurableObject and the two caches (not run locally). The snapshot is keyed by prototype, taken once before the first attempt and dropped when resolveFrameworkManagedMethods writes the managed set, so a successful construction ends the special case and the cached path never creates one.

On the open question: keeping the snapshot until a construction succeeds is the right call. Caching the result of a failed attempt would record an empty managed set whenever the throw happens before the framework installs its wrappers, and that would be permanent. With the snapshot held, a framework that wraps on attempt one and skips on attempt two (your test), or re-wraps on every attempt, is detected either way, since the comparison is always against the pre-wrapper originals.

@JPeer264
JPeer264 requested review from a team as code owners November 10, 2026 13:55
@JPeer264
JPeer264 requested review from a team and chargome and removed request for a team November 10, 2026 13:55

@JPeer264 JPeer264 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.

Thanks a lot for fixing that.

I keep the snapshot until the first successful construction rather than caching the result of a failed one. A framework that installs its wrappers on a later attempt would still be detected.

That's fine and I think even the best solution for it

On top I actually adapted our e2e test that this is also covered

@JPeer264
JPeer264 merged commit 39b10d8 into getsentry:develop Nov 10, 2026
377 checks passed
msonnb pushed a commit that referenced this pull request Nov 10, 2026
This PR adds the external contributor to the CHANGELOG.md file, so that
they are credited for their contribution. See #25851

Co-authored-by: JPeer264 <10677263+JPeer264@users.noreply.github.com>
nuntz added a commit to nuntz/mise that referenced this pull request Nov 11, 2026
… goes (#42)

@sentry/cloudflare 11.4.0 -> 11.12.0, which ships getsentry/sentry-javascript#25851,
the upstream form of our patch. The patch and its patchedDependencies entry are gone.
agent-recovery.test.ts fails on 11.4.0 without the patch and passes on 11.12.0.

What upstream changed, 11.5-11.12, and what in Mise reads it:
- Durable Objects now run Sentry.init() inside construction (#26218). Household
  (instrumentAgentWithSentry) gets it: errors thrown while a Household is being
  constructed now reach Sentry, so the weekly `bun run sentry` sweep can show
  issues that were invisible before. sentryOptions sets no cacheClient, so this
  applies. The repeated-init warning (11.5) does not fire in the worker tests.
- Spans for SQL on pi_ and cf_ tables are skipped (11.6). agentLog.ts reads
  pi_entries, so /agent-log's queries leave no db spans. Nothing in Mise reads
  db spans; row counts come from metering.ts cursors, not spans.
- Request spans set client.address from CF-Connecting-IP (11.11) only when
  dataCollection.userInfo is on; sentryOptions turns it off. Response header
  attributes follow the same SAFE_HEADERS allow-list.
- Workers skip spans for GETs of common static assets (11.10). Static assets
  are served before the Worker runs, so the sweep's http.server rows lose
  nothing they had.
- Aborted http client spans are no longer errors (11.7): node:http only. Mise
  fetches, so the sweep's failed spans are unchanged.
- Queues, KV, Workflows, cron monitoring, logs and metrics, tunnels, MCP, Vite
  instrumentation: Mise uses none of them. The other fixes are in span
  timestamps, client reports, linked errors and stack paths, which nothing in
  Mise reads.

Closes #42

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external PR from an external contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

instrumentAgentWithSentry: @callable() methods are lost after a constructor throws once

3 participants