Bug 43925 - Hard crashes from unhandled exceptions with no recourse
Summary: Hard crashes from unhandled exceptions with no recourse
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Runtime ()
Version: Master
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Chris Hamons
Depends on:
Reported: 2016-08-31 22:57 UTC by matt
Modified: 2016-09-13 17:59 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Example project that hard crashes when ran (347.72 KB, application/zip)
2016-08-31 22:57 UTC, matt
XCode Cocoa proj with same code as example by bug reporter. (27.70 KB, application/zip)
2016-09-03 00:33 UTC, Jon Goldberger [MSFT]





Related Links:

Description matt 2016-08-31 22:57:24 UTC
Created attachment 17286 [details]
Example project that hard crashes when ran

Certain exceptions that occur in Apple APIs hard crash our app without getting caught at any level. Attached is an example project that hard crashes the app from simply setting an NSComboBox index to -1.

We prefer to show unhandled exceptions in a user friendly dialog that allows support tickets to be submitted, and let the user continue using the app. Some exceptions do force the app to shutdown but in most cases we can still show an error dialog or at least log some data then restart the app. 

The example project attempts to catch the exception with the following methods:
* Setting NSSetUncaughtExceptionHandler (https://developer.apple.com/reference/foundation/1409609-nssetuncaughtexceptionhandler?language=objc)
* Subscribing to AppDomain.CurrentDomain.UnhandledException
* Try/catch around NSApplication.Main(args) or NSApplication.SharedApplication.Run()

The crash log: https://gist.github.com/zone117x/dd45be244c05c415598bca5f343478d2
Comment 1 matt 2016-08-31 23:08:26 UTC
It looks like there is some related handling going on here: https://github.com/xamarin/xamarin-macios/search?utf8=%E2%9C%93&q=xamarin_throw_ns_exception
But not really sure what that's doing or how to use it.
Comment 2 Jon Goldberger [MSFT] 2016-09-03 00:33:14 UTC
Created attachment 17324 [details]
XCode Cocoa proj with same code as example by bug reporter.

I could reproduce this issue and noted that the native exception is not propogated to the managed side of things. I see that this is expected most of the time as per some posts  I found from Rolf [1] [2] and others [3] about this. 

However, testing a native Obj-C Cocoa app (attached) that uses the exact same code does not crash the app but rather generates a warning and the app continues to run. 

[1] https://bugzilla.xamarin.com/show_bug.cgi?id=18427#c3
[2] https://forums.xamarin.com/discussion/comment/123809/#Comment_123809
[3] http://stackoverflow.com/questions/23909348/avoid-mac-app-crashing-after-unhandled-exception
Comment 4 matt 2016-09-07 23:06:58 UTC
This crashing behavior is a blocker for us. Seems like the issue should be marked higher priority considering there are probably lots of Xamarin.Mac apps deployed in the field right now hard crashing more often than comparable objc/swift apps. These are the kind of problems are turn devs away from the Xamarin ecosystem.
Comment 5 Chris Hamons 2016-09-13 17:59:53 UTC

Apologies for the delay in response, I was out of the office.

The difficulty in handling "native" objective-c exceptions in C# applications has been a pain point we've know about for awhile. Solving it was non-trivial, but in Cycle8 (currently in Beta, will hit stable within the next day or two) we have a solution.

If you go into your project projects, mac build and add the following to your additional mmp arguments


we will catch the native exception and convert it to a managed one. There will be a minor performance hit, larger on 32-bit.

In the future, we may default this on, but for right now it'll be opt in.

Please reopen if that doesn't solve your use case.