From 4740d5f4ae368936fe8f905c2db3d94b62caa63e Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Thu, 19 Nov 2026 17:56:23 +0100 Subject: [PATCH 1/2] fix(feedback): Stop shake detection from leaking the first resumed Activity SentryShakeDetector caches its SensorManager the first time it initializes. When an Activity resumed before the background warm-up ran, that manager came from the Activity, and an Activity's SensorManager keeps a reference to it. The long-lived FeedbackShakeIntegration then held the destroyed Activity for the rest of the process. Always resolve the SensorManager from the application context. Fixes JAVA-847 Co-Authored-By: Claude Opus 5.5 --- .../android/core/SentryShakeDetector.java | 5 ++++- .../android/core/SentryShakeDetectorTest.kt | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/SentryShakeDetector.java b/sentry-android-core/src/main/java/io/sentry/android/core/SentryShakeDetector.java index 72c100923cd..2861e4057f7 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/SentryShakeDetector.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/SentryShakeDetector.java @@ -76,7 +76,10 @@ private synchronized void init(final @NotNull Context context) { return; } if (sensorManager == null) { - sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); + // An Activity's SensorManager holds on to that Activity, and the cached manager outlives it. + sensorManager = + (SensorManager) + ContextUtils.getApplicationContext(context).getSystemService(Context.SENSOR_SERVICE); } if (sensorManager != null && accelerometer == null) { accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER, false); diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/SentryShakeDetectorTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/SentryShakeDetectorTest.kt index 24ccfceaa86..267570201f4 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/SentryShakeDetectorTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/SentryShakeDetectorTest.kt @@ -56,6 +56,24 @@ class SentryShakeDetectorTest { ) } + @Test + fun `resolves SensorManager from the application context`() { + val activityContext = mock() + whenever(activityContext.applicationContext).thenReturn(fixture.context) + + val sut = fixture.getSut() + sut.start(activityContext, fixture.listener) + + verify(activityContext, never()).getSystemService(any()) + verify(fixture.sensorManager) + .registerListener( + eq(sut), + eq(fixture.accelerometer), + eq(SensorManager.SENSOR_DELAY_NORMAL), + isA(), + ) + } + @Test fun `unregisters sensor listener on stop`() { val sut = fixture.getSut() From aea3d1bb238d09a0d994351e4f487d396eb1af07 Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Thu, 19 Nov 2026 17:56:49 +0100 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a01e88ce7cf..9472ec4027c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ### Fixes - Kotlin now sees `Attachment.getBytes()`, `Device.getArchs()`, `Spring.getActiveProfiles()` and `JsonSerializationUtils.bytesFrom(...)` as nullable, because `@Nullable` now annotates the returned array instead of its elements ([#6558](https://github.com/getsentry/sentry-java/pull/6558)) +- Shake-to-report no longer leaks the first Activity that resumes after `Sentry.init` ([#6686](https://github.com/getsentry/sentry-java/pull/6686)) ## 8.65.0