Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a19a7c8
feat(anr): Add ThreadSampleCollector
markushi Nov 20, 2026
70c1862
chore(anr): Merge non-fatal-anr-2 into non-fatal-anr-3
markushi Nov 23, 2026
79a512a
ref(anr): Make ThreadSampleCollector a pure reader
markushi Nov 23, 2026
d16e1c2
chore(anr): Merge non-fatal-anr-2 into non-fatal-anr-3
markushi Nov 23, 2026
f729109
ref(anr): Rename ThreadSampleCollector.onSample to sample
markushi Nov 23, 2026
07819d2
chore(anr): Merge non-fatal-anr-2 into non-fatal-anr-3
markushi Nov 23, 2026
01e6e97
ref(anr): Drop the interval from ThreadSampleCollector
markushi Nov 23, 2026
755ede3
chore(anr): Merge non-fatal-anr-2 into non-fatal-anr-3
markushi Nov 24, 2026
fd89a75
chore(anr): Merge non-fatal-anr-2 into non-fatal-anr-3
markushi Nov 24, 2026
0edebe2
ref(anr): Adapt to the renamed hang types
markushi Nov 24, 2026
105ceeb
chore(anr): Merge non-fatal-anr-2 into non-fatal-anr-3
markushi Nov 24, 2026
409117e
chore(anr): Merge non-fatal-anr-2 into non-fatal-anr-3
markushi Nov 24, 2026
c68ef5e
chore(anr): Merge non-fatal-anr-2 into non-fatal-anr-3
markushi Nov 24, 2026
3c54dba
chore(anr): Merge non-fatal-anr-2 into non-fatal-anr-3
markushi Nov 24, 2026
ff8ff9d
chore(anr): Merge non-fatal-anr-2 into non-fatal-anr-3
markushi Nov 24, 2026
e9fdd2a
chore(anr): Merge non-fatal-anr-2 into non-fatal-anr-3
markushi Nov 24, 2026
f9c9765
chore(anr): Merge non-fatal-anr-2 into non-fatal-anr-3
markushi Nov 25, 2026
37d659f
chore(anr): Merge non-fatal-anr-2 into non-fatal-anr-3
markushi Nov 25, 2026
6618531
chore(anr): Merge non-fatal-anr-2 into non-fatal-anr-3
markushi Nov 27, 2026
b9a8e81
chore(anr): Merge non-fatal-anr-2 into non-fatal-anr-3
markushi Nov 27, 2026
b9077f0
chore(anr): Merge non-fatal-anr-2 into non-fatal-anr-3
markushi Nov 27, 2026
813a121
ref(anr): Take the sample time of ThreadSampleCollector from the hang
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
7 changes: 7 additions & 0 deletions sentry-android-core/api/sentry-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@ public final class io/sentry/android/core/anr/AnrProfile {
}

