Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sentry-android-core/api/sentry-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ public final class io/sentry/android/core/anr/AnrStackTrace : java/lang/Comparab
public final class io/sentry/android/core/anr/NonFatalAnrMarker {
public static final field FILE_NAME Ljava/lang/String;
public static fun delete (Lio/sentry/SentryOptions;)V
public fun getPid ()I
public fun matches (IJ)Z
public static fun read (Lio/sentry/SentryOptions;)Lio/sentry/android/core/anr/NonFatalAnrMarker;
public static fun write (Lio/sentry/SentryOptions;IJJ)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.app.ActivityManager;
import android.app.ApplicationExitInfo;
import android.content.Context;
import android.os.Process;
import io.sentry.Attachment;
import io.sentry.DateUtils;
import io.sentry.Hint;
Expand All @@ -15,10 +16,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;
Expand Down Expand Up @@ -72,12 +76,20 @@ 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));
options
.getExecutorService()
.submit(
new ApplicationExitInfoHistoryDispatcher(
context, scopes, this.options, new AnrV2Policy(this.options)));
.submit(() -> {
try {
dispatcher.run();
} finally {
deletePreviousProcessMarker(androidOptions);
}
});
} catch (Throwable e) {
options.getLogger().log(SentryLevel.DEBUG, "Failed to start ANR processor.", e);
}
Expand All @@ -93,6 +105,20 @@ public void close() throws IOException {
}
}

/**
* Deletes the marker of the previous process. A marker of this process stays, so a second init in
* this process does not delete it.
*/
private static void deletePreviousProcessMarker(final @NotNull SentryAndroidOptions options) {
final @Nullable NonFatalAnrMarker marker = NonFatalAnrMarker.read(options);
// A new process gets the pid of the previous one only after the pid counter went around all of
// pid_max, which is highly unlikely between an ANR kill and the next launch.
if (marker != null && marker.getPid() == Process.myPid()) {
return;
}
NonFatalAnrMarker.delete(options);
}

private static final class AnrV2Policy
implements ApplicationExitInfoHistoryDispatcher.ApplicationExitInfoPolicy {

Expand Down Expand Up @@ -133,6 +159,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) {
Expand All @@ -144,13 +189,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);

Expand Down Expand Up @@ -185,6 +223,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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ private NonFatalAnrMarker(final int pid, final long startTimestampMs, final long
this.endTimestampMs = endTimestampMs;
}

/** The pid of the process that wrote this marker. */
public int getPid() {
return pid;
}

/** Whether an exit of the process {@code pid} at {@code exitTimestampMs} belongs to this hang. */
public boolean matches(final int pid, final long exitTimestampMs) {
return this.pid == pid
Expand Down
Loading
Loading