feat(anr): Profile main thread when ANR and report ANR profiles to Sentry#4899
feat(anr): Profile main thread when ANR and report ANR profiles to Sentry#4899
Conversation
|
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfileManager.java
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrCulpritIdentifier.java
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AggregatedStackTrace.java
Outdated
Show resolved
Hide resolved
Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| e59e22a | 329.74 ms | 383.31 ms | 53.57 ms |
| fc5ccaf | 270.49 ms | 363.90 ms | 93.41 ms |
| d15471f | 361.89 ms | 378.07 ms | 16.18 ms |
| d15471f | 322.58 ms | 396.08 ms | 73.50 ms |
| abfcc92 | 304.04 ms | 370.33 ms | 66.29 ms |
| 91bb874 | 310.68 ms | 359.24 ms | 48.56 ms |
| ab8a72d | 316.24 ms | 356.38 ms | 40.14 ms |
| d15471f | 286.65 ms | 314.68 ms | 28.03 ms |
| 6727e14 | 337.22 ms | 373.94 ms | 36.71 ms |
| f064536 | 329.00 ms | 395.62 ms | 66.62 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| e59e22a | 1.58 MiB | 2.20 MiB | 635.34 KiB |
| fc5ccaf | 1.58 MiB | 2.13 MiB | 557.54 KiB |
| d15471f | 1.58 MiB | 2.13 MiB | 559.54 KiB |
| d15471f | 1.58 MiB | 2.13 MiB | 559.54 KiB |
| abfcc92 | 1.58 MiB | 2.13 MiB | 557.31 KiB |
| 91bb874 | 1.58 MiB | 2.13 MiB | 559.07 KiB |
| ab8a72d | 1.58 MiB | 2.12 MiB | 551.55 KiB |
| d15471f | 1.58 MiB | 2.13 MiB | 559.54 KiB |
| 6727e14 | 1.58 MiB | 2.28 MiB | 718.64 KiB |
| f064536 | 1.58 MiB | 2.20 MiB | 633.90 KiB |
Previous results on branch: markushi/feat/anr-profiling
Startup times
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 00299fd | 359.87 ms | 424.85 ms | 64.98 ms |
| fa76e86 | 274.32 ms | 349.63 ms | 75.31 ms |
| 8c4e7d6 | 309.15 ms | 359.27 ms | 50.12 ms |
| 2cee1ab | 318.29 ms | 361.00 ms | 42.71 ms |
| 4c0ffee | 314.94 ms | 377.79 ms | 62.86 ms |
| 31581b9 | 350.00 ms | 420.63 ms | 70.63 ms |
| ddbbe91 | 289.51 ms | 359.74 ms | 70.23 ms |
| c10e603 | 367.92 ms | 393.50 ms | 25.58 ms |
| edecf31 | 310.80 ms | 367.02 ms | 56.22 ms |
| 83a9ec4 | 333.84 ms | 390.30 ms | 56.47 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 00299fd | 1.58 MiB | 2.29 MiB | 723.50 KiB |
| fa76e86 | 1.58 MiB | 2.29 MiB | 724.06 KiB |
| 8c4e7d6 | 1.58 MiB | 2.29 MiB | 727.03 KiB |
| 2cee1ab | 1.58 MiB | 2.29 MiB | 723.68 KiB |
| 4c0ffee | 1.58 MiB | 2.29 MiB | 723.67 KiB |
| 31581b9 | 1.58 MiB | 2.19 MiB | 624.94 KiB |
| ddbbe91 | 1.58 MiB | 2.29 MiB | 724.15 KiB |
| c10e603 | 1.58 MiB | 2.29 MiB | 723.72 KiB |
| edecf31 | 1.58 MiB | 2.29 MiB | 726.95 KiB |
| 83a9ec4 | 1.58 MiB | 2.29 MiB | 723.99 KiB |
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrCulpritIdentifier.java
Show resolved
Hide resolved
…ntry-java into markushi/feat/anr-profiling
sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrStackTrace.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java
Show resolved
Hide resolved
|
@sentry review |
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfileManager.java
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrCulpritIdentifier.java
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfileRotationHelper.java
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java
Show resolved
Hide resolved
romtsn
left a comment
There was a problem hiding this comment.
Great work already! I believe there are some things to address but I can check once more after that
sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoEventProcessor.java
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoEventProcessor.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfileManager.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable autofix in the Cursor dashboard.
sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoEventProcessor.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable autofix in the Cursor dashboard.
sentry-android-core/src/main/java/io/sentry/android/core/AnrIntegration.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoEventProcessor.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrCulpritIdentifier.java
Outdated
Show resolved
Hide resolved
sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoEventProcessor.java
Show resolved
Hide resolved
romtsn
left a comment
There was a problem hiding this comment.
some bugbot comments still left, but to me this looks great, hats off!
| try { | ||
| queueFile = new QueueFile.Builder(file).size(MAX_NUM_STACKTRACES).build(); | ||
| } catch (IOException e) { |
There was a problem hiding this comment.
Bug: The QueueFile capacity MAX_NUM_STACKTRACES (~121) is smaller than the number of collected stacks MAX_NUM_STACKS (151), causing the oldest stack traces to be silently dropped.
Severity: MEDIUM
Suggested Fix
Unify the constants so that the QueueFile capacity is at least as large as the maximum number of stack traces to be collected. The value for MAX_NUM_STACKTRACES should be derived from the same timeout as MAX_NUM_STACKS to ensure they are consistent.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location:
sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfileManager.java#L43-L45
Potential issue: There is a discrepancy between the number of stack traces collected and
the storage capacity for them during ANR profiling. The integration attempts to collect
`MAX_NUM_STACKS` (approximately 151) stack traces. However, the underlying `QueueFile`
is initialized with a smaller capacity of `MAX_NUM_STACKTRACES` (approximately 121).
Because `QueueFile` acts as a circular buffer, once it reaches capacity, it starts
overwriting the oldest entries. This results in the silent loss of the first ~30 stack
traces, which could contain critical information about the root cause of the ANR.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
| } catch (Throwable t) { | ||
| logger.log(SentryLevel.WARNING, "Failed to execute AnrStacktraceIntegration", t); | ||
| } | ||
| } |
There was a problem hiding this comment.
IOException from checkMainThread permanently kills polling thread
Medium Severity
The checkMainThread method declares throws IOException (from clearStacks() and addStackTrace()), but the inner try-catch in run() only catches InterruptedException. Any IOException propagates to the outer catch (Throwable t) on line 198, which logs a warning and exits the method, permanently terminating the ANR profiling thread. A single transient I/O error (disk full, file locked, etc.) silently and irrecoverably disables ANR profiling for the entire app session.


📜 Description
Adds ANR (Application Not Responding) profiling integration that profiles the main thread when an ANR is detected and reports the captured profiles to Sentry.
Key Changes:
AnrProfilingIntegrationto capture profiles during ANR eventsAnrV2Integrationnow takes care of matching and capturing the profile on the next start💡 Motivation and Context
This feature enables better ANR diagnostics by capturing profiling data at the time of ANR detection, allowing developers to identify performance bottlenecks and problematic code paths causing application hangs.
Example event: https://sentry-sdks.sentry.io/issues/7229210096/events/4598ff6fcc0f402d8ecca615005e7f64/
💚 How did you test it?
📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps