diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java b/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java index 1fafe4e0d9..86da5ad858 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java @@ -15,10 +15,13 @@ import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.SentryOptions; +import io.sentry.android.core.anr.NonFatalAnrMarker; import io.sentry.android.core.cache.AndroidEnvelopeCache; import io.sentry.android.core.internal.threaddump.Lines; import io.sentry.android.core.internal.threaddump.ThreadDumpParser; import io.sentry.android.core.internal.util.NativeEventUtils; +import io.sentry.cache.EnvelopeCache; +import io.sentry.cache.IEnvelopeCache; import io.sentry.hints.AbnormalExit; import io.sentry.hints.Backfillable; import io.sentry.hints.BlockingFlushHint; @@ -72,12 +75,22 @@ public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { } if (this.options.isAnrEnabled()) { + final @NotNull SentryAndroidOptions androidOptions = this.options; try { + final @NotNull ApplicationExitInfoHistoryDispatcher dispatcher = + new ApplicationExitInfoHistoryDispatcher( + context, scopes, androidOptions, new AnrV2Policy(androidOptions)); + // This process writes its marker only from a later task on this executor, so this never + // deletes it. options .getExecutorService() - .submit( - new ApplicationExitInfoHistoryDispatcher( - context, scopes, this.options, new AnrV2Policy(this.options))); + .submit(() -> { + try { + dispatcher.run(); + } finally { + NonFatalAnrMarker.delete(androidOptions); + } + }); } catch (Throwable e) { options.getLogger().log(SentryLevel.DEBUG, "Failed to start ANR processor.", e); } @@ -133,6 +146,25 @@ public void markReported(final long timestamp) { final long anrTimestamp = exitInfo.getTimestamp(); final boolean isBackground = exitInfo.getImportance() != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; + final AnrV2Hint anrHint = + new AnrV2Hint( + options.getFlushTimeoutMillis(), + options.getLogger(), + anrTimestamp, + shouldEnrich, + isBackground); + + if (shouldEnrich && wasReportedAsNonFatal(exitInfo)) { + options + .getLogger() + .log( + SentryLevel.DEBUG, + "Not reporting ANR event as it was already reported as a non-fatal ANR %s", + exitInfo.toString()); + endPreviousSession(anrHint); + markReported(anrTimestamp); + return null; + } final ParseResult result = parseThreadDump(exitInfo, isBackground); if (result.type == ParseResult.Type.NO_DUMP) { @@ -144,13 +176,6 @@ public void markReported(final long timestamp) { exitInfo.toString()); return null; } - final AnrV2Hint anrHint = - new AnrV2Hint( - options.getFlushTimeoutMillis(), - options.getLogger(), - anrTimestamp, - shouldEnrich, - isBackground); final Hint hint = HintUtils.createWithTypeCheckHint(anrHint); @@ -185,6 +210,19 @@ public void markReported(final long timestamp) { return new ApplicationExitInfoHistoryDispatcher.Report(event, hint, anrHint); } + private boolean wasReportedAsNonFatal(final @NotNull ApplicationExitInfo exitInfo) { + final @Nullable NonFatalAnrMarker marker = NonFatalAnrMarker.read(options); + return marker != null && marker.matches(exitInfo.getPid(), exitInfo.getTimestamp()); + } + + // Without an event, PreviousSessionFinalizer would end the session as normal. + private void endPreviousSession(final @NotNull AnrV2Hint anrHint) { + final @NotNull IEnvelopeCache cache = options.getEnvelopeDiskCache(); + if (cache instanceof EnvelopeCache) { + ((EnvelopeCache) cache).endPreviousSessionAbnormally(anrHint); + } + } + private @NotNull ParseResult parseThreadDump( final @NotNull ApplicationExitInfo exitInfo, final boolean isBackground) { final byte[] dump; diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2IntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2IntegrationTest.kt index 5d43c28ede..e90f43fe2f 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2IntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2IntegrationTest.kt @@ -3,12 +3,19 @@ package io.sentry.android.core import android.app.ActivityManager import android.app.ApplicationExitInfo import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.google.common.truth.Truth.assertThat import io.sentry.Hint import io.sentry.SentryEvent +import io.sentry.SentryUUID +import io.sentry.Session import io.sentry.android.core.AnrV2Integration.AnrV2Hint +import io.sentry.android.core.anr.NonFatalAnrMarker import io.sentry.android.core.cache.AndroidEnvelopeCache +import io.sentry.cache.EnvelopeCache import io.sentry.util.HintUtils import java.io.File +import java.util.Date +import java.util.concurrent.TimeUnit import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull @@ -42,88 +49,100 @@ class AnrV2IntegrationTest : ApplicationExitIntegrationTestBase() { timestamp = { it.timestamp() }, ), addExitInfo = { reason, timestamp, importance, addTrace, addBadTrace -> - val builder = ApplicationExitInfoBuilder.newBuilder() - reason?.let { builder.setReason(it) } - timestamp?.let { builder.setTimestamp(it) } - importance?.let { builder.setImportance(it) } - val exitInfo = - spy(builder.build()) { - if (!addTrace) { - return@spy - } - if (addBadTrace) { - whenever(mock.traceInputStream) - .thenReturn( - """ - Subject: Input dispatching timed out (7985007 com.example.app/com.example.app.ui.MainActivity (server) is not responding. Waited 5000ms for FocusEvent(hasFocus=false)) - Here are no Binder-related exception messages available. - Pid(12233) have D state thread(tid:12236 name:Signal Catcher) - - - RssHwmKb: 823716 - RssKb: 548348 - RssAnonKb: 382156 - RssShmemKb: 13304 - VmSwapKb: 82484 - - - --- CriticalEventLog --- - capacity: 20 - timestamp_ms: 1731507490032 - window_ms: 300000 - - ----- dumping pid: 12233 at 313446151 - libdebuggerd_client: unexpected registration response: 0 - - ----- Waiting Channels: pid 12233 at 2024-11-13 19:48:09.980104540+0530 ----- - Cmd line: com.example.app:mainProcess - """ - .trimIndent() - .byteInputStream(), - ) - } else { - whenever(mock.traceInputStream) - .thenReturn( - """ - "main" prio=5 tid=1 Blocked - | group="main" sCount=1 ucsCount=0 flags=1 obj=0x72a985e0 self=0xb400007cabc57380 - | sysTid=28941 nice=-10 cgrp=top-app sched=0/0 handle=0x7deceb74f8 - | state=S schedstat=( 324804784 183300334 997 ) utm=23 stm=8 core=3 HZ=100 - | stack=0x7ff93a9000-0x7ff93ab000 stackSize=8188KB - | held mutexes= - at io.sentry.samples.android.MainActivity${'$'}2.run(MainActivity.java:177) - - waiting to lock <0x0d3a2f0a> (a java.lang.Object) held by thread 5 - at android.os.Handler.handleCallback(Handler.java:942) - at android.os.Handler.dispatchMessage(Handler.java:99) - at android.os.Looper.loopOnce(Looper.java:201) - at android.os.Looper.loop(Looper.java:288) - at android.app.ActivityThread.main(ActivityThread.java:7872) - at java.lang.reflect.Method.invoke(Native method) - at com.android.internal.os.RuntimeInit${'$'}MethodAndArgsCaller.run(RuntimeInit.java:548) - at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) - - "perfetto_hprof_listener" prio=10 tid=7 Native (still starting up) - | group="" sCount=1 ucsCount=0 flags=1 obj=0x0 self=0xb400007cabc5ab20 - | sysTid=28959 nice=-20 cgrp=top-app sched=0/0 handle=0x7b2021bcb0 - | state=S schedstat=( 72750 1679167 1 ) utm=0 stm=0 core=3 HZ=100 - | stack=0x7b20124000-0x7b20126000 stackSize=991KB - | held mutexes= - native: #00 pc 00000000000a20f4 /apex/com.android.runtime/lib64/bionic/libc.so (read+4) (BuildId: 01331f74b0bb2cb958bdc15282b8ec7b) - native: #01 pc 000000000001d840 /apex/com.android.art/lib64/libperfetto_hprof.so (void* std::__1::__thread_proxy >, ArtPlugin_Initialize::${'$'}_34> >(void*)+260) (BuildId: 525cc92a7dc49130157aeb74f6870364) - native: #02 pc 00000000000b63b0 /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start(void*)+208) (BuildId: 01331f74b0bb2cb958bdc15282b8ec7b) - native: #03 pc 00000000000530b8 /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+64) (BuildId: 01331f74b0bb2cb958bdc15282b8ec7b) - (no managed stack frames) - """ - .trimIndent() - .byteInputStream(), - ) - } - } - shadowActivityManager.addApplicationExitInfo(exitInfo) + addAnrExitInfo(reason, timestamp, importance, null, addTrace, addBadTrace) }, flushLogPrefix = "Timed out waiting to flush %s event to disk.", ) + private fun ApplicationExitTestFixture.addAnrExitInfo( + reason: Int? = ApplicationExitInfo.REASON_ANR, + timestamp: Long? = null, + importance: Int? = null, + pid: Int? = null, + addTrace: Boolean = true, + addBadTrace: Boolean = false, + ) { + val builder = ApplicationExitInfoBuilder.newBuilder() + reason?.let { builder.setReason(it) } + timestamp?.let { builder.setTimestamp(it) } + importance?.let { builder.setImportance(it) } + pid?.let { builder.setPid(it) } + val exitInfo = + spy(builder.build()) { + if (!addTrace) { + return@spy + } + if (addBadTrace) { + whenever(mock.traceInputStream) + .thenReturn( + """ + Subject: Input dispatching timed out (7985007 com.example.app/com.example.app.ui.MainActivity (server) is not responding. Waited 5000ms for FocusEvent(hasFocus=false)) + Here are no Binder-related exception messages available. + Pid(12233) have D state thread(tid:12236 name:Signal Catcher) + + + RssHwmKb: 823716 + RssKb: 548348 + RssAnonKb: 382156 + RssShmemKb: 13304 + VmSwapKb: 82484 + + + --- CriticalEventLog --- + capacity: 20 + timestamp_ms: 1731507490032 + window_ms: 300000 + + ----- dumping pid: 12233 at 313446151 + libdebuggerd_client: unexpected registration response: 0 + + ----- Waiting Channels: pid 12233 at 2024-11-13 19:48:09.980104540+0530 ----- + Cmd line: com.example.app:mainProcess + """ + .trimIndent() + .byteInputStream(), + ) + } else { + whenever(mock.traceInputStream) + .thenReturn( + """ + "main" prio=5 tid=1 Blocked + | group="main" sCount=1 ucsCount=0 flags=1 obj=0x72a985e0 self=0xb400007cabc57380 + | sysTid=28941 nice=-10 cgrp=top-app sched=0/0 handle=0x7deceb74f8 + | state=S schedstat=( 324804784 183300334 997 ) utm=23 stm=8 core=3 HZ=100 + | stack=0x7ff93a9000-0x7ff93ab000 stackSize=8188KB + | held mutexes= + at io.sentry.samples.android.MainActivity${'$'}2.run(MainActivity.java:177) + - waiting to lock <0x0d3a2f0a> (a java.lang.Object) held by thread 5 + at android.os.Handler.handleCallback(Handler.java:942) + at android.os.Handler.dispatchMessage(Handler.java:99) + at android.os.Looper.loopOnce(Looper.java:201) + at android.os.Looper.loop(Looper.java:288) + at android.app.ActivityThread.main(ActivityThread.java:7872) + at java.lang.reflect.Method.invoke(Native method) + at com.android.internal.os.RuntimeInit${'$'}MethodAndArgsCaller.run(RuntimeInit.java:548) + at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) + + "perfetto_hprof_listener" prio=10 tid=7 Native (still starting up) + | group="" sCount=1 ucsCount=0 flags=1 obj=0x0 self=0xb400007cabc5ab20 + | sysTid=28959 nice=-20 cgrp=top-app sched=0/0 handle=0x7b2021bcb0 + | state=S schedstat=( 72750 1679167 1 ) utm=0 stm=0 core=3 HZ=100 + | stack=0x7b20124000-0x7b20126000 stackSize=991KB + | held mutexes= + native: #00 pc 00000000000a20f4 /apex/com.android.runtime/lib64/bionic/libc.so (read+4) (BuildId: 01331f74b0bb2cb958bdc15282b8ec7b) + native: #01 pc 000000000001d840 /apex/com.android.art/lib64/libperfetto_hprof.so (void* std::__1::__thread_proxy >, ArtPlugin_Initialize::${'$'}_34> >(void*)+260) (BuildId: 525cc92a7dc49130157aeb74f6870364) + native: #02 pc 00000000000b63b0 /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start(void*)+208) (BuildId: 01331f74b0bb2cb958bdc15282b8ec7b) + native: #03 pc 00000000000530b8 /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+64) (BuildId: 01331f74b0bb2cb958bdc15282b8ec7b) + (no managed stack frames) + """ + .trimIndent() + .byteInputStream(), + ) + } + } + shadowActivityManager.addApplicationExitInfo(exitInfo) + } + override fun assertEnrichedEvent(event: SentryEvent) { val mainThread = event.threads!!.first() assertEquals("main", mainThread.name) @@ -246,4 +265,120 @@ class AnrV2IntegrationTest : ApplicationExitIntegrationTestBase() { verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } + + @Test + fun `matching marker suppresses fatal event and ends session abnormal`() { + val integration = fixture.getSut(tmpDir, lastReportedTimestamp = oldTimestamp) + val previousSessionFile = writePreviousSession(startedAt = newTimestamp - 60_000) + NonFatalAnrMarker.write(fixture.options, PID, newTimestamp - 8_000, newTimestamp - 3_000) + fixture.addAnrExitInfo( + timestamp = newTimestamp, + importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND, + pid = PID, + ) + + integration.register(fixture.scopes, fixture.options) + + verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) + val session = + fixture.options.serializer.deserialize( + previousSessionFile.bufferedReader(), + Session::class.java, + )!! + assertThat(session.status).isEqualTo(Session.State.Abnormal) + assertThat(session.abnormalMechanism).isEqualTo("anr_foreground") + assertThat(session.timestamp!!.time).isEqualTo(newTimestamp) + assertThat(fixture.lastReportedFile.readText()).isEqualTo(newTimestamp.toString()) + } + + @Test + fun `marker with other pid does not suppress`() { + val integration = fixture.getSut(tmpDir, lastReportedTimestamp = oldTimestamp) + NonFatalAnrMarker.write(fixture.options, PID + 1, newTimestamp - 8_000, newTimestamp - 3_000) + fixture.addAnrExitInfo(timestamp = newTimestamp, pid = PID) + + integration.register(fixture.scopes, fixture.options) + + verify(fixture.scopes).captureEvent(any(), anyOrNull()) + } + + @Test + fun `marker outside time threshold does not suppress`() { + val integration = fixture.getSut(tmpDir, lastReportedTimestamp = oldTimestamp) + NonFatalAnrMarker.write(fixture.options, PID, newTimestamp - 38_001, newTimestamp - 30_001) + fixture.addAnrExitInfo(timestamp = newTimestamp, pid = PID) + + integration.register(fixture.scopes, fixture.options) + + verify(fixture.scopes).captureEvent(any(), anyOrNull()) + } + + @Test + fun `marker is deleted after the check`() { + val integration = fixture.getSut(tmpDir, lastReportedTimestamp = oldTimestamp) + NonFatalAnrMarker.write(fixture.options, PID + 1, newTimestamp - 8_000, newTimestamp - 3_000) + fixture.addAnrExitInfo(timestamp = newTimestamp, pid = PID) + + integration.register(fixture.scopes, fixture.options) + + assertThat(NonFatalAnrMarker.read(fixture.options)).isNull() + } + + @Test + fun `marker is deleted when there is no ANR exit to check`() { + val integration = fixture.getSut(tmpDir, lastReportedTimestamp = oldTimestamp) + NonFatalAnrMarker.write(fixture.options, PID, newTimestamp - 8_000, newTimestamp - 3_000) + + integration.register(fixture.scopes, fixture.options) + + assertThat(NonFatalAnrMarker.read(fixture.options)).isNull() + } + + @Test + fun `historical ANRs are not affected`() { + val integration = fixture.getSut(tmpDir, lastReportedTimestamp = oldTimestamp) + val historicalTimestamp = newTimestamp - TimeUnit.DAYS.toMillis(1) + NonFatalAnrMarker.write( + fixture.options, + PID, + historicalTimestamp - 8_000, + historicalTimestamp - 3_000, + ) + fixture.addAnrExitInfo(timestamp = historicalTimestamp, pid = PID) + fixture.addAnrExitInfo(timestamp = newTimestamp, pid = PID + 1) + + integration.register(fixture.scopes, fixture.options) + + verify(fixture.scopes) + .captureEvent(argThat { timestamp.time == historicalTimestamp }, anyOrNull()) + verify(fixture.scopes) + .captureEvent(argThat { timestamp.time == newTimestamp }, anyOrNull()) + } + + private fun writePreviousSession(startedAt: Long): File { + val file = EnvelopeCache.getPreviousSessionFile(fixture.options.cacheDirPath!!) + val session = + Session( + Session.State.Ok, + Date(startedAt), + Date(startedAt), + 0, + "did", + SentryUUID.generateSentryId(), + true, + null, + null, + null, + null, + "env", + "rel", + null, + ) + file.bufferedWriter().use { fixture.options.serializer.serialize(session, it) } + return file + } + + private companion object { + const val PID = 1234 + } } diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index a6b474ef79..9cce7f2943 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -5029,6 +5029,7 @@ public class io/sentry/cache/EnvelopeCache : io/sentry/cache/IEnvelopeCache { public fun (Lio/sentry/SentryOptions;Ljava/lang/String;I)V public static fun create (Lio/sentry/SentryOptions;)Lio/sentry/cache/IEnvelopeCache; public fun discard (Lio/sentry/SentryEnvelope;)V + public fun endPreviousSessionAbnormally (Lio/sentry/hints/AbnormalExit;)V public fun flushPreviousSession ()V public static fun getCurrentSessionFile (Ljava/lang/String;)Ljava/io/File; public static fun getPreviousSessionFile (Ljava/lang/String;)Ljava/io/File; diff --git a/sentry/src/main/java/io/sentry/cache/EnvelopeCache.java b/sentry/src/main/java/io/sentry/cache/EnvelopeCache.java index d3f473cfbc..a91c5218ce 100644 --- a/sentry/src/main/java/io/sentry/cache/EnvelopeCache.java +++ b/sentry/src/main/java/io/sentry/cache/EnvelopeCache.java @@ -279,6 +279,15 @@ private void tryEndPreviousSession(final @NotNull Hint hint) { } } + /** + * Ends the previous session for an abnormal exit that is reported without an event, the same way + * that storing an event with this hint does. + */ + @ApiStatus.Internal + public void endPreviousSessionAbnormally(final @NotNull AbnormalExit abnormalExit) { + tryEndPreviousSession(HintUtils.createWithTypeCheckHint(abnormalExit)); + } + private void writeCrashMarkerFile() { final File crashMarkerFile = new File(options.getCacheDirPath(), CRASH_MARKER_FILE); try (final OutputStream outputStream = new FileOutputStream(crashMarkerFile)) { diff --git a/sentry/src/test/java/io/sentry/cache/EnvelopeCacheTest.kt b/sentry/src/test/java/io/sentry/cache/EnvelopeCacheTest.kt index 0cc2c96819..8c28624344 100644 --- a/sentry/src/test/java/io/sentry/cache/EnvelopeCacheTest.kt +++ b/sentry/src/test/java/io/sentry/cache/EnvelopeCacheTest.kt @@ -594,6 +594,39 @@ class EnvelopeCacheTest { assertEquals(null, updatedSession.abnormalMechanism) } + @Test + fun `endPreviousSessionAbnormally ends the session without storing an envelope`() { + val cache = fixture.getSUT() + + val previousSessionFile = EnvelopeCache.getPreviousSessionFile(fixture.options.cacheDirPath!!) + val sessionStarted = Date(2023, 10, 1) + val sessionExitedWithAbnormal = sessionStarted.time + TimeUnit.HOURS.toMillis(3) + fixture.options.serializer.serialize( + createSession(sessionStarted), + previousSessionFile.bufferedWriter(), + ) + + cache.endPreviousSessionAbnormally( + object : AbnormalExit { + override fun mechanism(): String = "abnormal_mechanism" + + override fun ignoreCurrentThread(): Boolean = false + + override fun timestamp(): Long = sessionExitedWithAbnormal + }, + ) + + val updatedSession = + fixture.options.serializer.deserialize( + previousSessionFile.bufferedReader(), + Session::class.java, + )!! + assertThat(updatedSession.status).isEqualTo(State.Abnormal) + assertThat(updatedSession.abnormalMechanism).isEqualTo("abnormal_mechanism") + assertThat(updatedSession.timestamp!!.time).isEqualTo(sessionExitedWithAbnormal) + assertThat(cache.iterator().hasNext()).isFalse() + } + @Test fun `NativeCrashExit hint marks previous session as crashed with crash timestamp`() { val cache = fixture.getSUT()