public final class io/sentry/android/core/anr/AnrProfileManager : java/lang/AutoCloseable {
public static final field MAX_NUM_STACKTRACES I
public fun <init> (Lio/sentry/SentryOptions;)V
public fun <init> (Lio/sentry/SentryOptions;Ljava/io/File;)V
public fun add (Lio/sentry/android/core/anr/AnrStackTrace;)V
Expand Down Expand Up @@ -916,6 +917,12 @@ public abstract interface class io/sentry/android/core/hang/ThreadPoster {
public abstract fun remove (Ljava/lang/Runnable;)V
}

public final class io/sentry/android/core/hang/ThreadSampleCollector : io/sentry/android/core/hang/HangDataCollector {
public fun <init> (Ljava/lang/Thread;)V
public fun sample (Lio/sentry/time/Timestamp;)Lio/sentry/android/core/anr/AnrStackTrace;
public synthetic fun sample (Lio/sentry/time/Timestamp;)Ljava/lang/Object;
}

public abstract class io/sentry/android/core/performance/ActivityLifecycleCallbacksAdapter : android/app/Application$ActivityLifecycleCallbacks {
public fun <init> ()V
public fun onActivityCreated (Landroid/app/Activity;Landroid/os/Bundle;)V
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package io.sentry.android.core.anr;

import static io.sentry.SentryLevel.ERROR;
import static io.sentry.android.core.anr.AnrProfilingIntegration.POLLING_INTERVAL_MS;
import static io.sentry.android.core.anr.AnrProfilingIntegration.THRESHOLD_ANR_MS;

import io.sentry.ILogger;
import io.sentry.SentryOptions;
import io.sentry.android.core.hang.HangManager;
import io.sentry.cache.tape.ObjectQueue;
import io.sentry.cache.tape.QueueFile;
import io.sentry.util.Objects;
Expand All @@ -22,8 +21,12 @@
@ApiStatus.Internal
public final class AnrProfileManager implements AutoCloseable {

private static final int MAX_NUM_STACKTRACES =
(int) ((THRESHOLD_ANR_MS / POLLING_INTERVAL_MS) * 2);
/**
* One sample per {@link HangManager#DEFAULT_POLLING_INTERVAL_MS} tick for {@link
* HangManager#DEFAULT_MAX_RECORDING_TIME_MS}. The hang manager stops sampling a hang at this
* size, because a full file drops its oldest entry.
*/
public static final int MAX_NUM_STACKTRACES = 120;

@NotNull private final ObjectQueue<AnrStackTrace> queue;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.sentry.android.core.hang;

import static java.util.concurrent.TimeUnit.NANOSECONDS;

import io.sentry.android.core.anr.AnrStackTrace;
import io.sentry.time.Timestamp;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

/** Samples the stack and the state of a thread on each sampling tick of a recorded hang. */
@ApiStatus.Internal
public final class ThreadSampleCollector implements HangDataCollector<AnrStackTrace> {

private final @NotNull Thread thread;

public ThreadSampleCollector(final @NotNull Thread thread) {
this.thread = thread;
}

@Override
public @NotNull AnrStackTrace sample(final @NotNull Timestamp timestamp) {
return new AnrStackTrace(
NANOSECONDS.toMillis(timestamp.epochNanos()), thread.getStackTrace(), thread.getState());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.sentry.android.core.anr.AnrStackTrace
import io.sentry.android.core.hang.HangManager.DEFAULT_MAX_RECORDING_TIME_MS
import io.sentry.android.core.hang.HangManager.DEFAULT_POLLING_INTERVAL_MS
import io.sentry.test.DeferredExecutorService
import io.sentry.test.ImmediateExecutorService
import io.sentry.test.NonOverridableNoOpSentryExecutorService
import io.sentry.time.EpochClock
import io.sentry.time.JavaMonotonicTicker
Expand All @@ -21,6 +22,7 @@ import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeUnit.HOURS
import java.util.concurrent.TimeUnit.MILLISECONDS
import java.util.concurrent.TimeUnit.NANOSECONDS
import java.util.concurrent.locks.LockSupport
import kotlin.test.BeforeTest
import kotlin.test.Test
import org.mockito.kotlin.any
Expand Down Expand Up @@ -1121,20 +1123,31 @@ class HangManagerTest {
}

@Test
fun `manager on the default executor posts the probe of a thread signal`() {
val posted = CountDownLatch(1)
fun `manager on the default executor records the stack of the watched thread`() {
val parked = Thread { while (!Thread.currentThread().isInterrupted) LockSupport.park() }
parked.isDaemon = true
parked.start()
var posts = 0
val poster =
object : ThreadPoster {
override fun post(runnable: Runnable) = posted.countDown()
// The probe runs only at the 10th tick, so the thread looks blocked for about 600 ms.
override fun post(runnable: Runnable) {
if (++posts == 10) {
runnable.run()
}
}

override fun remove(runnable: Runnable) = Unit
}
val ticker = JavaMonotonicTicker.getInstance()
val recorded = CountDownLatch(1)
var record: HangRecord? = null
appStateProvider.foreground()
val created =
HangManager(
options,
SentryOptions().apply { executorService = ImmediateExecutorService() },
appStateProvider,
JavaMonotonicTicker.getInstance(),
ticker,
epochClock,
DEFAULT_POLLING_INTERVAL_MS,
DEFAULT_MAX_RECORDING_TIME_MS,
Expand All @@ -1150,16 +1163,36 @@ class HangManagerTest {
),
)
},
emptyList(),
listOf(
HangCollectorSetup(
ThreadSampleCollector(parked),
{ stackTrace, builder -> builder.addStackTrace(stackTrace) },
null,
),
),
)

created.subscribe(listener)
val recordingListener =
object : HangRecordingListener {
override fun onHangStarted() = HangRecordingRequest(true, false)

override fun onHangRecorded(hangRecord: HangRecord) {
record = hangRecord
recorded.countDown()
}
}

created.subscribe(recordingListener)

try {
assertThat(posted.await(10, TimeUnit.SECONDS)).isTrue()
assertThat(recorded.await(10, TimeUnit.SECONDS)).isTrue()
} finally {
created.unsubscribe(listener)
created.unsubscribe(recordingListener)
parked.interrupt()
parked.join()
}
assertThat(record!!.stackTraces).isNotEmpty()
assertThat(record!!.stackTraces.first().stack.map { it.methodName }).contains("park")
}

private class FakeAppStateProvider : AppStateProvider {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.sentry.android.core.hang

import com.google.common.truth.Truth.assertThat
import io.sentry.time.Timestamp
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeUnit.MILLISECONDS
import java.util.concurrent.locks.LockSupport
import kotlin.test.Test

class ThreadSampleCollectorTest {

private val unixTimeMs = 1_700_000_000_000L
private val timestamp = Timestamp.ofEpochNanos(MILLISECONDS.toNanos(unixTimeMs))

@Test
fun `sample returns the stack and the state of the thread at the given time`() {
val parked = Thread { while (!Thread.currentThread().isInterrupted) LockSupport.park() }
parked.isDaemon = true
parked.start()
try {
val deadlineNanos = System.nanoTime() + TimeUnit.SECONDS.toNanos(10)
while (parked.state != Thread.State.WAITING && System.nanoTime() < deadlineNanos) {
Thread.yield()
}
assertThat(parked.state).isEqualTo(Thread.State.WAITING)
val collector = ThreadSampleCollector(parked)

val sample = collector.sample(timestamp)

assertThat(sample.state).isEqualTo(Thread.State.WAITING)
assertThat(sample.timestampMs).isEqualTo(unixTimeMs)
assertThat(sample.stack.map { it.methodName }).contains("park")
} finally {
parked.interrupt()
parked.join()
}
}
}
Loading