Skip to content

feat(react-native): detect and report Android ANRs - #391

Merged
KishanPRao merged 14 commits into
mainfrom
fix/rn-anr-android
Aug 28, 2026
Merged

feat(react-native): detect and report Android ANRs#391
KishanPRao merged 14 commits into
mainfrom
fix/rn-anr-android

Conversation

@KishanPRao

@KishanPRao KishanPRao commented Aug 26, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds Android ANR detection with two modes, a threshold watchdog that reports mid-hang and ApplicationExitInfo records reported on the next launch, both carrying every thread's stack.

Changes

packages/react-native/src

  • BacktraceConfiguration.ts: adds the BacktraceAnrType enum and anr configuration options.
  • BacktraceClient.ts: starts ANR reporting on initialize, picks watchdog or exit-info by type, disposes the handler.
  • anr/AnrWatchdogHandler.ts: turns watchdog events into reports with main as the faulting thread and every other thread included, pauses the watchdog while backgrounded.
  • anr/AnrReporter.ts: reads exit-info records on launch, sends oldest first, saves the last-timestamp cursor when a report is delivered or stored in the database for retry, attaches the OS dump.
  • anr/AnrThreads.ts: attaches the non-main threads to a report, renaming duplicate thread names.
  • anr/AnrException.ts: error named Hang for classification.
  • index.ts: exports the new enum and config type.

packages/react-native/android

  • BacktraceAnrWatchdog.java: native module, starts/stops the watchdog, emits detection events with frames for every thread.
  • BacktraceReactNative.java: adds getAnrExitInfo, logs read failures at warn.
  • ReactNativePackage.java: registers the watchdog module.
  • AnrExitInfoReader.java: collects REASON_ANR records, returns them oldest first with attributes, message, raw trace, and parsed frames for the main thread and every other thread in the dump.
  • anr/AnrWatchdog.java: watchdog thread, posts a ticker to the main looper, reports when the tick is missed past the timeout, one report per hang, optional debugger bail-out.
  • anr/BacktraceThreadWatcher.java: tick counters the watchdog compares.
  • anr/ExitInfo.java: ported from backtrace-android.
  • anr/ExitInfoAdapter.java: ported from backtrace-android.
  • anr/ProcessExitInfoProvider.java: ported from backtrace-android.
  • anr/ActivityManagerExitInfoProvider.java: exit-record lookup, fetches all records instead of a fixed count.
  • anr/ExitInfoStackTraceParser.java: parses the OS ANR dump into per-thread frames, keeps native-method, unknown-source, and fileless frames, null-guards unnamed threads.
  • anr/AppExitInfoDetailsExtractor.java: extracts the ANR attributes and OS trace, attribute names match backtrace-android.
  • anr/StackFrameMapper.java: maps stack traces to bridge frames and formatted text, keeps the native program counter as address.
  • build.gradle: adds junit for unit tests.

Tests

  • tests/anrReporterTests.spec.ts: covers ordering, cursor advancement, database-stored failures, attributes, attachment.
  • tests/anrWatchdogHandlerTests.spec.ts: covers event handling, thread-name dedup, background pause, dispose.
  • android/src/test/ExitInfoStackTraceParserTest.java: parser tests over a real ANR dump, covering the fileless and duplicate-name forms.
  • android/src/test/resources/anrAppExitInfoStacktrace.txt: the dump fixture.

Docs and CI

  • README.md: ANR Detection section.
  • .github/workflows/android.yml: runs the SDK unit tests before the emulator build.

examples/sdk/reactNative

  • index.js: enables ANR in the example.
  • metro.config.js: pins a single react-native copy, a duplicate splits the JS event registry from the native one.
  • ErrorGenerator.java: adds blockMainThread to trigger a real ANR.
  • src/actions/android/action.ts: the ANR action blocks the main thread instead of sleeping async.

Note

A threshold report can be lost if the OS kills the app before the hang clears; the applicationExit type survives the kill.

ref: BT-7451

@KishanPRao
KishanPRao requested a review from melekr August 26, 2026 13:50
@KishanPRao KishanPRao self-assigned this Aug 26, 2026
@KishanPRao KishanPRao added the enhancement New feature or request label Aug 26, 2026

@melekr melekr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🚀

Comment thread packages/react-native/src/anr/AnrReporter.ts
Comment thread packages/react-native/src/anr/AnrReporter.ts
@KishanPRao
KishanPRao marked this pull request as ready for review August 28, 2026 15:45
@KishanPRao
KishanPRao merged commit 5502f0c into main Aug 28, 2026
12 checks passed
@KishanPRao
KishanPRao deleted the fix/rn-anr-android branch August 28, 2026 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants