-
-
Notifications
You must be signed in to change notification settings - Fork 471
fix(android): Fix crash when getHistoricalProcessStartReasons is called from a wrong process #5597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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) { | ||
| // 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏅 |
||
| Log.w("AppStartMetrics", ignored); // no logger instance here, so we just Log | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
|
||
There was a problem hiding this comment.
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->eb/c we log it.