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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixes

- Skip ANR profiling while a debugger is connected ([#6389](https://github.com/getsentry/sentry-java/pull/6389))

### Dependencies

- Bump Native SDK from v0.17.2 to v0.17.3 ([#6250](https://github.com/getsentry/sentry-java/pull/6250))
Expand Down
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 @@ -736,6 +736,7 @@ public class io/sentry/android/core/anr/AnrProfilingIntegration : io/sentry/Inte
public fun close ()V
protected fun getProfileManager ()Lio/sentry/android/core/anr/AnrProfileManager;
protected fun getState ()Lio/sentry/android/core/anr/AnrProfilingIntegration$MainThreadState;
protected fun isDebuggerConnected ()Z
public fun onBackground ()V
public fun onForeground ()V
public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion;

import android.os.Debug;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
Expand Down Expand Up @@ -211,6 +212,15 @@ public void run() {

@ApiStatus.Internal
protected void checkMainThread(final @NotNull Thread mainThread) throws IOException {
if (isDebuggerConnected()) {
// The main thread can sit at a breakpoint for a long time, which is not an ANR. Reset the
// timestamp, so the paused time does not count against us after the debugger detaches.
mainThreadState = MainThreadState.IDLE;
sampled = false;
updater.run();
return;
}

final long now = SystemClock.uptimeMillis();
final long diff = now - lastMainThreadExecutionTime;

Expand Down Expand Up @@ -268,6 +278,11 @@ protected void checkMainThread(final @NotNull Thread mainThread) throws IOExcept
}
}

@ApiStatus.Internal
protected boolean isDebuggerConnected() {
return Debug.isDebuggerConnected();
}

@TestOnly
@NotNull
protected MainThreadState getState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,37 @@ class AnrProfilingIntegrationTest {
assertEquals(AnrProfilingIntegration.MAX_NUM_STACKS, integration.numCollectedStacks.get())
}

@Test
fun `debugger pauses are treated as idle`() {
val mainThread = Thread.currentThread()
SystemClock.setCurrentTimeMillis(1_000)

val androidOptions =
SentryAndroidOptions().apply {
cacheDirPath = tmpDir.root.absolutePath
setLogger(mockLogger)
anrProfilingSampleRate = 1.0
}

var debuggerConnected = true
val integration =
object : AnrProfilingIntegration() {
override fun isDebuggerConnected(): Boolean = debuggerConnected
}
integration.register(mockScopes, androidOptions)

SystemClock.setCurrentTimeMillis(20_000)
integration.checkMainThread(mainThread)
assertEquals(AnrProfilingIntegration.MainThreadState.IDLE, integration.state)
assertEquals(0, integration.numCollectedStacks.get())

// the time spent paused must not count towards an ANR once the debugger detaches
debuggerConnected = false
SystemClock.setCurrentTimeMillis(20_100)
integration.checkMainThread(mainThread)
assertEquals(AnrProfilingIntegration.MainThreadState.IDLE, integration.state)
}

@Test
fun `background foreground transitions don't trigger an ANR`() {
val mainThread = Thread.currentThread()
Expand Down