Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixes

- Clear window reference only on activity stop in profileMeasurements collector ([#2407](https://github.com/getsentry/sentry-java/pull/2407))
- No longer disable OpenTelemetry exporters in default Java Agent config ([#2408](https://github.com/getsentry/sentry-java/pull/2408))
- Fix `ClassNotFoundException` for `io.sentry.spring.SentrySpringServletContainerInitializer` in `sentry-spring-jakarta` ([#2411](https://github.com/getsentry/sentry-java/issues/2411))
- Fix `sentry-samples-spring-jakarta` ([#2411](https://github.com/getsentry/sentry-java/issues/2411))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ public void onActivityPaused(@NotNull Activity activity) {}

@Override
public void onActivityStopped(@NotNull Activity activity) {
clearCurrentWindow(activity.getWindow());
stopTrackingWindow(activity.getWindow());
if (currentWindow != null && currentWindow.get() == activity.getWindow()) {
currentWindow = null;
}
}

@Override
Expand Down Expand Up @@ -141,15 +144,12 @@ public void stopCollection(final @Nullable String listenerId) {
}
Window window = currentWindow != null ? currentWindow.get() : null;
if (window != null && listenerMap.isEmpty()) {
clearCurrentWindow(window);
stopTrackingWindow(window);
}
}

@SuppressLint("NewApi")
private void clearCurrentWindow(final @NotNull Window window) {
if (currentWindow != null && currentWindow.get() == window) {
currentWindow = null;
}
private void stopTrackingWindow(final @NotNull Window window) {
if (trackedWindows.contains(window)) {
if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.N) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import io.sentry.ILogger
import io.sentry.SentryOptions
import io.sentry.android.core.BuildInfoProvider
import io.sentry.test.getCtor
import io.sentry.test.getProperty
import org.junit.runner.RunWith
import org.mockito.Mockito.spy
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import java.lang.ref.WeakReference
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down Expand Up @@ -229,4 +231,24 @@ class SentryFrameMetricsCollectorTest {
collector.stopCollection(id2)
assertEquals(1, fixture.removeOnFrameMetricsAvailableListenerCounter)
}

@Test
fun `collector removes current window only when last activity stops`() {
val collector = fixture.getSut(context)
val id1 = collector.startCollection(mock())
collector.onActivityStarted(fixture.activity)
collector.onActivityStarted(fixture.activity2)

// Stopping collecting data doesn't clear current tracked window reference
collector.stopCollection(id1)
assertNotNull(collector.getProperty<WeakReference<Window>?>("currentWindow"))

// Stopping first activity doesn't clear current tracked window reference
collector.onActivityStopped(fixture.activity)
assertNotNull(collector.getProperty<WeakReference<Window>?>("currentWindow"))

// Stopping last activity clears current tracked window reference
collector.onActivityStopped(fixture.activity2)
assertNull(collector.getProperty<WeakReference<Window>?>("currentWindow"))
}
}