Bug 18590 - SIGSEGV when throwing C# exceptions in a Java callback on main thread
Summary: SIGSEGV when throwing C# exceptions in a Java callback on main thread
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler (show other bugs)
Version: 4.12.0
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Jonathan Pryor
Depends on:
Reported: 2014-03-26 11:05 UTC by kissling
Modified: 2014-09-09 23:32 UTC (History)
3 users (show)

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


Description kissling 2014-03-26 11:05:39 UTC
When throwing a C# exception while in a Java callback, a SIGSEGV may occur, crashing the app.
This mainly occurs on the main thread.

Steps to reproduce:
1. Create a new Android project
2. Paste in the following code to MainActivity.
3. Run in Release mode.
4. App logs exception, then crashes with SIGSEGV.

using System;
using System.Threading.Tasks;
using Android.App;
using Android.OS;
using Java.Util;

namespace AndroidNativeExceptionBug
    [Activity(MainLauncher = true)]
    public sealed class MainActivity : Activity
        protected override void OnCreate(Bundle savedInstanceState)

                // Synchronous call first logs exception, then segfaults.

                // Works as expected. Doesn't crash the app.
            catch (Exception e)
                Console.WriteLine("Successfully caught {0}.", e);

        private sealed class Comparator : Java.Lang.Object, IComparator
            public int Compare(Java.Lang.Object object1, Java.Lang.Object object2)
                throw new Exception();

        private void Test()
            Arrays.Sort(new [] { Java.Lang.Boolean.False, Java.Lang.Boolean.True }, new Comparator()); 

When the Java callback is run from a different thread via Task.Run, the app does not crash. Please uncomment the second test case in the supplied code (and comment the first one) to see the difference.

Version information:

=== Xamarin Studio ===

Version 4.2.3 (build 60)
Installation UUID: 938751df-f7a5-457d-b87b-379763e0ada1
	Mono 3.2.6 ((no/9b58377)
	GTK+ 2.24.23 theme: Raleigh
	GTK# (
	Package version: 302060000

=== Apple Developer Tools ===

Xcode 5.1 (5084)
Build 5B130a

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: 58c3efa
Build date: 2014-10-03 18:02:26-0400

=== Xamarin.Android ===

Version: 4.12.1 (Business Edition)
Android SDK: /Users/etankissling/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		4.3 (API level 18)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

=== Xamarin.Mac ===

Xamarin.Mac: Not Installed

=== Build Information ===

Release ID: 402030060
Git revision: 30c4afc300c2a39ec5300851357ce02e49dd217e
Build date: 2014-03-05 22:09:33+0000
Xamarin addins: f8a9589b57c2bfab2ccd73c880e7ad81e3ecf044

=== Operating System ===

Mac OS X 10.9.2
Darwin etans-mbp 13.1.0 Darwin Kernel Version 13.1.0
    Thu Jan 16 19:40:37 PST 2014
    root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64
Comment 1 Ram Chandra 2014-03-26 13:34:08 UTC
I tried to reproduce this issue with the steps provided in bug description and I am able to reproduce this issue.

I observed that when I call the Test() in "Release" mode an error occur and application is crashing  but when I uncomment Task.Run((Action)Test).wait()" it doesn't throw any error and application is deployed successfully.

Screencast: http://www.screencast.com/t/BzPi4KTKp

Environment Info:

Mac OS X 10.7.5
Xamarin Studio: 4.2.3 (build 60)
Xamarin.Android: 4.12.1
Comment 2 Jonathan Pryor 2014-03-27 10:50:02 UTC
@Ram: Please gist/attach the complete `adb logcat` output; trying to read truncated text from a screencast is not wonderful.

I suspect that this is a variation/fallout of Bug #7634.

Note You need to log in before you can comment on or make changes to this bug.