Skip to content

Improve ANR detector runnable#3192

Merged
0xnm merged 1 commit intodevelopfrom
nogorodnikov/improve-anr-detector-runnable
Feb 23, 2026
Merged

Improve ANR detector runnable#3192
0xnm merged 1 commit intodevelopfrom
nogorodnikov/improve-anr-detector-runnable

Conversation

@0xnm
Copy link
Member

@0xnm 0xnm commented Feb 19, 2026

What does this PR do?

This PR changes a bit ANR detection mechanism to move away from the shared (between main thread and detector thread) synchronization object to the CountDownLatch.

This should reduce the execution cost of the detector callback from the main thread by not waiting to acquire the lock if detector thread is performing long operation when ANR is registered (addError may be a long waiting call if context queue is full and blocks till ready to accept new item).

Here is the benchmark results in case if getting thread info (or reporting to RUM monitor) takes 5ms. It compares the detector callback execution from the main thread between new implementation vs previous one.

ANR threshold is set to 1s, ANR test blocks thread for 1.05s, each scenario was executed 100 times with 20 warm-up iterations before.

Scenario Metric Current Original Original / Current
No-ANR 5ms mean 19.886 us 19.802 us 1.00x
No-ANR 5ms p50 16.833 us 17.083 us 1.01x
No-ANR 5ms p95 33.958 us 32.333 us 0.95x
No-ANR 5ms p99 80.125 us 43.500 us 0.54x
No-ANR 500ms mean 23.262 us 25.879 us 1.11x
No-ANR 500ms p50 17.917 us 19.417 us 1.08x
No-ANR 500ms p95 54.667 us 55.542 us 1.02x
No-ANR 500ms p99 76.625 us 135.250 us 1.76x
ANR mean 14.681 us 13.228 us 0.90x
ANR p50 11.458 us 12.125 us 1.06x
ANR p95 30.416 us 24.584 us 0.81x
ANR p99 59.167 us 25.666 us 0.43x

Here are results if getting thread info (or reporting to RUM monitor) takes 500 ms instead.

Scenario Metric Current Original Original / Current
No-ANR 5ms mean 13.338 us 16.664 us 1.25x
No-ANR 5ms p50 12.542 us 12.500 us 1.00x
No-ANR 5ms p95 26.916 us 28.208 us 1.05x
No-ANR 5ms p99 37.917 us 31.500 us 0.83x
No-ANR 500ms mean 51.588 us 62.442 us 1.21x
No-ANR 500ms p50 43.542 us 25.584 us 0.59x
No-ANR 500ms p95 126.750 us 74.250 us 0.59x
No-ANR 500ms p99 363.750 us 560.000 us 1.54x
ANR mean 8.787 us 457129.942 us 52021.90x
ANR p50 6.125 us 456356.792 us 74507.23x
ANR p95 15.917 us 465837.667 us 29266.68x
ANR p99 17.666 us 468173.666 us 26500.32x

It can be seen, that with the original approach trying to acquire the monitor when detector has long reporting routine comes with long wait as well, which is not there for the new approach.

Other results are mixed, but given the overall time (dozens of microseconds) it is okay.

100-400 us timings in the table above are probably outliers. Detector runnable just calls countDown(), and time should be more or less in the same ballpark.

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Make sure you discussed the feature or bugfix with the maintaining team in an Issue
  • Make sure each commit and the PR mention the Issue number (cf the CONTRIBUTING doc)

@0xnm 0xnm marked this pull request as ready for review February 19, 2026 16:01
@0xnm 0xnm requested a review from a team as a code owner February 19, 2026 16:01
@datadog-official

This comment has been minimized.

@codecov-commenter
Copy link

codecov-commenter commented Feb 19, 2026

Codecov Report

❌ Patch coverage is 70.58824% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.25%. Comparing base (da85551) to head (d5cc76c).
⚠️ Report is 2 commits behind head on develop.

Files with missing lines Patch % Lines
...og/android/rum/internal/anr/ANRDetectorRunnable.kt 70.59% 14 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #3192      +/-   ##
===========================================
- Coverage    71.31%   71.25%   -0.06%     
===========================================
  Files          929      929              
  Lines        34442    34457      +15     
  Branches      5813     5813              
===========================================
- Hits         24562    24551      -11     
- Misses        8247     8263      +16     
- Partials      1633     1643      +10     
Files with missing lines Coverage Δ
...og/android/rum/internal/anr/ANRDetectorRunnable.kt 67.61% <70.59%> (-14.54%) ⬇️

... and 41 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@0xnm 0xnm force-pushed the nogorodnikov/improve-anr-detector-runnable branch from 2880b77 to 2a301ad Compare February 20, 2026 08:02
hamorillo
hamorillo previously approved these changes Feb 20, 2026
Copy link
Contributor

@hamorillo hamorillo left a comment

Choose a reason for hiding this comment

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

🚀

shouldStop = true
}

private fun reportAnr() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice refactor! It looks much cleaner now!

@0xnm 0xnm force-pushed the nogorodnikov/improve-anr-detector-runnable branch from 2a301ad to d5cc76c Compare February 20, 2026 13:21
@0xnm 0xnm requested a review from hamorillo February 20, 2026 13:21
@0xnm 0xnm merged commit 4389648 into develop Feb 23, 2026
26 checks passed
@0xnm 0xnm deleted the nogorodnikov/improve-anr-detector-runnable branch February 23, 2026 16:16
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.

4 participants