From 8c9f0c835a3f36597ed06bce0266f8859b0ffc9c Mon Sep 17 00:00:00 2001 From: Sebastian Kaspari Date: Fri, 5 Nov 2021 13:14:59 +0100 Subject: [PATCH] Closes #21695: ThreadPenaltyDeathWithIgnoresListener: Ignore stack traces containing InstrumentationHooks class --- .../ThreadPenaltyDeathWithIgnoresListener.kt | 13 +++++++++++- ...readPenaltyDeathWithIgnoresListenerTest.kt | 11 ++++++++++ .../resources/InstrumentationHooksLogcat.txt | 20 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 app/src/test/resources/InstrumentationHooksLogcat.txt diff --git a/app/src/main/java/org/mozilla/fenix/perf/ThreadPenaltyDeathWithIgnoresListener.kt b/app/src/main/java/org/mozilla/fenix/perf/ThreadPenaltyDeathWithIgnoresListener.kt index fcc7774b32..87f8da48ec 100644 --- a/app/src/main/java/org/mozilla/fenix/perf/ThreadPenaltyDeathWithIgnoresListener.kt +++ b/app/src/main/java/org/mozilla/fenix/perf/ThreadPenaltyDeathWithIgnoresListener.kt @@ -12,6 +12,7 @@ import mozilla.components.support.base.log.logger.Logger import org.mozilla.fenix.utils.ManufacturerCodes private const val FCQN_EDM_STORAGE_PROVIDER_BASE = "com.android.server.enterprise.storage.EdmStorageProviderBase" +private const val INSTRUMENTED_HOOKS_CLASS = "com.android.tools.deploy.instrument.InstrumentationHooks" /** * A [StrictMode.OnThreadViolationListener] that recreates @@ -45,7 +46,8 @@ class ThreadPenaltyDeathWithIgnoresListener( } private fun shouldViolationBeIgnored(violation: Violation): Boolean = - isSamsungLgEdmStorageProviderStartupViolation(violation) + isSamsungLgEdmStorageProviderStartupViolation(violation) || + containsInstrumentedHooksClass(violation) private fun isSamsungLgEdmStorageProviderStartupViolation(violation: Violation): Boolean { // Root issue: https://github.com/mozilla-mobile/fenix/issues/17920 @@ -70,4 +72,13 @@ class ThreadPenaltyDeathWithIgnoresListener( // issue, we'd catch it on other manufacturers. return violation.stackTrace.any { it.className == FCQN_EDM_STORAGE_PROVIDER_BASE } } + + private fun containsInstrumentedHooksClass(violation: Violation): Boolean { + // See https://github.com/mozilla-mobile/fenix/issues/21695 + // When deploying debug builds from Android Studio then we may hit a DiskReadViolation + // occasionally. There's an upstream fix for this, but the stable version of Android Studio + // still seems to be affected. + // https://cs.android.com/android-studio/platform/tools/base/+/abbbe67087626460e0127d3f5377f9cf896e9941 + return violation.stackTrace.any { it.className == INSTRUMENTED_HOOKS_CLASS } + } } diff --git a/app/src/test/java/org/mozilla/fenix/perf/ThreadPenaltyDeathWithIgnoresListenerTest.kt b/app/src/test/java/org/mozilla/fenix/perf/ThreadPenaltyDeathWithIgnoresListenerTest.kt index f046e406c3..25e54e4181 100644 --- a/app/src/test/java/org/mozilla/fenix/perf/ThreadPenaltyDeathWithIgnoresListenerTest.kt +++ b/app/src/test/java/org/mozilla/fenix/perf/ThreadPenaltyDeathWithIgnoresListenerTest.kt @@ -68,6 +68,14 @@ class ThreadPenaltyDeathWithIgnoresListenerTest { listener.onThreadViolation(violation) } + @Test + fun `WHEN provided the InstrumentationHooks violation THEN it will be ignored and logged`() { + every { violation.stackTrace } returns getInstrumentationHooksStackTrace() + listener.onThreadViolation(violation) + + verify { logger.debug("Ignoring StrictMode ThreadPolicy violation", violation) } + } + @Test fun `WHEN violation is null THEN we don't throw an exception`() { listener.onThreadViolation(null) @@ -75,4 +83,7 @@ class ThreadPenaltyDeathWithIgnoresListenerTest { private fun getEdmStorageProviderStackTrace() = StackTraces.getStackTraceFromLogcat("EdmStorageProviderBaseLogcat.txt") + + private fun getInstrumentationHooksStackTrace() = + StackTraces.getStackTraceFromLogcat("InstrumentationHooksLogcat.txt") } diff --git a/app/src/test/resources/InstrumentationHooksLogcat.txt b/app/src/test/resources/InstrumentationHooksLogcat.txt new file mode 100644 index 0000000000..94d1803654 --- /dev/null +++ b/app/src/test/resources/InstrumentationHooksLogcat.txt @@ -0,0 +1,20 @@ +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1659) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at libcore.io.ForwardingOs.access(ForwardingOs.java:131) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:7719) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:281) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at java.io.File.exists(File.java:813) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at com.android.tools.deploy.instrument.ResourceOverlays.updateLoader(ResourceOverlays.java:73) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at com.android.tools.deploy.instrument.ResourceOverlays.addResourceOverlays(ResourceOverlays.java:36) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at com.android.tools.deploy.instrument.InstrumentationHooks.addResourceOverlays(InstrumentationHooks.java:102) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at android.app.LoadedApk.getResources(LoadedApk.java:1312) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at android.app.ContextImpl.createAppContext(ContextImpl.java:3011) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at android.app.ContextImpl.createAppContext(ContextImpl.java:3003) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at android.app.Service.createServiceBaseContext(Service.java:904) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at android.app.ActivityThread.handleCreateService(ActivityThread.java:4475) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at android.app.ActivityThread.access$1700(ActivityThread.java:247) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2076) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at android.os.Looper.loopOnce(Looper.java:201) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at android.os.Looper.loop(Looper.java:288) +02-08 10:56:02.185 21990 21990 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7842)