Bug 27916 - Exception thrown in Modal Window causes native crash (Classic API)
Summary: Exception thrown in Modal Window causes native crash (Classic API)
Status: RESOLVED ANSWERED
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Other ()
Version: 1.10.0
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: 2.4.0 (C6)
Assignee: Chris Hamons
URL:
Depends on:
Blocks:
 
Reported: 2015-03-11 15:09 UTC by Jon Goldberger [MSFT]
Modified: 2015-04-08 15:01 UTC (History)
3 users (show)

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


Attachments
Test Project (31.61 KB, application/zip)
2015-03-11 15:09 UTC, Jon Goldberger [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Jon Goldberger [MSFT] 2015-03-11 15:09:59 UTC
Created attachment 10278 [details]
Test Project

## Description

An exception thrown in a modal window causes a native crash. 

## Steps to reproduce:

1. Open attached test project.

2. Launch App

3. Click the Push Me button

4. Click the Crash button

Expected result: App will not crash with a native crash

Actual result: App crashes with native crash.

## Native stacktrace:

gist: https://gist.github.com/jgold6/67502d329b80bec19708


## Notes

I tested this on Xam.Mac 1.10, 1.12, and 1.13. I was able to reproduce the native crash with all three versions.

## Version info:

=== Xamarin Studio ===

Version 5.8 (build 443)
Installation UUID: 2dc9022f-f9a8-424f-8284-bf224cbbfde0
Runtime:
	Mono 3.12.1 ((detached/b7764aa)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312010000

=== Apple Developer Tools ===

Xcode 6.2 (6776)
Build 6C131e

=== Xamarin.Mac ===

Version: 1.10.0.21 (Business Edition)

=== Xamarin.Android ===

Version: 4.20.0.37 (Business Edition)
Android SDK: /Users/apple/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)

=== Xamarin Android Player ===

Version: Unknown version
Location: /Applications/Xamarin Android Player.app

=== Xamarin.iOS ===

Version: 8.8.0.2 (Business Edition)
Hash: ccfcd59
Branch: 
Build date: 2015-03-10 02:20:32-0400

=== Build Information ===

Release ID: 508000443
Git revision: 73883239470cbe8e261c94d95f7c3d0452fd393b
Build date: 2015-03-10 07:22:51-04
Xamarin addins: a2ff7b617f09d9c45d8bbf3d010b5db0d7d36100

=== Operating System ===

Mac OS X 10.10.2
Darwin Jons-iMac.local 14.1.0 Darwin Kernel Version 14.1.0
    Thu Feb 26 19:26:47 PST 2015
    root:xnu-2782.10.73~1/RELEASE_X86_64 x86_64
Comment 3 Chris Hamons 2015-03-25 18:22:19 UTC
I can reproduce this as well.

We are looking into ways of improving exception handling in the future, but sometime when you "leak" a c# exception through objective-c strange things can occur. 

Until we can improve this, the obvious, but rather annoying, solution for this is "don't do that".
Comment 4 Chris Hamons 2015-03-26 09:55:42 UTC
I've spoken with some other member of my team, and the short answer is that you never should let a C# exception "escape" to the calling objective-c. If you do, the results are undefined but generally involve crashing.

Without getting too far into the technical details, the issue is that we can't just convert the c# exception to an objective-c one, as this would cause performance issues and objective-c exceptions really aren't much better.

I'm going to work with documentation to try to get this documented better.

Sorry, I'm sure that wasn't the solution you were hoping for when you filed this bug.
Comment 5 Chris Hamons 2015-03-26 15:48:47 UTC
https://github.com/praeclarum/StopCrashing might be of interest as well.
Comment 6 Benoit Sansregret 2015-04-03 13:13:19 UTC
Hi,

I'm really disappointed with your answer. Basically you are saying to me don't code bugs and you are going to be ok. 
I mentioned in my email to the support that my real case was more complex then the simplified test project I sent you.

The crash was happening randomly in a method of a NSMenuDelegate we created. The problem has been found by luck and it's now fixed. 

My complain is about having more information to debug those issue. The the app is crashing and the only thing you got his a cryptic Native Crash that doesn't contain any hint on what part of your code is throwing it.

The StopCrashing script worked great and it will helps us prevent some future crash but there will be situation where it will fail.

I wish there was a way for you to give us more information when the app crash.

Thank you
Comment 7 Chris Hamons 2015-04-08 15:01:12 UTC
Benoit,

I'm sorry with the fact that you are disappointed with the answer I gave. I'll try to give you more color on specifically why what you are asking for is a very difficult problem to solve in a performant manor, and that it isn't any different than what we tell iOS customers.

Nobody here believes that having managed exceptions that bubble through unmanaged code be undefined behavior (and thus bad) is the ideal solution. In general, we do everything we can to bubble up useful information for both native and managed crashes to help you solve your issues quickly.

Without getting too bogged down with the technical reasons why, setting up the infrastructure to catch managed exceptions at every managed/native boundary is non-trivially expensive and there are a _lot_ of transitions that happen in many common operations. Many operations, specifically ones that involve the UI thread must finish quickly or your app will stutter and have unacceptable performance characteristics. Many of those callbacks do very simple things that rarely have the possibility of throwing, so this overhead would both be expensive and unnecessary in those cases. 

Thus, we'd don't set up those try / catches for you. For places where you code does non-trivial things (beyond say returning a booleans or simple math), you can try catch yourself. 

Hope that helps you understand the answer, even if it wasn't what you were hoping for.

Thanks,
-- Chris H