Skip to content

feat: ios native anr capture#2679

Open
bitsandfoxes wants to merge 1 commit into
chore/anr-native-samplesfrom
feat/ios-anr
Open

feat: ios native anr capture#2679
bitsandfoxes wants to merge 1 commit into
chore/anr-native-samplesfrom
feat/ios-anr

Conversation

@bitsandfoxes
Copy link
Copy Markdown
Contributor

What does it do

This PR allows Unity users to set enableAppHangTracking through the SentryUnityOptions via IosNativeAnrEnabled.

Why does it do it

On iOS, the Unity player loop runs on the main thread (the one with the CFRunLoop). The generated UnityAppController.mm registers Unity's frame tick as a CADisplayLink callback on the main run loop. Every Update, FixedUpdate, and LateUpdate execute on that single thread. Native UIKit/AppKit events share the same queue.
So when a Unity script blocks (e.g. infinite loop in Update, synchronous I/O, deadlock on .Wait()), it blocks the iOS main run loop directly. sentry-cocoa's watchdog enqueues a block on the main queue and times its execution. That means if Unity is stuck, that block never runs, and that hang is reported.

This is effectively replacing the Unity SDK C# ANR integration. The generated native ANR event contains an actual stack trace, making the report actually actionable.

How does it do it

We add a new flag IosNativeAnrEnabled that mirrors the AndroidNativeAnrEnabled. But unlike on Android, this replaces the C# watchdog entirely. We will need to follow up on this for Android as well. But the idea is to deprecate the `Anr

Screenshot 2026-05-13 at 16 35 22

The resulting native event contains a fully symbolicated stack trace of the C# code that caused the freeze.

Follow up

We'll need to work on the in-app frames to clean up the stack trace presentation.

@github-actions
Copy link
Copy Markdown
Contributor

Fails
🚫 Please consider adding a changelog entry for the next release.
Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Instructions and example for changelog

Please add an entry to CHANGELOG.md to the "Unreleased" section. Make sure the entry includes this PR's number.

Example:

## Unreleased

### Features

- ios native anr capture ([#2679](https://github.com/getsentry/sentry-unity/pull/2679))

If none of the above apply, you can opt out of this check by adding #skip-changelog to the PR description or adding a skip-changelog label.

Generated by 🚫 dangerJS against c3b5f3b

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit c3b5f3b. Configure here.

OptionsSetInt(cOptions, "enableNetworkBreadcrumbs", 0);

Logger?.LogDebug("Setting EnableAppHangTracking: {0}", options.IosNativeAnrEnabled);
OptionsSetInt(cOptions, "enableAppHangTracking", options.IosNativeAnrEnabled ? 1 : 0);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

macOS gets duplicate ANR detection from iOS-specific flag

Medium Severity

SentryCocoaBridgeProxy.Init() is shared between iOS and macOS, and now unconditionally sets enableAppHangTracking based on IosNativeAnrEnabled (which defaults to true). On macOS, this enables native app-hang tracking, but unlike the iOS branch, DisableAnrIntegration() is never called — so both the native sentry-cocoa watchdog and the C# AnrIntegration run simultaneously, likely producing duplicate ANR reports on macOS.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit c3b5f3b. Configure here.

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