Skip to content

Commit f3561de

Browse files
committed
fix(android): Fix crash when getHistoricalProcessStartReasons is called from a wrong process
1 parent 57d359a commit f3561de

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import android.os.Handler;
1212
import android.os.Looper;
1313
import android.os.SystemClock;
14+
import android.util.Log;
1415
import androidx.annotation.NonNull;
1516
import androidx.annotation.Nullable;
1617
import androidx.annotation.VisibleForTesting;
@@ -467,18 +468,28 @@ public void registerLifecycleCallbacks(final @NotNull Application application) {
467468
final @Nullable ActivityManager activityManager =
468469
(ActivityManager) application.getSystemService(Context.ACTIVITY_SERVICE);
469470
if (activityManager != null) {
470-
final List<ApplicationStartInfo> historicalProcessStartReasons =
471-
activityManager.getHistoricalProcessStartReasons(1);
472-
if (!historicalProcessStartReasons.isEmpty()) {
473-
final @NotNull ApplicationStartInfo info = historicalProcessStartReasons.get(0);
474-
cachedStartInfo = info;
475-
if (info.getStartupState() == ApplicationStartInfo.STARTUP_STATE_STARTED) {
476-
if (info.getStartType() == ApplicationStartInfo.START_TYPE_COLD) {
477-
appStartType = AppStartType.COLD;
478-
} else {
479-
appStartType = AppStartType.WARM;
471+
try {
472+
final List<ApplicationStartInfo> historicalProcessStartReasons =
473+
activityManager.getHistoricalProcessStartReasons(1);
474+
if (!historicalProcessStartReasons.isEmpty()) {
475+
final @NotNull ApplicationStartInfo info = historicalProcessStartReasons.get(0);
476+
cachedStartInfo = info;
477+
if (info.getStartupState() == ApplicationStartInfo.STARTUP_STATE_STARTED) {
478+
if (info.getStartType() == ApplicationStartInfo.START_TYPE_COLD) {
479+
appStartType = AppStartType.COLD;
480+
} else {
481+
appStartType = AppStartType.WARM;
482+
}
480483
}
481484
}
485+
} catch (RuntimeException ignored) {
486+
// getHistoricalProcessStartReasons may throw different kinds of exceptions, namely:
487+
// - SecurityException when called from an isolated process
488+
// - IllegalArgumentException when called with a wrong userId
489+
// - others
490+
// See impl:
491+
// https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java;l=10866-10893
492+
Log.w("AppStartMetrics", ignored); // no logger instance here, so we just Log
482493
}
483494
}
484495
}

0 commit comments

Comments
 (0)