Skip to content

feat(anr): Profile main thread when ANR and report ANR profiles to Sentry#4899

Open
markushi wants to merge 40 commits intomainfrom
markushi/feat/anr-profiling
Open

feat(anr): Profile main thread when ANR and report ANR profiles to Sentry#4899
markushi wants to merge 40 commits intomainfrom
markushi/feat/anr-profiling

Conversation

@markushi
Copy link
Member

@markushi markushi commented Nov 12, 2025

📜 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:

  • New AnrProfilingIntegration to capture profiles during ANR events
  • AnrV2Integration now takes care of matching and capturing the profile on the next start
  • If the captured ANR event only contains system frames, a static fingerprint will get set, effectively changing the grouping behavior to group all noisy ANRs into a single issue

💡 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?

  • Added tests

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

@github-actions
Copy link
Contributor

github-actions bot commented Nov 12, 2025

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 5ae3e11

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions
Copy link
Contributor

github-actions bot commented Nov 12, 2025

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 304.55 ms 377.88 ms 73.33 ms
Size 1.58 MiB 2.29 MiB 727.01 KiB

Baseline results on branch: main

Startup times

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

@markushi markushi marked this pull request as draft December 3, 2025 07:19
@markushi
Copy link
Member Author

@sentry review

@markushi markushi marked this pull request as ready for review December 17, 2025 09:51
Copy link
Member

@romtsn romtsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work already! I believe there are some things to address but I can check once more after that

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@markushi markushi requested a review from romtsn February 26, 2026 20:02
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

@romtsn romtsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some bugbot comments still left, but to me this looks great, hats off!

Comment on lines +43 to +45
try {
queueFile = new QueueFile.Builder(file).size(MAX_NUM_STACKTRACES).build();
} catch (IOException e) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Additional Locations (1)

Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants