Skip to content
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

MacCatalyst currently doesn't surface unhandled exceptions in user code so we should help MAUI users out #7176

Closed
1 task
PureWeen opened this issue May 13, 2022 · 7 comments · Fixed by xamarin/xamarin-macios#21778
Labels
area-core-platform Integration with platforms blocked Work that is currently blocked legacy-area-desktop Windows / WinUI / Project Reunion & Mac Catalyst / macOS specifics (Menus & other Controls)) partner/macios Issues for the Mac / iOS SDK partner Issue or Request from a partner team platform/iOS 🍎 platform/macOS 🍏 macOS / Mac Catalyst s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working t/desktop The issue relates to desktop scenarios (MacOS/MacCatalyst/Windows/WinUI/WinAppSDK)
Milestone

Comments

@PureWeen
Copy link
Member

PureWeen commented May 13, 2022

Currently AppKit will handle any exception thrown from user code and won't surface it to the user.

The following code

object test = null;
test.GetHashCode();

Will just throw an NRE with no indication to the user that an NRE has occurred

We should wire into the following method

Runtime.MarshalManagedException += Runtime_MarshalManagedException;

And break when a debugger is attached to help the user (and us) be more successful.

We should also wire up our UI Tests so that any exceptions that make it here are surfaced.

Depends on

@mattleibow
Copy link
Member

mattleibow commented May 13, 2022

We can add a bit of logic in the platform folder to wire up the event:

class Program
{
    static void Main()
    {
#if DEBUG
        Runtime.MarshalManagedException += (sender, e) => {
            Debugger.Break();
        };
#endif
   }
}

Not sure if it is best in Program.cs or the app delegate... @rolfbjarne where is the best place for an event like this?

@rolfbjarne
Copy link
Member

IMHO the sooner the better, so first thing in Main

@mattleibow
Copy link
Member

Related to xamarin/xamarin-macios#15037

@mattleibow mattleibow added partner Issue or Request from a partner team blocked Work that is currently blocked partner/macios Issues for the Mac / iOS SDK platform/macOS 🍏 macOS / Mac Catalyst platform/iOS 🍎 and removed area-core-hosting Extensions / Hosting / AppBuilder / Startup labels Mar 28, 2023
@mattleibow mattleibow added this to the Backlog milestone Mar 28, 2023
@ghost
Copy link

ghost commented Mar 28, 2023

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

@Eilon Eilon added the legacy-area-desktop Windows / WinUI / Project Reunion & Mac Catalyst / macOS specifics (Menus & other Controls)) label Jun 20, 2023
@samhouts samhouts added the t/bug Something isn't working label Jul 31, 2023
@samdoshi
Copy link

samdoshi commented Oct 4, 2023

If anyone else is trying to work around this issue with uncaught exceptions in MacCatalyst code, I've got the following added to my MacCatalyst / Program.cs main function:

        #if DEBUG
        // https://github.com/dotnet/maui/issues/7176
        Runtime.MarshalManagedException += (_, a) => {
            Console.WriteLine("Uncaught MacCatalyst Exception");
            Console.WriteLine(a.Exception.ToString());
            Debugger.Break();
            Environment.Exit(-1);
        };
        #endif

It will fire the debugger but it isn't much use, the printed stack trace is however. Hopefully this is helpful to someone else.

@rolfbjarne
Copy link
Member

It might work to set the NSApplicationCrashOnExceptions flag by doing something like this:

var defs = new NSDictionary ((NSString) "NSApplicationCrashOnExceptions", NSValue.FromBoolean (true));
NSUserDefaults.StandardUserDefaults.RegisterDefaults (defs);

Ref: https://learn.microsoft.com/en-us/appcenter/sdk/crashes/macos#enable-catching-uncaught-exceptions-thrown-on-the-main-thread

@Zhanglirong-Winnie Zhanglirong-Winnie added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Dec 19, 2023
@Zhanglirong-Winnie
Copy link

Verified this issue with VSM 17.6.7 (build 417). Can repro this issue.

@Eilon Eilon added the t/desktop The issue relates to desktop scenarios (MacOS/MacCatalyst/Windows/WinUI/WinAppSDK) label May 10, 2024
@PureWeen PureWeen added the area-core-platform Integration with platforms label May 31, 2024
rolfbjarne added a commit to xamarin/xamarin-macios that referenced this issue Dec 9, 2024
…ion is marshalled.

This means the debugger will be notified of all marshalled exceptions, and
consider them unhandled (and break the debugged app into the debugger).

There's no corresponding change for CoreCLR, because we have no configuration
where we can attach a debugger to a CoreCLR-based app (VSCode doesn't support
macOS, and NativeAOT doesn't support debugging).

Partial fix for #15037.
Fixes dotnet/maui#7176.
Ref: dotnet/android#6106
@github-actions github-actions bot locked and limited conversation to collaborators Jan 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-core-platform Integration with platforms blocked Work that is currently blocked legacy-area-desktop Windows / WinUI / Project Reunion & Mac Catalyst / macOS specifics (Menus & other Controls)) partner/macios Issues for the Mac / iOS SDK partner Issue or Request from a partner team platform/iOS 🍎 platform/macOS 🍏 macOS / Mac Catalyst s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working t/desktop The issue relates to desktop scenarios (MacOS/MacCatalyst/Windows/WinUI/WinAppSDK)
Projects
None yet
7 participants