fix(anr): extend main thread detection from thread dump#5733
Open
markushi wants to merge 9 commits into
Open
Conversation
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>
📲 Install BuildsAndroid
|
Contributor
Performance metrics 🚀
|
| 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 |
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
approved these changes
Jul 7, 2026
romtsn
left a comment
Member
There was a problem hiding this comment.
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
…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>
romtsn
reviewed
Jul 7, 2026
romtsn
approved these changes
Jul 7, 2026
Co-authored-by: Roman Zavarnitsyn <rom4ek93@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📜 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
----- pid <pid> at ... -----) and marks the thread whosesysTidequals it as the main thread."main"so downstream consumers see a consistent name even when the OS renamed it.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 == pidis robust and does not depend on the (truncatable) process name.Fixes JAVA-635
Fixes #5731
💚 How did you test it?
ThreadDumpParserTest📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps
--
View Junior Session in Sentry