Skip to content

fix(core): [Scope Parent Retention 1] Track SDK initialization identity - #6636

Draft
adinauer wants to merge 3 commits into
fix/scope-parent-retention-v9from
fix/scope-parent-retention-v9-init-identity
Draft

adinauer wants to merge 3 commits into
fix/scope-parent-retention-v9from
fix/scope-parent-retention-v9-init-identity

Conversation

@adinauer

@adinauer adinauer commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

PR Stack (Scope Parent Retention)


📜 Description

Assign an opaque token to each SDK initialization and expose an explicit initialization identity check. Root scopes create the token, forks inherit it, and Sentry.getCurrentScopes() validates thread-local scopes by token identity instead of walking their parent chain.

This PR intentionally keeps parent references strong and preserves the existing scope ancestry behavior. Weak parent retention is introduced separately in the next stack PR.

Benchmark

A focused JMH benchmark compared the previous ancestry-based check with initialization-token identity across different parent-chain depths:

Parent depth Ancestry check Initialization-token check Result
0 0.387 ns/op 0.527 ns/op Token check +0.14 ns
1 0.792 ns/op 0.571 ns/op 1.4x faster
10 4.811 ns/op 0.628 ns/op 7.7x faster
100 153.094 ns/op 0.558 ns/op 274x faster
1,000 2,642.499 ns/op 0.546 ns/op 4,840x faster

Both checks effectively allocate 0 B/op. The previous check is O(depth), while initialization-token identity remains O(1). The benchmark mirrors the relevant method logic on desktop JDK 21 and does not measure scope-fork construction or Android runtime behavior.

💡 Motivation and Context

SDK initialization identity must remain valid independently of the parent chain without retaining a root Scopes instance. Separating the check from ancestry prepares scopes for collectible parents while also avoiding recursive traversal for stale-scope validation.

Refs #6248

💚 How did you test it?

  • make format api
  • ./gradlew :sentry:test --tests='io.sentry.ScopesTest' --tests='io.sentry.SentryTest'
  • Focused JMH run with the GC profiler on JDK 21

📝 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

The next PR changes immediate parent references to weak references and adds retention regressions across core, Spring, and OpenTelemetry.

#skip-changelog

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

Validate thread-local scopes using their SDK initialization root instead of walking the parent chain. This keeps stale-scope detection independent from ancestry and makes the check constant time.

Refs #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

adinauer and others added 2 commits November 17, 2026 12:38
Keep SDK initialization identity independent from any Scopes instance. Descendants now retain only a small shared token rather than the initialization root.

Refs #6248

Co-Authored-By: Claude <noreply@anthropic.com>
Use reference equality for the opaque token shared by scopes created from the same SDK initialization.

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