Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6d03de3
feat(anr): Add ThreadHangSignal
markushi Nov 20, 2026
16f09dc
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi Nov 23, 2026
7bd435c
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi Nov 23, 2026
2717cf8
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi Nov 23, 2026
f68fc3a
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi Nov 24, 2026
0e8b2c7
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi Nov 24, 2026
81e5f0c
ref(anr): Adapt to the renamed hang types
markushi Nov 24, 2026
0a96f43
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi Nov 24, 2026
1a11455
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi Nov 24, 2026
1cd4179
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi Nov 24, 2026
fb26345
ref(anr): Read the probe time from the metronome
markushi Nov 24, 2026
febf121
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi Nov 24, 2026
9febe3e
ref(anr): Unsubscribe instead of closing the hang manager
markushi Nov 24, 2026
e2e2da8
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi Nov 24, 2026
2ab7198
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi Nov 24, 2026
fc143d6
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi Nov 25, 2026
f5f6458
style(anr): Format the HangManager constants
markushi Nov 25, 2026
47ab474
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi Nov 27, 2026
8f6f5c0
fix(anr): Do not read an unset probe time as a probe run
markushi Nov 27, 2026
f3878a8
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi Nov 27, 2026
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
9 changes: 9 additions & 0 deletions sentry-android-core/api/sentry-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ public final class io/sentry/android/core/hang/HangManager : io/sentry/android/c
public static final field DEFAULT_EXECUTOR_FACTORY Lio/sentry/android/core/hang/HangManager$ExecutorFactory;
public static final field DEFAULT_MAX_RECORDING_TIME_MS J
public static final field DEFAULT_POLLING_INTERVAL_MS J
public static final field DEFAULT_SUSPICIOUS_THRESHOLD_MS J
public fun <init> (Lio/sentry/SentryOptions;Lio/sentry/android/core/AppStateProvider;Lio/sentry/time/MonotonicTicker;Lio/sentry/time/EpochClock;JJILio/sentry/android/core/hang/HangManager$ExecutorFactory;Lio/sentry/android/core/hang/HangManager$SignalFactory;Ljava/util/List;)V
public fun addTickListener (Lio/sentry/android/core/hang/Metronome$TickListener;)V
public fun nowMs ()J
Expand Down Expand Up @@ -902,6 +903,14 @@ public abstract interface class io/sentry/android/core/hang/Metronome$TickListen
public abstract fun onMetronomeTick (J)V
}

public final class io/sentry/android/core/hang/ThreadHangSignal : io/sentry/android/core/hang/HangSignal, io/sentry/android/core/hang/Metronome$TickListener {
public fun <init> (Lio/sentry/android/core/hang/Metronome;Lio/sentry/IDebugManager;Lio/sentry/android/core/hang/ThreadPoster;J)V
public fun close ()V
public fun onMetronomeTick (J)V
public fun start (Lio/sentry/android/core/hang/HangSignalListener;)V
public fun stop ()V
}

