Skip to content

feat(android): attach all threads to app hangs through the NDK - #2344

Open
bitsandfoxes wants to merge 2 commits into
masterfrom
spire/sentry-native-ndk-attach-all-threads-to
Open

bitsandfoxes wants to merge 2 commits into
masterfrom
spire/sentry-native-ndk-attach-all-threads-to

Conversation

@bitsandfoxes

Copy link
Copy Markdown
Contributor

Android SDKs that use the NDK can turn on app-hang tracking (NdkOptions.setEnableAppHangTracking). However, their app-hang events carry only the hung thread's stack. sentry-native can now attach the stacks of all threads to app-hang events (sentry_options_set_app_hang_thread_stackwalk_mode, #2327), but the NDK bindings don't expose it, so the Android SDK and the SDKs built on it can't turn it on.

How

NdkOptions gets setAppHangAttachAllThreads(boolean) and isAppHangAttachAllThreads(). It's off by default, next to setEnableAppHangTracking and setAppHangTimeoutMillis.

  • On: initSentryNative calls sentry_options_set_app_hang_thread_stackwalk_mode(options, SENTRY_THREAD_STACKWALK_MODE_ALL). Each app-hang event lists the other threads with their names and stacks, and the hung thread is marked as crashed.
  • Off: the init doesn't touch the mode, so it stays on native's default, SENTRY_THREAD_STACKWALK_MODE_HUNG_ONLY, and app-hang events are unchanged.

The option only has an effect while app-hang tracking is on.

Caveats

The option exposes two Android issues from #2327 to every app that turns it on. Today they can only happen for the hung thread; with the option on, they can happen for every thread.

  • Allocator-lock deadlock: to read a thread's stack, the watchdog pauses that thread and unwinds it with libunwindstack, which allocates memory. A thread paused while it holds the allocator's lock leaves the watchdog waiting on that lock, and both threads hang.
  • EINTR: each thread is interrupted by a signal so its stack can be read. A thread blocked in sleep, poll, epoll_wait or a similar call returns early with EINTR. Code that doesn't retry on EINTR can treat that as an error.

Add NdkOptions.setAppHangAttachAllThreads(boolean) and
isAppHangAttachAllThreads(), off by default. When it is on, the JNI init
sets the app-hang thread stackwalk mode to SENTRY_THREAD_STACKWALK_MODE_ALL;
when it is off, only the hung thread is walked.
@github-actions

github-actions Bot commented Nov 25, 2026 •

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

Generated by 🚫 dangerJS against f0aa3c5

@bitsandfoxes
bitsandfoxes marked this pull request as ready for review November 25, 2026 14:11

This branch has not been deployed

No deployments
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.

1 participant