Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixes

- Fix crash when `getHistoricalProcessStartReasons` is called from an isolated or wrong-userId process ([#5597](https://github.com/getsentry/sentry-java/pull/5597))
- Release `MediaMuxer` when a replay segment has no encodable frames to avoid a resource leak ([#5583](https://github.com/getsentry/sentry-java/pull/5583))

## 8.44.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
Expand Down Expand Up @@ -467,18 +468,28 @@ public void registerLifecycleCallbacks(final @NotNull Application application) {
final @Nullable ActivityManager activityManager =
(ActivityManager) application.getSystemService(Context.ACTIVITY_SERVICE);
if (activityManager != null) {
final List<ApplicationStartInfo> historicalProcessStartReasons =
activityManager.getHistoricalProcessStartReasons(1);
if (!historicalProcessStartReasons.isEmpty()) {
final @NotNull ApplicationStartInfo info = historicalProcessStartReasons.get(0);
cachedStartInfo = info;
if (info.getStartupState() == ApplicationStartInfo.STARTUP_STATE_STARTED) {
if (info.getStartType() == ApplicationStartInfo.START_TYPE_COLD) {
appStartType = AppStartType.COLD;
} else {
appStartType = AppStartType.WARM;
try {
final List<ApplicationStartInfo> historicalProcessStartReasons =
activityManager.getHistoricalProcessStartReasons(1);
if (!historicalProcessStartReasons.isEmpty()) {
final @NotNull ApplicationStartInfo info = historicalProcessStartReasons.get(0);
cachedStartInfo = info;
if (info.getStartupState() == ApplicationStartInfo.STARTUP_STATE_STARTED) {
if (info.getStartType() == ApplicationStartInfo.START_TYPE_COLD) {
appStartType = AppStartType.COLD;
} else {
appStartType = AppStartType.WARM;
}
}
}
} catch (RuntimeException ignored) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit if you want it: ignored -> e b/c we log it.

// getHistoricalProcessStartReasons may throw different kinds of exceptions, namely:
// - SecurityException when called from an isolated process
// - IllegalArgumentException when called with a wrong userId
// - others
// See impl:
// https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java;l=10866-10893

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏅

Log.w("AppStartMetrics", ignored); // no logger instance here, so we just Log
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ class SentryShadowActivityManager {
companion object {
private var historicalProcessStartReasons: List<ApplicationStartInfo> = emptyList()
private var importance: Int = RunningAppProcessInfo.IMPORTANCE_FOREGROUND
private var historicalProcessStartReasonsException: RuntimeException? = null

fun setHistoricalProcessStartReasons(startReasons: List<ApplicationStartInfo>) {
historicalProcessStartReasons = startReasons
}

fun setHistoricalProcessStartReasonsException(exception: RuntimeException) {
historicalProcessStartReasonsException = exception
}

fun setImportance(importance: Int) {
this.importance = importance
}

fun reset() {
historicalProcessStartReasons = emptyList()
importance = RunningAppProcessInfo.IMPORTANCE_FOREGROUND
historicalProcessStartReasonsException = null
}

@Implementation
Expand All @@ -35,6 +41,7 @@ class SentryShadowActivityManager {

@Implementation
fun getHistoricalProcessStartReasons(maxNum: Int): List<ApplicationStartInfo> {
historicalProcessStartReasonsException?.let { throw it }
return historicalProcessStartReasons.take(maxNum)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ class AppStartMetricsTestApi35 {
assertNull(metrics.appStartReason)
}

@Test
fun `does not crash when getHistoricalProcessStartReasons throws SecurityException`() {
SentryShadowActivityManager.setHistoricalProcessStartReasonsException(
SecurityException("isolated process")
)
val metrics = AppStartMetrics.getInstance()

val app = ApplicationProvider.getApplicationContext<Application>()
metrics.registerLifecycleCallbacks(app)

assertEquals(AppStartMetrics.AppStartType.UNKNOWN, metrics.appStartType)
assertNull(metrics.appStartReason)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: Second test for IllegalStateException (or broaden the existing one for RuntimeException)?

(I feel obliged to say it^^ given the caught type + inline comment, but up to you...)

private fun waitForMainLooperIdle() {
Handler(Looper.getMainLooper()).post {}
Shadows.shadowOf(Looper.getMainLooper()).idle()
Expand Down
Loading