diff --git a/CHANGELOG.md b/CHANGELOG.md index eaf59a76d94..ab4ac783513 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ > - Java 8 support will be dropped. SDK 9.0.0 requires Java 11+. See [#6469](https://github.com/getsentry/sentry-java/issues/6469). > - The Android `minSdk` will be raised from 21 to 24. See [#5508](https://github.com/getsentry/sentry-java/issues/5508). +## Unreleased + +### Fixes + +- 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)) + ## 8.65.1 ### Fixes diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/PerfettoProfiler.java b/sentry-android-core/src/main/java/io/sentry/android/core/PerfettoProfiler.java index 4a64359aa44..50651f9e97c 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/PerfettoProfiler.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/PerfettoProfiler.java @@ -13,6 +13,7 @@ import io.sentry.SentryLevel; import io.sentry.android.core.internal.profiling.ChunkRecord; import io.sentry.profiling.ProfileRecordingState; +import io.sentry.util.ExceptionUtils; import java.io.File; import java.util.concurrent.RejectedExecutionException; import java.util.function.Consumer; @@ -56,15 +57,11 @@ public final class PerfettoProfiler { private volatile @Nullable ChunkRecord chunkRecord = null; - @SuppressLint("WrongConstant") public PerfettoProfiler( final @NotNull Context context, final @NotNull ILogger logger, final @NotNull ISentryExecutorService executorService) { - this( - logger, - executorService, - (ProfilingManager) context.getSystemService(Context.PROFILING_SERVICE)); + this(logger, executorService, getProfilingManager(context, logger)); } PerfettoProfiler( @@ -76,6 +73,30 @@ public PerfettoProfiler( this.profilingManager = profilingManager; } + /** + * Returns null when the device can't provide ProfilingManager, which makes {@link #start} fail, + * so the device records no profiles. + */ + @SuppressLint("WrongConstant") + private static @Nullable ProfilingManager getProfilingManager( + final @NotNull Context context, final @NotNull ILogger logger) { + try { + return (ProfilingManager) context.getSystemService(Context.PROFILING_SERVICE); + } catch (LinkageError e) { + // Some devices report API 35+ but ship a framework whose ProfilingManager can't be loaded + // (NoClassDefFoundError) or initialized (ExceptionInInitializerError). Caught before the + // broad catch below, because rethrowIfFatal rethrows every LinkageError. + logger.log(SentryLevel.WARNING, "ProfilingManager can't be loaded on this device.", e); + return null; + } catch (Throwable t) { + // Broad on purpose: getSystemService is a system-service boundary, and OEM frameworks can + // throw anything from it. + ExceptionUtils.rethrowIfFatal(t); + logger.log(SentryLevel.WARNING, "ProfilingManager is not available on this device.", t); + return null; + } + } + /** * Starts a profiling session for the given chunk, and tells whether it could be started. A * failure the OS reports later is written to that record as it happens, so that whoever holds it diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/PerfettoProfilerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/PerfettoProfilerTest.kt index a745143a38e..3bcacb13d18 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/PerfettoProfilerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/PerfettoProfilerTest.kt @@ -5,7 +5,9 @@ import android.os.ProfilingManager import android.os.ProfilingResult import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.google.common.truth.Truth.assertThat import io.sentry.ILogger +import io.sentry.SentryLevel import io.sentry.SentryNanotimeDate import io.sentry.android.core.internal.profiling.ChunkRecord import io.sentry.profiling.ProfileRecordingState @@ -20,6 +22,7 @@ import java.util.function.Consumer import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertFailsWith import kotlin.test.assertFalse import kotlin.test.assertNotNull import kotlin.test.assertNull @@ -27,7 +30,9 @@ import kotlin.test.assertTrue import org.junit.runner.RunWith import org.mockito.kotlin.any import org.mockito.kotlin.doAnswer +import org.mockito.kotlin.eq import org.mockito.kotlin.mock +import org.mockito.kotlin.verify import org.mockito.kotlin.whenever import org.robolectric.annotation.Config @@ -105,6 +110,43 @@ class PerfettoProfilerTest { assertNull(profiler.startSession()) } + @Test + fun `start returns null when ProfilingManager can't be loaded`() { + assertStartFailsWhenGetSystemServiceThrows(NoClassDefFoundError("android.os.ProfilingManager")) + } + + @Test + fun `start returns null when ProfilingManager can't be initialized`() { + assertStartFailsWhenGetSystemServiceThrows(ExceptionInInitializerError()) + } + + @Test + fun `start returns null when getSystemService throws`() { + assertStartFailsWhenGetSystemServiceThrows(IllegalStateException()) + } + + @Test + fun `fatal errors from getSystemService are rethrown`() { + val error = OutOfMemoryError() + val context = + mock().also { + whenever(it.getSystemService(Context.PROFILING_SERVICE)).thenThrow(error) + } + val thrown = + assertFailsWith { PerfettoProfiler(context, mockLogger, executor) } + assertThat(thrown).isSameInstanceAs(error) + } + + private fun assertStartFailsWhenGetSystemServiceThrows(error: Throwable) { + val context = + mock().also { + whenever(it.getSystemService(Context.PROFILING_SERVICE)).thenThrow(error) + } + val profiler = PerfettoProfiler(context, mockLogger, executor) + assertThat(profiler.startSession()).isNull() + verify(mockLogger).log(eq(SentryLevel.WARNING), any(), eq(error)) + } + @Test fun `endAndCollect calls listener with null when never started`() { val profiler = getSut()