Skip to content

fix(core): Clear contexts on Scope.clear() - #5902

Merged
runningcode merged 2 commits into
mainfrom
no/java-683-scope-clear-contexts
Aug 6, 2026
Merged

fix(core): Clear contexts on Scope.clear()#5902
runningcode merged 2 commits into
mainfrom
no/java-683-scope-clear-contexts

Conversation

@runningcode

Copy link
Copy Markdown
Contributor

📜 Description

Scope.clear() reset every field on the scope except contexts, so anything set via setContexts(...) survived a clear(). The root cause is that Contexts had no clear() method (unlike the tags, extra, and fingerprint collections that clear() already resets), so contexts were simply left untouched.

This PR:

  • Adds Contexts.clear().
  • Overrides clear() in CombinedContextsView to delegate to the default context, consistent with how put/remove already delegate (otherwise clear() on the view would no-op against its always-empty inherited storage).
  • Clears contexts in Scope.clear() and notifies scope observers with the now-empty contexts, so persisted scopes (e.g. Android's scope-on-disk used for native crashes) stay in sync — matching what setContexts/clearBreadcrumbs already do.

💡 Motivation and Context

Reported while adding the scopes API to the Godot Engine (hybrid) SDK: clear() cleared everything except contexts, forcing a manual workaround that removes each context key by hand.

Fixes GH-5894
Fixes JAVA-683

💚 How did you test it?

Added unit tests:

  • ContextsTestclear() empties the contexts.
  • CombinedContextsViewTestclear() clears only the default context.
  • ScopeTest — extended the reset test to cover contexts, plus a new test verifying clear() notifies scope observers with empty contexts.

📝 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.

🔮 Next steps

Hybrid SDKs (e.g. Godot) can drop their manual context-clearing workaround once this ships.

Scope.clear() reset every field except contexts, so anything set via
setContexts survived a clear. Contexts had no clear() method, unlike the
tags, extra, and fingerprint collections, which is why it was skipped.

Add Contexts.clear(), override it in CombinedContextsView to delegate to
the default context, and clear contexts in Scope.clear() while notifying
scope observers so persisted scopes stay in sync.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@linear-code

linear-code Bot commented Aug 6, 2026

Copy link
Copy Markdown

JAVA-683

@sentry

sentry Bot commented Aug 6, 2026

Copy link
Copy Markdown

📲 Install Builds

Android

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

⚙️ sentry-android Build Distribution Settings

@runningcode
runningcode marked this pull request as ready for review August 6, 2026 13:44
@runningcode runningcode added the sanity-check PR needs a lightweight review for obvious issues label Aug 6, 2026
@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 318.40 ms 365.31 ms 46.91 ms
Size 0 B 0 B 0 B

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
d15471f 303.49 ms 439.08 ms 135.59 ms
b750b96 408.98 ms 480.32 ms 71.34 ms
17a0955 372.53 ms 446.70 ms 74.17 ms
d217708 355.34 ms 381.39 ms 26.05 ms
5b1a06b 310.56 ms 362.79 ms 52.22 ms
fcec2f2 311.35 ms 384.94 ms 73.59 ms
62b579c 349.26 ms 426.26 ms 77.00 ms
5f14e5d 325.76 ms 368.32 ms 42.56 ms
48277cd 320.38 ms 379.90 ms 59.52 ms
a416a65 295.53 ms 373.74 ms 78.21 ms

App size

Revision Plain With Sentry Diff
d15471f 1.58 MiB 2.13 MiB 559.54 KiB
b750b96 1.58 MiB 2.10 MiB 533.19 KiB
17a0955 1.58 MiB 2.10 MiB 533.20 KiB
d217708 1.58 MiB 2.10 MiB 532.97 KiB
5b1a06b 0 B 0 B 0 B
fcec2f2 1.58 MiB 2.12 MiB 551.51 KiB
62b579c 0 B 0 B 0 B
5f14e5d 1.58 MiB 2.19 MiB 620.00 KiB
48277cd 0 B 0 B 0 B
a416a65 1.58 MiB 2.12 MiB 555.26 KiB

@markushi markushi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@runningcode
runningcode enabled auto-merge (squash) August 6, 2026 14:03
@runningcode
runningcode merged commit 8a8d947 into main Aug 6, 2026
73 checks passed
@runningcode
runningcode deleted the no/java-683-scope-clear-contexts branch August 6, 2026 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sanity-check PR needs a lightweight review for obvious issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

scope.clear() does not remove contexts

2 participants