public abstract interface class io/sentry/android/core/hang/ThreadPoster {
public abstract fun post (Ljava/lang/Runnable;)V
public abstract fun remove (Ljava/lang/Runnable;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public final class HangManager implements Metronome, AppState.AppStateListener {
/** How long a hang is sampled at most. The hang itself can last longer. */
public static final long DEFAULT_MAX_RECORDING_TIME_MS = 8_000;

public static final long DEFAULT_SUSPICIOUS_THRESHOLD_MS = 300;

/**
* Creates the executor of the manager thread. It must run one task at a time on one thread, in
* the order they were queued.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package io.sentry.android.core.hang;

import io.sentry.IDebugManager;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Reports {@link HangSignal.State#SUSPICIOUS} when a probe posted to the watched thread did not run
* for more than the suspicious threshold, and {@link HangSignal.State#IDLE} when it ran. The
* watched thread is the thread that the {@link ThreadPoster} posts to.
*
* <p>Only the probe runs on the watched thread. Everything else runs on the thread that calls the
* ticks of the {@link Metronome}. All times come from {@link Metronome#nowMs()}.
*
* <p>A hang starts at the last run of the probe. The watched thread can block at any time up to one
* tick after that run, so the duration of a hang can be up to one polling interval too long.
*/
@ApiStatus.Internal
public final class ThreadHangSignal implements HangSignal, Metronome.TickListener {

private final @NotNull Metronome metronome;
private final @NotNull IDebugManager debugManager;
private final @NotNull ThreadPoster poster;
private final long suspiciousThresholdMs;

// The origin of Metronome.nowMs() is arbitrary, so 0 cannot mean "never ran".
private volatile long probeRunMs = Long.MIN_VALUE;

private final @NotNull Runnable probe;

private @Nullable HangSignalListener listener;
private long probeFloorMs;
private boolean suspicious;
private long suspiciousSinceMs;

public ThreadHangSignal(
final @NotNull Metronome metronome,
final @NotNull IDebugManager debugManager,
final @NotNull ThreadPoster poster,
final long suspiciousThresholdMs) {
this.metronome = metronome;
this.debugManager = debugManager;
this.poster = poster;
this.suspiciousThresholdMs = suspiciousThresholdMs;
this.probe = () -> probeRunMs = metronome.nowMs();
}

@Override
public void start(final @NotNull HangSignalListener listener) {
this.listener = listener;
suspicious = false;
// The probe may not have run since the last stop, so its time would read as a hang.
probeFloorMs = metronome.nowMs();
metronome.addTickListener(this);
}

@Override
public void stop() {
metronome.removeTickListener(this);
poster.remove(probe);
listener = null;
suspicious = false;
}

@Override
public void close() {
stop();
}

@Override
public void onMetronomeTick(final long nowMs) {
check(nowMs);
poster.remove(probe);
poster.post(probe);
}

private void check(final long nowMs) {
final @Nullable HangSignalListener currentListener = listener;
if (currentListener == null) {
return;
}

// A breakpoint is not a hang.
if (debugManager.isDebuggerConnected()) {
probeFloorMs = nowMs;
if (suspicious) {
suspicious = false;
currentListener.onSignalCanceled(this);
}
return;
}

final long lastProbeMs = Math.max(probeRunMs, probeFloorMs);
Comment thread
markushi marked this conversation as resolved.
Resolved
if (suspicious) {
if (lastProbeMs > suspiciousSinceMs) {
suspicious = false;
currentListener.onSignalChanged(this, State.IDLE, nowMs - lastProbeMs);
}
} else if (nowMs - lastProbeMs > suspiciousThresholdMs) {
suspicious = true;
suspiciousSinceMs = lastProbeMs;
currentListener.onSignalChanged(this, State.SUSPICIOUS, nowMs - lastProbeMs);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import io.sentry.android.core.hang.HangManager.DEFAULT_POLLING_INTERVAL_MS
import io.sentry.test.DeferredExecutorService
import io.sentry.test.NonOverridableNoOpSentryExecutorService
import io.sentry.time.EpochClock
import io.sentry.time.JavaMonotonicTicker
import io.sentry.time.TestMonotonicTicker
import io.sentry.time.Timestamp
import java.io.IOException
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeUnit.HOURS
import java.util.concurrent.TimeUnit.MILLISECONDS
Expand Down Expand Up @@ -1118,6 +1120,48 @@ class HangManagerTest {
assertThat(signal.closeCount).isEqualTo(0)
}

@Test
fun `manager on the default executor posts the probe of a thread signal`() {
val posted = CountDownLatch(1)
val poster =
object : ThreadPoster {
override fun post(runnable: Runnable) = posted.countDown()

override fun remove(runnable: Runnable) = Unit
}
appStateProvider.foreground()
val created =
HangManager(
options,
appStateProvider,
JavaMonotonicTicker.getInstance(),
epochClock,
DEFAULT_POLLING_INTERVAL_MS,
DEFAULT_MAX_RECORDING_TIME_MS,
MAX_SAMPLES,
HangManager.DEFAULT_EXECUTOR_FACTORY,
{ metronome ->
listOf(
ThreadHangSignal(
metronome,
{ false },
poster,
HangManager.DEFAULT_SUSPICIOUS_THRESHOLD_MS,
),
)
},
emptyList(),
)

created.subscribe(listener)

try {
assertThat(posted.await(10, TimeUnit.SECONDS)).isTrue()
} finally {
created.unsubscribe(listener)
}
}

private class FakeAppStateProvider : AppStateProvider {
val listeners = mutableListOf<AppState.AppStateListener>()
private var inBackground: Boolean? = null
Expand Down
Loading
Loading