Title: @sentry/cloudflare: captureException inside fire-and-forget waitUntil catch silently dropped — client disposed before promise rejects · Issue #20533 · getsentry/sentry-javascript · GitHub
Open Graph Title: @sentry/cloudflare: captureException inside fire-and-forget waitUntil catch silently dropped — client disposed before promise rejects · Issue #20533 · getsentry/sentry-javascript
X Title: @sentry/cloudflare: captureException inside fire-and-forget waitUntil catch silently dropped — client disposed before promise rejects · Issue #20533 · getsentry/sentry-javascript
Description: Summary @sentry/cloudflare silently drops Sentry.captureException calls made inside a .catch() attached to a promise passed to ctx.waitUntil(...) (the canonical fire-and-forget pattern in Cloudflare Workers / Durable Objects). The client...
Open Graph Description: Summary @sentry/cloudflare silently drops Sentry.captureException calls made inside a .catch() attached to a promise passed to ctx.waitUntil(...) (the canonical fire-and-forget pattern in Cloudflar...
X Description: Summary @sentry/cloudflare silently drops Sentry.captureException calls made inside a .catch() attached to a promise passed to ctx.waitUntil(...) (the canonical fire-and-forget pattern in Cloudflar...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/20533
X: @github
Domain: github.com
IPv4 address: 140.82.113.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"@sentry/cloudflare: captureException inside fire-and-forget waitUntil catch silently dropped — client disposed before promise rejects","articleBody":"## Summary\n\n`@sentry/cloudflare` silently drops `Sentry.captureException` calls made inside a `.catch()` attached to a promise passed to `ctx.waitUntil(...)` (the canonical fire-and-forget pattern in Cloudflare Workers / Durable Objects). The client is `flush()`+`dispose()`d as soon as the wrapped fetch handler returns its `Response`, so any later capture inside the still-running background promise has no live transport to send through.\n\nThis affects the most common 202-Accepted dispatch pattern in Workers, where the request handler returns immediately and offloads work via `waitUntil`.\n\n## SDK version\n- `@sentry/cloudflare@10.50.0`\n- Verified by reading `build/esm/request.js` and `build/esm/flush.js` in this version. The same code shape exists at `packages/cloudflare/src/request.ts` and `packages/cloudflare/src/flush.ts` on `main` at the time of writing.\n\n## Environment\n- Runtime: Cloudflare Workers + Durable Objects\n- Wrapping: `withSentry` on default fetch handler, `instrumentDurableObjectWithSentry` on a `DurableObject` subclass\n- The DO `fetch` returns a 202 `Response` synchronously and offloads its real work via `this.ctx.waitUntil(promise.catch(handler))`\n\n## Minimal reproducer\n\n```ts\n// reproducer-worker.ts\nimport * as Sentry from \"@sentry/cloudflare\";\nimport { DurableObject } from \"cloudflare:workers\";\n\nclass FailingDOBase extends DurableObject {\n async fetch(_req: Request) {\n const work = (async () => {\n throw new Error(\"background-throw\"); // simulated unhandled error\n })().catch((err) => {\n // Expected: this captureException reaches Sentry.\n // Actual: silently dropped — the SDK already disposed the client\n // when the 202 below was returned.\n Sentry.captureException(err);\n });\n\n this.ctx.waitUntil(work); // keeps invocation alive until `work` settles\n return new Response(\"accepted\", { status: 202 });\n }\n}\n\nexport const FailingDO = Sentry.instrumentDurableObjectWithSentry(\n (env: { SENTRY_DSN: string }) => ({\n dsn: env.SENTRY_DSN,\n enabled: true,\n }),\n FailingDOBase,\n);\n\ninterface Env { FAILING_DO: DurableObjectNamespace; SENTRY_DSN: string; }\n\nexport default Sentry.withSentry(\n (env: Env) => ({ dsn: env.SENTRY_DSN, enabled: true }),\n {\n async fetch(request: Request, env: Env, _ctx: ExecutionContext) {\n const id = env.FAILING_DO.idFromName(\"repro\");\n return env.FAILING_DO.get(id).fetch(request);\n },\n },\n);\n```\n\n```jsonc\n// wrangler.jsonc (minimal)\n{\n \"name\": \"sentry-fire-and-forget-repro\",\n \"main\": \"reproducer-worker.ts\",\n \"compatibility_date\": \"2026-04-24\",\n \"compatibility_flags\": [\"nodejs_compat\"],\n \"durable_objects\": {\n \"bindings\": [{ \"name\": \"FAILING_DO\", \"class_name\": \"FailingDO\" }]\n },\n \"migrations\": [\n { \"tag\": \"v1\", \"new_sqlite_classes\": [\"FailingDO\"] }\n ],\n \"vars\": {\n \"SENTRY_DSN\": \"\u003cyour-dsn>\"\n }\n}\n```\n\n`curl https://\u003cworker>.workers.dev/` returns `202 accepted` immediately. The background promise rejects ~tens of ms later. **No event ever appears in Sentry.** The `console.error` from a manual `console.error` line in the catch *does* appear in `wrangler tail`, confirming the catch ran.\n\nFor comparison, the same code path inside the *synchronous* fetch handler body (i.e. `throw new Error(...)` before the `Response` is returned) is captured correctly. Only the `waitUntil` path is silent.\n\n## Why it happens\n\n`packages/cloudflare/src/request.ts` (in 10.50.0, `build/esm/request.js:78–148` of the published bundle):\n\n```ts\nreturn startSpanManual({ name, attributes }, async span => {\n let res;\n try {\n res = await handler(); // user's fetch handler returns 202\n setHttpStatus(span, res.status);\n } catch (e) { ... }\n\n // Non-streaming response branch:\n span.end();\n if (res.status === 101) {\n waitUntil?.(client?.flush(2000));\n } else {\n waitUntil?.(flushAndDispose(client)); // \u003c-- client is disposed here\n }\n return res;\n});\n```\n\n`packages/cloudflare/src/flush.ts` → `flushAndDispose`:\n\n```ts\nasync function flushAndDispose(client, timeout = 2000) {\n await client.flush(timeout);\n client.dispose(); // \u003c-- terminates the transport\n}\n```\n\nThis runs the moment the user handler returns, **before** any promise the user passed to their own `ctx.waitUntil` rejects. Once `client.dispose()` runs, subsequent `captureException` calls land in an isolation scope whose client is dead — the envelope is never sent. The invocation itself stays alive because Cloudflare keeps it up for the user's `waitUntil` promise, but the SDK has already torn down its transport.\n\n`makeFlushLock` (in `flush.ts`) already tracks every promise registered via `ctx.waitUntil`, exposing a `ready` promise that resolves when *all* of them settle. But `flushAndDispose` does not consult it — it disposes immediately after the handler returns.\n\n## Suggested fix direction\n\nMake `flushAndDispose` await the flush-lock's `ready` before disposing, so the client outlives every fire-and-forget promise:\n\n```ts\n// pseudo-diff\nasync function flushAndDispose(client, flushLock, timeout = 2000) {\n // Wait for all ctx.waitUntil(...) promises that were registered during this invocation\n await flushLock.finalize(); // resolves immediately if nothing pending; else waits\n await client.flush(timeout);\n client.dispose();\n}\n```\n\nThis means at the wrapper site:\n\n```ts\n// instead of\nwaitUntil?.(flushAndDispose(client));\n\n// would be\nwaitUntil?.(flushAndDispose(client, flushLock));\n```\n\nTradeoff: invocations with long-running `waitUntil` work would keep the Sentry client alive until that work finishes — which is exactly the desired semantics for fire-and-forget patterns. The cost is that flush no longer happens at the moment the response is returned, but that's already what users using `waitUntil` accept.\n\nAlternatively (less invasive): document this limitation explicitly in the Cloudflare integration docs, and recommend that fire-and-forget catches do `Sentry.captureException(...)` + `await Sentry.flush(2000)` *and* re-init the client inside the catch — though re-init is awkward in an isolation-scope-aware world.\n\n## Real-world impact\n\nWe hit this in a production worker we operate. A `TypeError` deep inside our DO's `executeTurn()` was caught by the same fire-and-forget pattern shown in the reproducer. Both `console.error` and `Sentry.captureException` ran, but **Sentry stayed at 0 events for hours** while user-facing requests silently froze. Diagnosis required `wrangler tail` to see the `console.error` line; the SDK gave us no signal.\n\nWe've worked around it locally by also reporting the failure to our own backend over a separate HTTP callback (so the user gets an immediate error UI), but Sentry observability for this class of failure is still effectively off until this is fixed in the SDK.\n\nThanks for the SDK!\n","author":{"url":"https://github.com/zzzarit-bulldozer","@type":"Person","name":"zzzarit-bulldozer"},"datePublished":"2026-04-27T17:02:23Z","url":"https://github.com/getsentry/sentry-javascript/issues/20533"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:8a5b3966-802f-dbf7-2276-f04c91b7be8d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9560:9F9AF:39A5E1:4E63F8:6B043F60 |
| html-safe-nonce | 32f74b816de59921efd17cd848613c676ad566b02bc5ea3f12446b296d3c546e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NTYwOjlGOUFGOjM5QTVFMTo0RTYzRjg6NkIwNDNGNjAiLCJ2aXNpdG9yX2lkIjoiNzE4NjIzMDM2NzA0MTkyOTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | edd1cfa4a935224b3d06829b9dc84c8dcf59bb1f1fc3ccbc33d632515c3946b9 |
| hovercard-subject-tag | issue:4337489916 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-javascript/20533/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e798a75039123348c9f0a5fc99432c70f67b3b5142b3d640c4a6de6c438e942e/getsentry/sentry-javascript/issues/20533 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e798a75039123348c9f0a5fc99432c70f67b3b5142b3d640c4a6de6c438e942e/getsentry/sentry-javascript/issues/20533 |
| og:image:alt | Summary @sentry/cloudflare silently drops Sentry.captureException calls made inside a .catch() attached to a promise passed to ctx.waitUntil(...) (the canonical fire-and-forget pattern in Cloudflar... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | zzzarit-bulldozer |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9185ccfefd80c1ff2d9ddcdb0cd2becf46b5a1efbbf30741a895e4bb8de25e64 |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-javascript git https://github.com/getsentry/sentry-javascript.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3328572 |
| octolytics-dimension-repository_nwo | getsentry/sentry-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3328572 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 06dc448a76fe4751ac1db26524aefa3aa3e889a8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width