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
Copy link
Copy Markdown
Member

@logaretm logaretm commented May 25, 2026

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

linear-code Bot commented May 25, 2026

JS-2605

@logaretm logaretm changed the title test(cloudflare): reproduce flush lock wrapper growth fix(cloudflare): avoid repeated flush lock wrapping May 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 owner May 25, 2026 11:01
@logaretm logaretm requested review from JPeer264 and andreiborza and removed request for a team May 25, 2026 11:01
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 25, 2026

size-limit report 📦

Path Size % Change Change
@sentry/browser 27.32 kB - -
@sentry/browser - with treeshaking flags 25.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 flags 74.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/react 29.05 kB - -
@sentry/react (incl. Tracing) 47.55 kB - -
@sentry/vue 32.24 kB - -
@sentry/vue (incl. Tracing) 47.17 kB - -
@sentry/svelte 27.35 kB - -
CDN Bundle 29.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 - uncompressed 87.8 kB - -
CDN Bundle (incl. Tracing) - uncompressed 144.29 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 92.29 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 148.05 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 217.02 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 263.07 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 266.81 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 276.77 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 280.5 kB - -
@sentry/nextjs (client) 50.03 kB - -
@sentry/sveltekit (client) 45.8 kB - -
@sentry/core/server 76.48 kB - -
@sentry/core/browser 63.23 kB - -
@sentry/node-core 62.59 kB - -
@sentry/node 131.1 kB - -
@sentry/node - without tracing 75.02 kB - -
@sentry/aws-serverless 87.24 kB +0.01% +1 B 🔺
@sentry/cloudflare (withSentry) - minified 173.52 kB +0.23% +390 B 🔺
@sentry/cloudflare (withSentry) 433.52 kB +0.26% +1.09 kB 🔺

View base workflow run

Copy link
Copy Markdown
Member

@JPeer264 JPeer264 left a comment

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 develop May 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 free to 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