Repository navigation
Conversation
isAncestorOf recursed once per parent link, so a long Scopes chain (e.g. from a task that keeps re-scheduling itself through SentryScheduleHook) could throw StackOverflowError. Since getsentry#6004 this is also called from Sentry.getCurrentScopes() in globalHubMode. Walk the chain with a loop instead. Results are unchanged.
hocaron
requested review from
0xadam-brown,
adinauer,
markushi,
romtsn and
runningcode
as code owners
October 12, 2026 16:14
This branch has not been deployed
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.
📜 Description
Scopes.isAncestorOfrecursed once per parent link. It now walks theparentScopeschain with a loop, so a long chain can't throwStackOverflowError. Behaviour is otherwise unchanged.💡 Motivation and Context
Split out from #6248, as mentioned there. This PR does not fix the leak itself, only the stack overflow a long chain can cause.
Long
Scopeschains happen in practice: a task that re-schedules itself throughSentryScheduleHookgets a new fork whose parent is the previous run's fork (see #6248, similar to #5051). We saw chains of ~38k links after ~13 days in production.Since #6004,
Sentry.getCurrentScopes()callsroot.isAncestorOf(scopes)in globalHubMode, so the recursion can run on a hot path. The OTelSentryContextWrapperalso callsisAncestorOf.💚 How did you test it?
Added
ScopesTest›isAncestorOf does not overflow the stack on a deep chain: builds 20,000 chainedforkedCurrentScopeforks and callsroot.isAncestorOf(deepest)on a thread with a 256 KB stack.main: fails withexpected:<true> but was:<java.lang.StackOverflowError>./gradlew :sentry:test :sentry:apiCheck :sentry:spotlessCheckpasses locally (JDK 17).📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps
The fix that stops the chain from growing in the first place is proposed in #6248.