Skip to content

fix(cloudflare): avoid repeated flush lock wrapping - #21156

Merged
logaretm merged 3 commits into
developfrom
awad/js-2605-makeflushlock-grows-a-waituntil-wrapper-chain-on-every-init
May 26, 2026
Merged

fix(cloudflare): avoid repeated flush lock wrapping#21156
logaretm merged 3 commits into
developfrom
awad/js-2605-makeflushlock-grows-a-waituntil-wrapper-chain-on-every-init

Conversation

@logaretm

@logaretmlogaretm commented May 25, 2026

Copy link
Copy Markdown
Member

Durable Objects can reuse the same instrumented context across many requests. makeFlushLock() replaced context.waitUntil on every init, so each request wrapped the previous wrapper and built an unbounded call chain.

A later waitUntil() call then had to recurse through every wrapper, eventually causing stack overflows and retaining stale closures.

I added a mechanism to reuse a single waitUntil wrapper per Cloudflare context instead of wrapping again on every SDK init. Also added a regression coverage for long-lived contexts repeatedly creating flush locks.

Fixes#21150

@linear-code

Copy link
Copy Markdown

JS-2605

@logaretmlogaretm changed the title test(cloudflare): reproduce flush lock wrapper growthfix(cloudflare): avoid repeated flush lock wrappingMay 25, 2026
@logaretm
logaretm marked this pull request as ready for review May 25, 2026 11:01
@logaretm
logaretm requested a review from a team as a code ownerMay 25, 2026 11:01
@logaretm
logaretm requested review from JPeer264 and andreiborza and removed request for a teamMay 25, 2026 11:01
@github-actions

github-actionsBot commented May 25, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

PathSize% ChangeChange
@sentry/browser27.32 kB--
@sentry/browser - with treeshaking flags25.74 kB--
@sentry/browser (incl. Tracing)45.31 kB--
@sentry/browser (incl. Tracing + Span Streaming)47.55 kB--
@sentry/browser (incl. Tracing, Profiling)50.3 kB--
@sentry/browser (incl. Tracing, Replay)84.92 kB--
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags74.43 kB--
@sentry/browser (incl. Tracing, Replay with Canvas)89.64 kB--
@sentry/browser (incl. Tracing, Replay, Feedback)102.24 kB--
@sentry/browser (incl. Feedback)44.51 kB--
@sentry/browser (incl. sendFeedback)32.13 kB--
@sentry/browser (incl. FeedbackAsync)37.25 kB--
@sentry/browser (incl. Metrics)28.41 kB--
@sentry/browser (incl. Logs)28.64 kB--
@sentry/browser (incl. Metrics & Logs)29.33 kB--
@sentry/react29.05 kB--
@sentry/react (incl. Tracing)47.55 kB--
@sentry/vue32.24 kB--
@sentry/vue (incl. Tracing)47.17 kB--
@sentry/svelte27.35 kB--
CDN Bundle29.73 kB--
CDN Bundle (incl. Tracing)47.84 kB--
CDN Bundle (incl. Logs, Metrics)31.21 kB--
CDN Bundle (incl. Tracing, Logs, Metrics)49.09 kB--
CDN Bundle (incl. Replay, Logs, Metrics)70.53 kB--
CDN Bundle (incl. Tracing, Replay)85.34 kB--
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)86.5 kB--
CDN Bundle (incl. Tracing, Replay, Feedback)91.2 kB--
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)92.37 kB--
CDN Bundle - uncompressed87.8 kB--
CDN Bundle (incl. Tracing) - uncompressed144.29 kB--
CDN Bundle (incl. Logs, Metrics) - uncompressed92.29 kB--
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed148.05 kB--
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed217.02 kB--
CDN Bundle (incl. Tracing, Replay) - uncompressed263.07 kB--
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed266.81 kB--
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed276.77 kB--
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed280.5 kB--
@sentry/nextjs (client)50.03 kB--
@sentry/sveltekit (client)45.8 kB--
@sentry/core/server76.48 kB--
@sentry/core/browser63.23 kB--
@sentry/node-core62.59 kB--
@sentry/node131.1 kB--
@sentry/node - without tracing75.02 kB--
@sentry/aws-serverless87.24 kB+0.01%+1 B 🔺
@sentry/cloudflare (withSentry) - minified173.52 kB+0.23%+390 B 🔺
@sentry/cloudflare (withSentry)433.52 kB+0.26%+1.09 kB 🔺

View base workflow run

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

Looks great. I also added a memory test to ensure we not introduce any other memory leaks in the future by accident

@logaretm
logaretm merged commit 1e0341d into developMay 26, 2026
73 of 74 checks passed
@logaretm
logaretm deleted the awad/js-2605-makeflushlock-grows-a-waituntil-wrapper-chain-on-every-init branch May 26, 2026 08:34
JPeer264 added a commit that referenced this pull request May 28, 2026
)
follow up to: #21156
This prevents a deadlock in the `waitUntil`, which happened in the
sentry-mcp:
<img width="972" height="119" alt="Screenshot 2026-05-27 at 17 04 19"
src="https://github.com/user-attachments/assets/18ec0f64-c766-4f6f-82a6-9453280668a5"
/>
Sign up for freeto 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.

makeFlushLock grows a waitUntil wrapper chain on every init(), causing stack overflow and memory exhaustion in Durable Objects

2 participants

@logaretm@JPeer264