From 373e6f07a61f510c6c6e4f37e35803ce543498a1 Mon Sep 17 00:00:00 2001 From: Roman Zavarnitsyn Date: Tue, 2 May 2023 16:48:25 +0200 Subject: [PATCH 1/3] Fix Hub.close using withScope and not configureScope --- CHANGELOG.md | 4 ++++ sentry/src/main/java/io/sentry/Hub.java | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 890327378c..1f18d85b35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Fixes + +- Use `configureScope` instead of `withScope` in `Hub.close()`. This ensures that the main scope releases the in-memory data when closing a hub instance. + ### Dependencies - Bump Gradle from v8.1.0 to v8.1.1 ([#2666](https://github.com/getsentry/sentry-java/pull/2666)) diff --git a/sentry/src/main/java/io/sentry/Hub.java b/sentry/src/main/java/io/sentry/Hub.java index dcc80fa287..38a2250b9d 100644 --- a/sentry/src/main/java/io/sentry/Hub.java +++ b/sentry/src/main/java/io/sentry/Hub.java @@ -340,7 +340,7 @@ public void close() { } } - withScope(scope -> scope.clear()); + configureScope(scope -> scope.clear()); options.getTransactionProfiler().close(); options.getTransactionPerformanceCollector().close(); options.getExecutorService().close(options.getShutdownTimeoutMillis()); From 4511a265be54317b6f0118fe4f20a52dfd16904b Mon Sep 17 00:00:00 2001 From: Roman Zavarnitsyn Date: Tue, 2 May 2023 17:43:00 +0200 Subject: [PATCH 2/3] Add test --- sentry/src/test/java/io/sentry/HubTest.kt | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/sentry/src/test/java/io/sentry/HubTest.kt b/sentry/src/test/java/io/sentry/HubTest.kt index 62ad641372..88cd21c202 100644 --- a/sentry/src/test/java/io/sentry/HubTest.kt +++ b/sentry/src/test/java/io/sentry/HubTest.kt @@ -1548,6 +1548,26 @@ class HubTest { verify(performanceCollector).close() } + @Test + fun `Hub close should clear the scope`() { + val options = SentryOptions().apply { + dsn = "https://key@sentry.io/proj" + } + + val sut = Hub(options) + sut.addBreadcrumb("Test") + sut.startTransaction("test", "test.op", true) + sut.close() + + // we have to clone the scope, so its isEnabled returns true, but it's still built up from + // the old scope preserving its data + val clone = sut.clone() + var oldScope: Scope? = null + clone.configureScope { scope -> oldScope = scope } + assertNull(oldScope!!.transaction) + assertTrue(oldScope!!.breadcrumbs.isEmpty()) + } + @Test fun `when tracesSampleRate and tracesSampler are not set on SentryOptions, startTransaction returns NoOp`() { val hub = generateHub { From 24cbe325373d77a577e9df766c8cfc6c06ccae6f Mon Sep 17 00:00:00 2001 From: Roman Zavarnitsyn Date: Tue, 2 May 2023 17:47:24 +0200 Subject: [PATCH 3/3] Pr id --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f18d85b35..f38599a59e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Fixes -- Use `configureScope` instead of `withScope` in `Hub.close()`. This ensures that the main scope releases the in-memory data when closing a hub instance. +- Use `configureScope` instead of `withScope` in `Hub.close()`. This ensures that the main scope releases the in-memory data when closing a hub instance. ([#2688](https://github.com/getsentry/sentry-java/pull/2688)) ### Dependencies