Skip to content

fix(core): Use WeakRef for Span-Scope circular references#21242

Open
JPeer264 wants to merge 4 commits into
developfrom
jp/memory-leak-traces-sample-rate
Open

fix(core): Use WeakRef for Span-Scope circular references#21242
JPeer264 wants to merge 4 commits into
developfrom
jp/memory-leak-traces-sample-rate

Conversation

@JPeer264
Copy link
Copy Markdown
Member

closes #18339
closes JS-1224

This PR adresses two problems:

Problem 1

The _sentryScope has a reference on the span and the span has a reference to the scope, which is a circular dependency and it couldn't be garbage collected.

Problem 2

When a span has been sent, they are kept for 5 minutes as cache just in case, which could eat up memory quite fast, but is actively removed after 5 minutes. By changing this to an LRUMap, we can still keep them, but limit them to 10k spans.

A reproduction and load tests can be found here: https://github.com/JPeer264/sentry-repros/tree/issue-18339

Memory snapshots

Before

Snapshot Time Heap Size Node Count
Baseline 0:00 23.6 MB 207,767
During 1 1:47 44.9 MB (+90%) 585,619 (+182%)
During 2 3:48 71.1 MB (+201%) 1,192,498 (+474%)
During 3 5:50 100.9 MB (+327%) 1,794,671 (+764%)
During 4 7:50 105.6 MB (+347%) 2,019,638 (+872%)
During 5 9:51 133.7 MB (+466%) 2,020,743 (+873%)
During 6 11:51 134.0 MB (+467%) 2,023,947 (+874%)
Final 13:15 120.1 MB (+409%) 1,734,738 (+735%)

After

The memory clearly doesn't spike that much anymore

Snapshot Time Heap Size Node Count
Baseline 0:00 19.2 MB 186,001
During 1 3:24 25.4 MB (+32%) 236,693 (+27%)
During 2 6:25 25.7 MB (+34%) 240,009 (+29%)
During 3 9:25 26.0 MB (+35%) 243,060 (+31%)
During 4 12:25 25.8 MB (+34%) 237,430 (+28%)
Final 12:51 24.7 MB (+29%) 220,281 (+18%)

AI generated text:

Break the circular reference between Span and Scope by using WeakRef
in both directions, allowing proper garbage collection of completed
spans and their associated scopes.

Replace unbounded Map with LRUMap for _sentSpans in the span exporter to
prevent memory growth from accumulated span IDs.

@JPeer264 JPeer264 requested a review from a team May 29, 2026 09:41
@JPeer264 JPeer264 self-assigned this May 29, 2026
@JPeer264 JPeer264 requested a review from a team as a code owner May 29, 2026 09:41
@JPeer264 JPeer264 requested review from andreiborza, logaretm and mydea and removed request for a team May 29, 2026 09:41
@linear-code
Copy link
Copy Markdown

linear-code Bot commented May 29, 2026

JS-1224

Comment thread packages/core/src/tracing/utils.ts Outdated
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 29, 2026

size-limit report 📦

Path Size % Change Change
@sentry/browser 27.2 kB +0.14% +37 B 🔺
@sentry/browser - with treeshaking flags 25.65 kB +0.13% +33 B 🔺
@sentry/browser (incl. Tracing) 45.25 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 47.48 kB +0.01% +4 B 🔺
@sentry/browser (incl. Tracing, Profiling) 50.24 kB +0.04% +18 B 🔺
@sentry/browser (incl. Tracing, Replay) 84.84 kB +0.02% +12 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 74.4 kB +0.01% +7 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 89.54 kB +0.02% +13 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 102.16 kB +0.01% +9 B 🔺
@sentry/browser (incl. Feedback) 44.36 kB +0.06% +24 B 🔺
@sentry/browser (incl. sendFeedback) 32.02 kB +0.12% +36 B 🔺
@sentry/browser (incl. FeedbackAsync) 37.11 kB +0.09% +32 B 🔺
@sentry/browser (incl. Metrics) 28.27 kB +0.1% +27 B 🔺
@sentry/browser (incl. Logs) 28.52 kB +0.11% +31 B 🔺
@sentry/browser (incl. Metrics & Logs) 29.22 kB +0.11% +32 B 🔺
@sentry/react 29.03 kB +0.13% +36 B 🔺
@sentry/react (incl. Tracing) 47.52 kB +0.03% +14 B 🔺
@sentry/vue 32.22 kB +0.05% +13 B 🔺
@sentry/vue (incl. Tracing) 47.16 kB +0.01% +1 B 🔺
@sentry/svelte 27.23 kB +0.14% +37 B 🔺
CDN Bundle 29.58 kB +0.09% +26 B 🔺
CDN Bundle (incl. Tracing) 47.81 kB +0.01% +3 B 🔺
CDN Bundle (incl. Logs, Metrics) 31.08 kB +0.11% +34 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) 49.05 kB +0.03% +13 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) 70.34 kB +0.05% +29 B 🔺
CDN Bundle (incl. Tracing, Replay) 85.18 kB +0.01% +1 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 86.35 kB +0.01% +4 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 91.05 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.2 kB -0.01% -7 B 🔽
CDN Bundle - uncompressed 87.69 kB +0.11% +96 B 🔺
CDN Bundle (incl. Tracing) - uncompressed 144.1 kB +0.01% +8 B 🔺
CDN Bundle (incl. Logs, Metrics) - uncompressed 92.18 kB +0.11% +96 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 147.86 kB +0.01% +8 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 216.9 kB +0.05% +96 B 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 262.88 kB +0.01% +8 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 266.62 kB +0.01% +8 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 276.57 kB +0.01% +8 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 280.31 kB +0.01% +8 B 🔺
@sentry/nextjs (client) 50.05 kB +0.05% +21 B 🔺
@sentry/sveltekit (client) 45.7 kB +0.06% +25 B 🔺
@sentry/core/server 75.94 kB -0.82% -621 B 🔽
@sentry/core/browser 63.09 kB -0.01% -1 B 🔽
@sentry/node-core 61.7 kB +0.02% +10 B 🔺
@sentry/node 130.35 kB -0.13% -159 B 🔽
@sentry/node - without tracing 74.06 kB -0.05% -31 B 🔽
@sentry/aws-serverless 86.26 kB -0.04% -27 B 🔽
@sentry/cloudflare (withSentry) - minified 171.5 kB +0.01% +8 B 🔺
@sentry/cloudflare (withSentry) 429.32 kB +0.01% +27 B 🔺

View base workflow run

Comment thread packages/core/src/utils/spanUtils.ts Outdated
Comment thread packages/core/src/tracing/utils.ts Outdated
Break the circular reference between Span and Scope by using WeakRef
in both directions, allowing proper garbage collection of completed
spans and their associated scopes.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@JPeer264 JPeer264 force-pushed the jp/memory-leak-traces-sample-rate branch from c41e7c6 to e0af87f Compare May 29, 2026 09:52
Comment thread packages/core/src/utils/spanUtils.ts
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 6619ea6. Configure here.

Comment thread packages/core/src/utils/spanUtils.ts Outdated
Comment thread packages/core/src/utils/spanUtils.ts Outdated
@JPeer264 JPeer264 force-pushed the jp/memory-leak-traces-sample-rate branch from 089ec80 to 5df54e7 Compare May 29, 2026 16:46
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.

Memory leak with high tracesSampleRate

3 participants