Skip to content

fix(ios): an exception in a native scope callback no longer recurses until the stack overflows - #5877

Merged
jamescrosswell merged 1 commit into
mainfrom
ai/sentry-dotnet-5867
Nov 8, 2026
Merged

jamescrosswell merged 1 commit into
mainfrom
ai/sentry-dotnet-5867

Conversation

@jamescrosswell

Copy link
Copy Markdown
Collaborator

On iOS the SDK reads the replay ID, and syncs scope changes, by passing a managed callback to native configureScope:. If that callback throws, the exception crosses the native boundary and raises Runtime.MarshalManagedException. In #5867 it threw because allocating the callback's managed SentryObjCScope wrapper failed under memory pressure. RuntimeMarshalManagedExceptionIntegration.Handle captured it, and that capture read the replay ID through the same callback, which threw again. The recursion only stopped when the stack overflowed. The original report was lost, and the crash showed up as a Stack overflow pointing at Sentry.

  • CocoaScope.Configure now wraps all eight native ConfigureScope calls. It marks the thread while the native call runs and catches whatever comes back.
  • Handle doesn't capture an exception that escapes one of those callbacks, or abort for it. Instead it sets the mode that returns the exception to Configure: UnwindNativeCode on Mono, and ThrowObjectiveCException on CoreCLR and NativeAOT, which can't unwind native frames. The capture then continues without the replay ID or that scope sync. Mono needs its own mode because there the NSException can't unwind back through Mono's frames, so it ends as an uncaught NSException crash.
  • RuntimeAdapter.IsMono checked for Mono.Runtime, which exists only in legacy Mono, so it was always false on .NET. It now checks Mono.RuntimeStructs, the same check dotnet/runtime's own tests use. This also makes the Mono-only branch from fix: prevent redundant native exceptions on iOS #5126 reachable.
  • Handle no longer throws when its own capture fails. The issue also hit this, as a HandleFatalError abort. In that case the exception mode is left unchanged.

Notes for review:

  • UnwindNativeCode and the NSException both skip sentry-cocoa's configureScope: frames without running their cleanup. Nothing in that chain holds a lock across the callback (SentrySDK → SentrySDKInternal → -[SentryHub configureScope:]). The per-call SentryObjCScope wrapper leaks on this path.
  • The unit tests fake the native call, so they don't cover the native round trip. I checked it with a local repro app that makes the native SentryObjCScope selectors throw, on an iOS 27.1 simulator in Release, on both Mono (.NET 10.0.403) and CoreCLR (.NET 11 rc.2). Each failure now crosses the boundary once and comes back to Configure. Captures return, their events arrive, and the app keeps running. When app code then throws across the boundary, its exception is captured once before the usual abort, with no duplicate native crash. Unpatched main recursed 215 times in that scenario and sent nothing. The iOS device tests and the iOS crash-integration tests pass locally.

Closes #5867

🤖 Generated with Claude Code

An exception escaping a managed callback passed to native configureScope: raised
Runtime.MarshalManagedException. Its handler captured the exception, and that capture read
the replay ID through the same callback, so under memory pressure it recursed until the
stack overflowed and the original report was lost.

CocoaScope.Configure now wraps every native ConfigureScope call. While it runs, the handler
hands the exception back to Configure instead of capturing it: UnwindNativeCode on Mono,
ThrowObjectiveCException on CoreCLR. RuntimeAdapter.IsMono, which was always false on .NET,
now detects Mono, and the handler no longer throws if its own capture fails.

Fixes #5867

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
public event MarshalObjectiveCExceptionHandler? MarshalObjectiveCException;

public bool IsMono { get; } = Type.GetType("Mono.Runtime") != null;
public bool IsMono { get; } = Type.GetType("Mono.RuntimeStructs") != null;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.67%. Comparing base (a9b0062) to head (0edb3ca).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5877      +/-   ##
==========================================
+ Coverage   76.57%   76.67%   +0.10%     
==========================================
  Files         528      528              
  Lines       19624    19524     -100     
  Branches     3873     3840      -33     
==========================================
- Hits        15028    14971      -57     
+ Misses       3711     3669      -42     
+ Partials      885      884       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jamescrosswell
jamescrosswell marked this pull request as ready for review November 5, 2026 23:00
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 5, 2026

@ric-oliv ric-oliv 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.

Very interesting solution...

ric-oliv
ric-oliv previously approved these changes Nov 6, 2026

@ric-oliv ric-oliv 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.

Tested, it fixes the issue, and I cannot see any potential issues.

@ric-oliv
ric-oliv dismissed their stale review November 6, 2026 16:59

Duplicate of my earlier approval.

@ric-oliv
ric-oliv self-requested a review November 6, 2026 17:01

@ric-oliv ric-oliv 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.

LGTM!

@jamescrosswell
jamescrosswell merged commit c29eed4 into main Nov 8, 2026
61 checks passed
@jamescrosswell
jamescrosswell deleted the ai/sentry-dotnet-5867 branch November 8, 2026 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

2 participants