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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,14 +22,17 @@ 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
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

Expand Down Expand Up @@ -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<Context>().also {
whenever(it.getSystemService(Context.PROFILING_SERVICE)).thenThrow(error)
}
val thrown =
assertFailsWith<OutOfMemoryError> { PerfettoProfiler(context, mockLogger, executor) }
assertThat(thrown).isSameInstanceAs(error)
}

private fun assertStartFailsWhenGetSystemServiceThrows(error: Throwable) {
val context =
mock<Context>().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<String>(), eq(error))
}

@Test
fun `endAndCollect calls listener with null when never started`() {
val profiler = getSut()
Expand Down
Loading