Skip to content

Commit 1e1ab7f

Browse files
authored
Clear window reference only on activity stop in profileMeasurements collector (#2407)
* the current window reference in SentryFrameMetricsCollector is cleared only after the activity stops, and no more when the profiler stops
1 parent f122116 commit 1e1ab7f

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Fixes
66

7+
- Clear window reference only on activity stop in profileMeasurements collector ([#2407](https://github.com/getsentry/sentry-java/pull/2407))
78
- No longer disable OpenTelemetry exporters in default Java Agent config ([#2408](https://github.com/getsentry/sentry-java/pull/2408))
89
- Fix `ClassNotFoundException` for `io.sentry.spring.SentrySpringServletContainerInitializer` in `sentry-spring-jakarta` ([#2411](https://github.com/getsentry/sentry-java/issues/2411))
910
- Fix `sentry-samples-spring-jakarta` ([#2411](https://github.com/getsentry/sentry-java/issues/2411))

sentry-android-core/src/main/java/io/sentry/android/core/internal/util/SentryFrameMetricsCollector.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ public void onActivityPaused(@NotNull Activity activity) {}
113113

114114
@Override
115115
public void onActivityStopped(@NotNull Activity activity) {
116-
clearCurrentWindow(activity.getWindow());
116+
stopTrackingWindow(activity.getWindow());
117+
if (currentWindow != null && currentWindow.get() == activity.getWindow()) {
118+
currentWindow = null;
119+
}
117120
}
118121

119122
@Override
@@ -141,15 +144,12 @@ public void stopCollection(final @Nullable String listenerId) {
141144
}
142145
Window window = currentWindow != null ? currentWindow.get() : null;
143146
if (window != null && listenerMap.isEmpty()) {
144-
clearCurrentWindow(window);
147+
stopTrackingWindow(window);
145148
}
146149
}
147150

148151
@SuppressLint("NewApi")
149-
private void clearCurrentWindow(final @NotNull Window window) {
150-
if (currentWindow != null && currentWindow.get() == window) {
151-
currentWindow = null;
152-
}
152+
private void stopTrackingWindow(final @NotNull Window window) {
153153
if (trackedWindows.contains(window)) {
154154
if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.N) {
155155
try {

sentry-android-core/src/test/java/io/sentry/android/core/internal/util/SentryFrameMetricsCollectorTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import io.sentry.ILogger
1111
import io.sentry.SentryOptions
1212
import io.sentry.android.core.BuildInfoProvider
1313
import io.sentry.test.getCtor
14+
import io.sentry.test.getProperty
1415
import org.junit.runner.RunWith
1516
import org.mockito.Mockito.spy
1617
import org.mockito.kotlin.mock
1718
import org.mockito.kotlin.whenever
19+
import java.lang.ref.WeakReference
1820
import kotlin.test.BeforeTest
1921
import kotlin.test.Test
2022
import kotlin.test.assertEquals
@@ -229,4 +231,24 @@ class SentryFrameMetricsCollectorTest {
229231
collector.stopCollection(id2)
230232
assertEquals(1, fixture.removeOnFrameMetricsAvailableListenerCounter)
231233
}
234+
235+
@Test
236+
fun `collector removes current window only when last activity stops`() {
237+
val collector = fixture.getSut(context)
238+
val id1 = collector.startCollection(mock())
239+
collector.onActivityStarted(fixture.activity)
240+
collector.onActivityStarted(fixture.activity2)
241+
242+
// Stopping collecting data doesn't clear current tracked window reference
243+
collector.stopCollection(id1)
244+
assertNotNull(collector.getProperty<WeakReference<Window>?>("currentWindow"))
245+
246+
// Stopping first activity doesn't clear current tracked window reference
247+
collector.onActivityStopped(fixture.activity)
248+
assertNotNull(collector.getProperty<WeakReference<Window>?>("currentWindow"))
249+
250+
// Stopping last activity clears current tracked window reference
251+
collector.onActivityStopped(fixture.activity2)
252+
assertNull(collector.getProperty<WeakReference<Window>?>("currentWindow"))
253+
}
232254
}

0 commit comments

Comments
 (0)