diff --git a/core/java/com/android/internal/os/LooperStats.java b/core/java/com/android/internal/os/LooperStats.java index 0f0eeddf9a472..b3d9ca7670b51 100644 --- a/core/java/com/android/internal/os/LooperStats.java +++ b/core/java/com/android/internal/os/LooperStats.java @@ -126,9 +126,11 @@ public class LooperStats implements Looper.Observer { } DispatchSession session = (DispatchSession) token; - Entry entry = findEntry(msg, /* allowCreateNew= */true); - synchronized (entry) { - entry.exceptionCount++; + Entry entry = findEntry(msg, /* allowCreateNew= */session != DispatchSession.NOT_SAMPLED); + if (entry != null) { + synchronized (entry) { + entry.exceptionCount++; + } } recycleSession(session); diff --git a/core/tests/coretests/src/com/android/internal/os/LooperStatsTest.java b/core/tests/coretests/src/com/android/internal/os/LooperStatsTest.java index 2c597b14c5573..79b8477543115 100644 --- a/core/tests/coretests/src/com/android/internal/os/LooperStatsTest.java +++ b/core/tests/coretests/src/com/android/internal/os/LooperStatsTest.java @@ -137,6 +137,26 @@ public final class LooperStatsTest { assertThat(entry.maxCpuUsageMicros).isEqualTo(0); } + @Test + public void testThrewException_notSampled() { + TestableLooperStats looperStats = new TestableLooperStats(2, 100); + + Object token = looperStats.messageDispatchStarting(); + looperStats.tickRealtime(10); + looperStats.tickThreadTime(10); + looperStats.messageDispatched(token, mHandlerFirst.obtainMessage(0)); + assertThat(looperStats.getEntries()).hasSize(1); + + // Will not be sampled so does not contribute to any entries. + Object token2 = looperStats.messageDispatchStarting(); + looperStats.tickRealtime(100); + looperStats.tickThreadTime(10); + looperStats.dispatchingThrewException( + token2, mHandlerSecond.obtainMessage(7), new ArithmeticException()); + assertThat(looperStats.getEntries()).hasSize(1); + assertThat(looperStats.getEntries().get(0).messageCount).isEqualTo(1); + } + @Test public void testMultipleMessagesDispatched() { TestableLooperStats looperStats = new TestableLooperStats(2, 100);