Skip to content

fix(anr): extend main thread detection from thread dump#5733

Open
markushi wants to merge 9 commits into
mainfrom
fix/anrv2-main-thread-process-name
Open

fix(anr): extend main thread detection from thread dump#5733
markushi wants to merge 9 commits into
mainfrom
fix/anrv2-main-thread-process-name

Conversation

@markushi

@markushi markushi commented Jul 7, 2026

Copy link
Copy Markdown
Member

📜 Description

Not every main thread has a name="main" set, so we ended up not correctly identifying it (on some Android versions/OEMs, native threads, etc...).

To solve this we now rely on a common Linux/Android convention: The main thread kernel id (sysTid) is the same as the process id (pid). For managed Java threads, a fallback for the "main" thread name remains.

Changes

  • Parses the process id from the thread dump header (----- pid <pid> at ... -----) and marks the thread whose sysTid equals it as the main thread.
  • The main thread's name is normalized back to "main" so downstream consumers see a consistent name even when the OS renamed it.
  • Threads without a stacktrace are skipped, as they are not actionable.

No public API change.

💡 Motivation and Context

When the OS names the main thread after the process, ANRs were not attributed to the main thread and were reported without a stack trace. Matching on sysTid == pid is robust and does not depend on the (truncatable) process name.

Fixes JAVA-635
Fixes #5731

💚 How did you test it?

  • Added tests to ThreadDumpParserTest
  • Updated existing 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

--

View Junior Session in Sentry

On some Android versions/OEMs, AppExitInfo's thread dump names the
main thread after the process name (e.g. 'com.example.app') instead
of 'main'. ThreadDumpParser previously only matched on 'main',
causing the thread to not be flagged as main/crashed and resulting
in ANR events with no usable stack trace.

Pass the process name from ApplicationExitInfo.getProcessName() to
ThreadDumpParser and treat a thread as main if its name equals
'main' OR the process name.

Fixes JAVA-635

Co-Authored-By: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
@linear-code

linear-code Bot commented Jul 7, 2026

Copy link
Copy Markdown

JAVA-635

@sentry

sentry Bot commented Jul 7, 2026

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.47.0 (1) release

⚙️ sentry-android Build Distribution Settings

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 466.78 ms 513.84 ms 47.06 ms
Size 0 B 0 B 0 B

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
d364ace 382.77 ms 443.21 ms 60.44 ms
4c04bb8 333.16 ms 408.16 ms 75.00 ms
bb0ff41 344.70 ms 413.82 ms 69.12 ms
91bb874 314.47 ms 440.00 ms 125.53 ms
bdbe1f4 380.66 ms 464.44 ms 83.78 ms
8687935 294.00 ms 304.02 ms 10.02 ms
eb95ded 317.51 ms 369.08 ms 51.57 ms
b936425 302.69 ms 372.86 ms 70.17 ms
d15471f 342.08 ms 415.44 ms 73.35 ms
5b66efd 308.67 ms 363.85 ms 55.18 ms

App size

Revision Plain With Sentry Diff
d364ace 1.58 MiB 2.11 MiB 539.75 KiB
4c04bb8 0 B 0 B 0 B
bb0ff41 0 B 0 B 0 B
91bb874 1.58 MiB 2.13 MiB 559.07 KiB
bdbe1f4 1.58 MiB 2.11 MiB 538.88 KiB
8687935 1.58 MiB 2.19 MiB 619.17 KiB
eb95ded 0 B 0 B 0 B
b936425 0 B 0 B 0 B
d15471f 1.58 MiB 2.13 MiB 559.54 KiB
5b66efd 1.58 MiB 2.13 MiB 559.07 KiB

Previous results on branch: fix/anrv2-main-thread-process-name

Startup times

Revision Plain With Sentry Diff
14ba103 320.87 ms 362.88 ms 42.01 ms
420f794 314.27 ms 377.56 ms 63.29 ms
b6ecc1e 311.13 ms 366.86 ms 55.73 ms
2a331f8 341.76 ms 424.69 ms 82.93 ms

App size

Revision Plain With Sentry Diff
14ba103 0 B 0 B 0 B
420f794 0 B 0 B 0 B
b6ecc1e 0 B 0 B 0 B
2a331f8 0 B 0 B 0 B

markushi and others added 3 commits July 7, 2026 12:17
The main thread can be renamed by the OS to the (potentially truncated) process
name, so relying on the process name is fragile. On Linux/Android the main
thread's kernel thread id (sysTid) always equals the process id, so use that as
the authoritative signal instead.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@romtsn romtsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this looks good! shall we also address the second part which is not to attach a thread if its stacktrace has no frames? Could be addressed in a separate PR ofc

@markushi markushi changed the title fix(anr): detect main thread when OS names it after the process fix(anr): extend main thread detection from thread dump Jul 7, 2026
markushi and others added 3 commits July 7, 2026 14:45
…trace

Normalize the main thread's name back to "main" once it is identified (the OS
may have renamed it to the process name), and drop threads that have no
stacktrace as they are not actionable.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Identify the main thread by matching the thread whose kernel thread id (sysTid)
equals the process id, falling back to the conventional "main" thread name, and
skip threads that have no stacktrace as they are not actionable.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@markushi markushi marked this pull request as ready for review July 7, 2026 14:08
@markushi markushi requested a review from romtsn July 7, 2026 14:08
Comment thread CHANGELOG.md
Co-authored-by: Roman Zavarnitsyn <rom4ek93@gmail.com>
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.

ANRv2 (AppExitInfo): main thread not detected when the OS thread dump names it after the process → ANR reported with no stack trace

3 participants