fix(core): Use WeakRef for Span-Scope circular references#21242
Open
JPeer264 wants to merge 4 commits into
Open
fix(core): Use WeakRef for Span-Scope circular references#21242JPeer264 wants to merge 4 commits into
JPeer264 wants to merge 4 commits into
Conversation
Contributor
size-limit report 📦
|
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
e0af87f
Compare
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.
logaretm
approved these changes
May 29, 2026
089ec80 to
5df54e7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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.