-
Notifications
You must be signed in to change notification settings - Fork 516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issues with unhandled exceptions #15252
Comments
A potential workaround would be to add event handlers to the Ref: https://docs.microsoft.com/en-us/xamarin/ios/platform/exception-marshaling#events |
Thanks. After testing with those, I find that by default the same thing happens there as well. However, if I set the AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
{
Console.WriteLine("In UnhandledException Handler");
};
AppDomain.CurrentDomain.FirstChanceException += (sender, args) =>
{
Console.WriteLine("In FirstChanceException Handler");
};
ObjCRuntime.Runtime.MarshalManagedException += (sender, args) =>
{
Console.WriteLine("In MarshalManagedException Handler");
args.ExceptionMode = ObjCRuntime.MarshalManagedExceptionMode.UnwindNativeCode;
};
ObjCRuntime.Runtime.MarshalObjectiveCException += (sender, args) =>
{
Console.WriteLine("In MarshalObjectiveCException Handler");
};
throw new Exception("Test Exception"); My debug output now includes the following:
So it seems I can workaround the issue for now by using I am a bit confused why this works though. The doc you pointed at says:
So, why does setting |
We changed the default in .NET, and that page hasn't been updated yet: |
Yes, I think that should work fine. |
Thanks! |
Confirming that the same problem exists for macOS applications, and that setting the exception mode to Unwind doesn't seem to make |
I think the comments in this file also have to be updated to reflect what is now the default: https://github.com/xamarin/xamarin-macios/blob/main/src/ObjCRuntime/ExceptionMode.cs |
@rolfbjarne Does this workaround apply to Xamarin.iOS as well? It seems that Runtime.MarshalManagedException, Runtime.MarshalObjectiveCException and AppDomain.CurrentDomain.UnhandledException don't trigger at all in Release mode in Xamarin.iOS unless --interpreter=all is set. Unfortunately, enabling the interpreter in Xamarin.iOS for us is a huge performance hit. |
Yes, but you must explicitly enable exception marshalling (we turned it on by default in .NET). You can do that by adding this to the additional mtouch arguments in the project's iOS Build options (this is the MtouchExtraArgs property in the csproj).
|
Has this issue been resolved in .NET 7?
For a .NET 7 macOS application, it looks like the AppDomain.UnhandledException is not triggered at all. I tested it by throwing a managed exception on the main thread. I can see that the Runtime.MarshalManagedException is fired, but then setting the ExceptionMode to UnwindNativeCode doesn't fire the UnhandledException either, it just crashes the app. |
No, it's not been fixed yet. |
A few updates:
This has been fixed (9b8869b). In my test case,
We can probably fix the fourth, but we likely won't be able to fix the second and third.
This is not fixed. In fact there doesn't seem to be a way for us to call this handler, the managed runtime does it automatically during exception handler. Unfortunately we're somewhat exceptional, so the existing logic in the managed runtime doesn't work for us. |
I've filed dotnet/runtime#102730 to see if we can find a way to raise the UnhandledException event. |
…xception. (#20656) Call mono_unhandled_exception to raise AppDomain.UnhandledException when managed exceptions are unhandled. Partial fix for #15252 (for MonoVM, still pending for CoreCLR, which needs dotnet/runtime#102730 fixed first).
The problem with the |
It has been "fixed", but it looks like you won't be releasing the fix until .NET 10. @rolfbjarne Is that correct? |
No, the fixes that have been implemented so far are all in .NET 9 already. The .NET 10 milestone is for the remaining issues. |
@rolfbjarne , are you sure? Because I'm using .net 9 and MAUI 9.0.12 and I can't get my iOS project to fire AppDomain.CurrentDomain.UnhandledException at all (with a deliberately-caused exception in MAUI/C# code). It works just fine in my Android project. Also, the MarshalManagedException handler never gets called. |
@rolfbjarne
and I can see this event handler being assigned (debugger breakpoint), but the event never fires. This is the case both in an iOS simulator and on an actual device. I am also setting an UnhandledException event handler:
And I can see that event being fired on the iOS simulator, but it never fires on the physical device (iPad attached to my Windows machine via USB cable, and deploying the app to my device which is listed under "iOS Local Devices"). |
@bitshftr with this project: https://github.com/rolfbjarne/maui-exceptions I get this output:
Can you try that project and see what you get? |
Simulator: The only significant difference I see in my log vs. yours is that I have a System.Exception: 'Hexception' line right before the UnhandledException line. Device: My log is significantly different from your log. Here's mine:
That's all that's in my log. And notice that some of the lines are out of order vs. they way your code is arranged. (and I don't know why some of those lines above are treated like code and some aren't...weird git formatting) |
@rolfbjarne Any idea why my device logs show that none of the event handlers are firing, except for FirstChanceException? |
Are you connected to a remote mac where the iOS device is connected? If not, can you try that and see if you get a different behavior? |
Heres's my setup (that generated the above logs):
When I publish the app to TestFlight with the Release build configuration, the UnhandledException handler fires as expected. But of course, I can't debug like that. When I try the build from my Windows machine, to the iPad connected to a remote Mac (which is located down the hall from my office) via USB, as a "Remote Device", in Debug mode: I get a log similar to your "device" log. So it appears the only issue is when building on a Windows machine to a Local Device. Which is, of course, the only way I have to debug on a device interactively. |
Steps to Reproduce
On a Mac, after installing the latest 6.0.300 SDK and the
ios
workload:dotnet new ios
AppDelegate.cs
. At the end ofFinishedLaunching
, beforereturn true;
insert the following code:dotnet run
Expected Behavior
The output should indicate that the
FirstChanceException
handler fires once, and then theUnhandledException
handler should fire once. Then I should get a crash report window from macOS (if applicable).Actual Behavior
The log shows that the
FirstChanceException
handler fires over 1000 times (the exact number seems to vary). TheUnhandledException
handler never fires. I do get the crash report window from macOS.Environment
Version information
Crash Report
I think this is similar to the issues reported in #12040 and #14796, but I'm not certain. Is there any workaround? If not, I don't think should wait for .NET 7.
The text was updated successfully, but these errors were encountered: