Skip to content

Commit 3406bbb

Browse files
runningcodeclaude
andauthored
fix(feedback): Stop shake detection from leaking the first resumed Activity (#6686)
* 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 <noreply@anthropic.com> * changelog --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e76c4e5 commit 3406bbb

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
### Fixes
1111

1212
- 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))
13+
- Shake-to-report no longer leaks the first Activity that resumes after `Sentry.init` ([#6686](https://github.com/getsentry/sentry-java/pull/6686))
1314

1415
## 8.65.0
1516

‎sentry-android-core/src/main/java/io/sentry/android/core/SentryShakeDetector.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ private synchronized void init(final @NotNull Context context) {
7676
return;
7777
}
7878
if (sensorManager == null) {
79-
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
79+
// An Activity's SensorManager holds on to that Activity, and the cached manager outlives it.
80+
sensorManager =
81+
(SensorManager)
82+
ContextUtils.getApplicationContext(context).getSystemService(Context.SENSOR_SERVICE);
8083
}
8184
if (sensorManager != null && accelerometer == null) {
8285
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER, false);

‎sentry-android-core/src/test/java/io/sentry/android/core/SentryShakeDetectorTest.kt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ class SentryShakeDetectorTest {
5656
)
5757
}
5858

59+
@Test
60+
fun `resolves SensorManager from the application context`() {
61+
val activityContext = mock<Context>()
62+
whenever(activityContext.applicationContext).thenReturn(fixture.context)
63+
64+
val sut = fixture.getSut()
65+
sut.start(activityContext, fixture.listener)
66+
67+
verify(activityContext, never()).getSystemService(any<String>())
68+
verify(fixture.sensorManager)
69+
.registerListener(
70+
eq(sut),
71+
eq(fixture.accelerometer),
72+
eq(SensorManager.SENSOR_DELAY_NORMAL),
73+
isA<Handler>(),
74+
)
75+
}
76+
5977
@Test
6078
fun `unregisters sensor listener on stop`() {
6179
val sut = fixture.getSut()

0 commit comments

Comments
 (0)