Skip to content

Commit 8884e89

Browse files
fix(profiling): Don't crash when ProfilingManager can't be loaded (#6742)
* fix(profiling): Don't crash when ProfilingManager can't be loaded Co-Authored-By: Roman Zavarnitsyn <roman.zavarnitsyn@sentry.io> * changelog Co-Authored-By: Roman Zavarnitsyn <roman.zavarnitsyn@sentry.io> * fix(profiling): Treat ProfilingManager lookup as a system-service boundary Co-Authored-By: Roman Zavarnitsyn <roman.zavarnitsyn@sentry.io> * test(profiling): Use Truth for new PerfettoProfiler assertions --------- Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com> Co-authored-by: Roman Zavarnitsyn <roman.zavarnitsyn@sentry.io>
1 parent 08bd9c0 commit 8884e89

3 files changed

Lines changed: 74 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
> - Java 8 support will be dropped. SDK 9.0.0 requires Java 11+. See [#6469](https://github.com/getsentry/sentry-java/issues/6469).
66
> - The Android `minSdk` will be raised from 21 to 24. See [#5508](https://github.com/getsentry/sentry-java/issues/5508).
77

8+
## Unreleased
9+
10+
### Fixes
11+
12+
- Profiling: No longer crash on API 35+ devices whose framework can't load `ProfilingManager`. Perfetto profiling is turned off on those devices instead ([#6742](https://github.com/getsentry/sentry-java/pull/6742))
13+
814
## 8.65.1
915

1016
### Fixes

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import io.sentry.SentryLevel;
1414
import io.sentry.android.core.internal.profiling.ChunkRecord;
1515
import io.sentry.profiling.ProfileRecordingState;
16+
import io.sentry.util.ExceptionUtils;
1617
import java.io.File;
1718
import java.util.concurrent.RejectedExecutionException;
1819
import java.util.function.Consumer;
@@ -56,15 +57,11 @@ public final class PerfettoProfiler {
5657

5758
private volatile @Nullable ChunkRecord chunkRecord = null;
5859

59-
@SuppressLint("WrongConstant")
6060
public PerfettoProfiler(
6161
final @NotNull Context context,
6262
final @NotNull ILogger logger,
6363
final @NotNull ISentryExecutorService executorService) {
64-
this(
65-
logger,
66-
executorService,
67-
(ProfilingManager) context.getSystemService(Context.PROFILING_SERVICE));
64+
this(logger, executorService, getProfilingManager(context, logger));
6865
}
6966

7067
PerfettoProfiler(
@@ -76,6 +73,30 @@ public PerfettoProfiler(
7673
this.profilingManager = profilingManager;
7774
}
7875

76+
/**
77+
* Returns null when the device can't provide ProfilingManager, which makes {@link #start} fail,
78+
* so the device records no profiles.
79+
*/
80+
@SuppressLint("WrongConstant")
81+
private static @Nullable ProfilingManager getProfilingManager(
82+
final @NotNull Context context, final @NotNull ILogger logger) {
83+
try {
84+
return (ProfilingManager) context.getSystemService(Context.PROFILING_SERVICE);
85+
} catch (LinkageError e) {
86+
// Some devices report API 35+ but ship a framework whose ProfilingManager can't be loaded
87+
// (NoClassDefFoundError) or initialized (ExceptionInInitializerError). Caught before the
88+
// broad catch below, because rethrowIfFatal rethrows every LinkageError.
89+
logger.log(SentryLevel.WARNING, "ProfilingManager can't be loaded on this device.", e);
90+
return null;
91+
} catch (Throwable t) {
92+
// Broad on purpose: getSystemService is a system-service boundary, and OEM frameworks can
93+
// throw anything from it.
94+
ExceptionUtils.rethrowIfFatal(t);
95+
logger.log(SentryLevel.WARNING, "ProfilingManager is not available on this device.", t);
96+
return null;
97+
}
98+
}
99+
79100
/**
80101
* Starts a profiling session for the given chunk, and tells whether it could be started. A
81102
* failure the OS reports later is written to that record as it happens, so that whoever holds it

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import android.os.ProfilingManager
55
import android.os.ProfilingResult
66
import androidx.test.core.app.ApplicationProvider
77
import androidx.test.ext.junit.runners.AndroidJUnit4
8+
import com.google.common.truth.Truth.assertThat
89
import io.sentry.ILogger
10+
import io.sentry.SentryLevel
911
import io.sentry.SentryNanotimeDate
1012
import io.sentry.android.core.internal.profiling.ChunkRecord
1113
import io.sentry.profiling.ProfileRecordingState
@@ -20,14 +22,17 @@ import java.util.function.Consumer
2022
import kotlin.test.BeforeTest
2123
import kotlin.test.Test
2224
import kotlin.test.assertEquals
25+
import kotlin.test.assertFailsWith
2326
import kotlin.test.assertFalse
2427
import kotlin.test.assertNotNull
2528
import kotlin.test.assertNull
2629
import kotlin.test.assertTrue
2730
import org.junit.runner.RunWith
2831
import org.mockito.kotlin.any
2932
import org.mockito.kotlin.doAnswer
33+
import org.mockito.kotlin.eq
3034
import org.mockito.kotlin.mock
35+
import org.mockito.kotlin.verify
3136
import org.mockito.kotlin.whenever
3237
import org.robolectric.annotation.Config
3338

@@ -105,6 +110,43 @@ class PerfettoProfilerTest {
105110
assertNull(profiler.startSession())
106111
}
107112

113+
@Test
114+
fun `start returns null when ProfilingManager can't be loaded`() {
115+
assertStartFailsWhenGetSystemServiceThrows(NoClassDefFoundError("android.os.ProfilingManager"))
116+
}
117+
118+
@Test
119+
fun `start returns null when ProfilingManager can't be initialized`() {
120+
assertStartFailsWhenGetSystemServiceThrows(ExceptionInInitializerError())
121+
}
122+
123+
@Test
124+
fun `start returns null when getSystemService throws`() {
125+
assertStartFailsWhenGetSystemServiceThrows(IllegalStateException())
126+
}
127+
128+
@Test
129+
fun `fatal errors from getSystemService are rethrown`() {
130+
val error = OutOfMemoryError()
131+
val context =
132+
mock<Context>().also {
133+
whenever(it.getSystemService(Context.PROFILING_SERVICE)).thenThrow(error)
134+
}
135+
val thrown =
136+
assertFailsWith<OutOfMemoryError> { PerfettoProfiler(context, mockLogger, executor) }
137+
assertThat(thrown).isSameInstanceAs(error)
138+
}
139+
140+
private fun assertStartFailsWhenGetSystemServiceThrows(error: Throwable) {
141+
val context =
142+
mock<Context>().also {
143+
whenever(it.getSystemService(Context.PROFILING_SERVICE)).thenThrow(error)
144+
}
145+
val profiler = PerfettoProfiler(context, mockLogger, executor)
146+
assertThat(profiler.startSession()).isNull()
147+
verify(mockLogger).log(eq(SentryLevel.WARNING), any<String>(), eq(error))
148+
}
149+
108150
@Test
109151
fun `endAndCollect calls listener with null when never started`() {
110152
val profiler = getSut()

0 commit comments

Comments
 (0)