Repository navigation
fix(ios): an exception in a native scope callback no longer recurses until the stack overflows - #5877
Merged
Merged
Conversation
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>
jamescrosswell
commented
Nov 5, 2026
| public event MarshalObjectiveCExceptionHandler? MarshalObjectiveCException; | ||
|
|
||
| public bool IsMono { get; } = Type.GetType("Mono.Runtime") != null; | ||
| public bool IsMono { get; } = Type.GetType("Mono.RuntimeStructs") != null; |
Collaborator
Author
There was a problem hiding this comment.
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
ric-oliv
approved these changes
Nov 6, 2026
ric-oliv
previously approved these changes
Nov 6, 2026
ric-oliv
self-requested a review
November 6, 2026 17:01
This was referenced Nov 10, 2026
This was referenced Nov 12, 2026
This was referenced Nov 13, 2026
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.
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 raisesRuntime.MarshalManagedException. In #5867 it threw because allocating the callback's managedSentryObjCScopewrapper failed under memory pressure.RuntimeMarshalManagedExceptionIntegration.Handlecaptured 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 aStack overflowpointing at Sentry.CocoaScope.Configurenow wraps all eight nativeConfigureScopecalls. It marks the thread while the native call runs and catches whatever comes back.Handledoesn't capture an exception that escapes one of those callbacks, or abort for it. Instead it sets the mode that returns the exception toConfigure:UnwindNativeCodeon Mono, andThrowObjectiveCExceptionon 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.IsMonochecked forMono.Runtime, which exists only in legacy Mono, so it was alwaysfalseon .NET. It now checksMono.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.Handleno longer throws when its own capture fails. The issue also hit this, as aHandleFatalErrorabort. In that case the exception mode is left unchanged.Notes for review:
UnwindNativeCodeand the NSException both skip sentry-cocoa'sconfigureScope:frames without running their cleanup. Nothing in that chain holds a lock across the callback (SentrySDK→SentrySDKInternal→-[SentryHub configureScope:]). The per-callSentryObjCScopewrapper leaks on this path.SentryObjCScopeselectors 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 toConfigure. 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