Skip to content

fix(core): [Scope Parent Retention 2] Release intermediate parent scopes - #6637

Draft
adinauer wants to merge 4 commits into
fix/scope-parent-retention-v9-init-identityfrom
fix/scope-parent-retention-v9-weak-parents
Draft

adinauer wants to merge 4 commits into
fix/scope-parent-retention-v9-init-identityfrom
fix/scope-parent-retention-v9-weak-parents

Conversation

@adinauer

@adinauer adinauer commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

PR Stack (Scope Parent Retention)


📜 Description

Hold each forked Scopes instance's immediate parent through a WeakReference. If that parent has been collected, getParentScopes() returns null and ancestry traversal ends. Forks retain only the opaque SDK initialization token introduced in #6636, not a root Scopes instance.

This applies the behavior to all regular scope forks, so asynchronous wrappers and scheduler integrations do not need special task-specific fork APIs. It also makes ancestry and diagnostic chain traversal iterative.

💡 Motivation and Context

Escaping or self-rescheduling tasks can retain their captured scopes indefinitely. Strong parent links then keep every historical ancestor alive, producing an unbounded retention chain. Weak immediate parents allow those scopes—including the initialization root—to be collected, while the shared token preserves SDK initialization identity independently of ancestry.

Resolves #6248

💚 How did you test it?

  • make format api
  • Focused tests across core, all three Spring variants, and OpenTelemetry:
    • ScopesTest
    • SentryTest
    • SentryWrapperTest
    • Spring task decorator tests
    • Spring WebFlux schedule hook tests
    • SentryContextWrapperTest

The regressions verify that immediate parents and the initialization root are collectable, collected ancestry terminates at null, descendants remain usable, and SDK initialization identity continues to work through the shared token.

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.
  • Public API changes reviewed by another Mobile SDK team member or implemented according to the develop docs spec.

🔮 Next steps

Merge this PR into #6636 with a merge commit, then merge #6636 into the collection PR the same way.

⚠️ Merge this PR using a merge commit (not squash). Only the collection branch is squash-merged into main.

adinauer and others added 2 commits November 17, 2026 11:20
Hold immediate scope parents weakly while retaining the SDK initialization root. This prevents escaping asynchronous tasks from retaining an unbounded chain of historical Scopes instances.

Fixes #6248

Co-Authored-By: Claude <noreply@anthropic.com>
@sentry

sentry Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.63.0 (1) release

⚙️ sentry-android Build Distribution Settings

…cope-parent-retention-v9-weak-parents

# Conflicts:
#	sentry/src/main/java/io/sentry/Scopes.java

This branch has not been deployed

No deployments
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.

1 participant