Uh oh!
There was an error while loading. Please reload this page.
fix(core): Use WeakRef for Span-Scope circular references - #21242
Conversation
Uh oh!
There was an error while loading. Please reload this page.
size-limit report 📦
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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>
c41e7c6 to
e0af87fCompareUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ 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.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
089ec80 to
5df54e7CompareUh oh!
There was an error while loading. Please reload this page.

closes#18339
closes JS-1224
This PR adresses two problems:
Problem 1
The
_sentryScopehas 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
After
The memory clearly doesn't spike that much anymore
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.