Bug 33305 - [XA 5.1] Exceptions thrown from void-returning `async` methods don't contain the full stack trace.
Summary: [XA 5.1] Exceptions thrown from void-returning `async` methods don't contain ...
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 5.1
Hardware: PC All
: High major
Target Milestone: 6.0 (C6)
Assignee: Jonathan Pryor
: 36763 ()
Depends on: 30513
  Show dependency tree
Reported: 2015-08-21 21:34 UTC by Brendan Zagaeski (Xamarin Team, assistant)
Modified: 2015-12-10 17:18 UTC (History)
3 users (show)

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

Test case (30.71 KB, application/zip)
2015-08-21 21:34 UTC, Brendan Zagaeski (Xamarin Team, assistant)
Detailed verification status version info (1.53 KB, text/plain)
2015-09-08 15:37 UTC, Brendan Zagaeski (Xamarin Team, assistant)

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:

Description Brendan Zagaeski (Xamarin Team, assistant) 2015-08-21 21:34:32 UTC
Created attachment 12608 [details]
Test case

It appears that exceptions thrown from `async` blocks are losing their topmost stack frame.

This is very closely related to Bug 30513. I suspect that fixing the test case from Bug 30513, Comment 1 might resolve this test case too. But that example uses a Task-returning `async` method, so I am filing this bug separately to ensure that this test case is also "verified fixed" once Bug 30513 is resolved.

## Regression status: regression, but a bit of a special case since it seems to
be related to the new exception propagation style in XA 5.1

"BAD":  XA   (f98871a)
"BAD":  XA (d23da36)
"GOOD": XA  (86274ad)

## Steps to reproduce

1. Run the attached test case on emulator or device.

2. Tap the "Hello World, Click Me!" button.

## Results

The stack trace for the exception (from the application output) is missing the most important stack frame: where the exception was thrown from:

### Excerpt from the application output on XA 5.1

> [MonoDroid] System.InvalidOperationException: die!
> [MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
> [MonoDroid] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (object) [0x00000] in /Users/builder/data/lanes/1978/f98871a9/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1006
> [MonoDroid] at Android.App.SyncContext/<Post>c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/src/Android.App/SyncContext.cs:18
> [MonoDroid] at Java.Lang.Thread/RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:36
> [MonoDroid] at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) [0x00009] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Java.Lang.IRunnable.cs:71
> [MonoDroid] at (wrapper dynamic-method) object.49d98638-00ff-43b9-86fc-840bf0c1e3ca (intptr,intptr) <IL 0x00011, 0x0001f>

Note that there's no mention of MainActivity or anything else relevant to
tracking down where the exception came from.

## Example "expected result" from XA 4.20 (excerpt from the application output)

> [MonoDroid] System.InvalidOperationException: die!
> [MonoDroid]   at AndroidApp1.MainActivity+<OnCreate>c__AnonStorey1+<OnCreate>c__async0.MoveNext () [0x00052] in /private/tmp/Working/AsyncVoidIncompleteStackTrace/AndroidApp1/MainActivity.cs:31 

## Additional version information (brief)

Mono 4.0.3 (detached/d6946b4)

Xamarin Studio 5.9.5 (build 10) (48d16bc)

Mac OS X 10.10.5
Comment 2 Jonathan Pryor 2015-08-22 09:19:49 UTC
Fixed in monodroid/e5042d7c.
Comment 3 Brendan Zagaeski (Xamarin Team, assistant) 2015-09-08 15:37:15 UTC
Created attachment 12828 [details]
Detailed verification status version info

## Verification status: fixed on current Alpha version

GOOD: Xamarin.Android (cfa43bb5), which I believe is based on mono/mono-4.2.0-branch 9b990f2.

## Results

The stack trace in the `adb logcat` output contains the expected top frame of the stack trace (it mentions "OnCreate", just as in the "expected result" from XA 4.20 in comment 0):

> System.InvalidOperationException: die!
>   at AndroidApp1.MainActivity+<OnCreate>c__AnonStorey1+<OnCreate>c__async0.MoveNext () <0x8f522608 + 0x000c7> in <filename unknown>:0 
> --- End of stack trace from previous location where exception was thrown ---
>    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () in <filename unknown>:line 0
>    at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (System.Object state) in <filename unknown>:line 0
>    at Android.App.SyncContext+<Post>c__AnonStorey0.<>m__0 () in <filename unknown>:line 0
>    at Java.Lang.Thread+RunnableImplementor.Run () in <filename unknown>:line 0
>    at Java.Lang.IRunnableInvoker.n_Run (IntPtr jnienv, IntPtr native__this) in <filename unknown>:line 0
>    at (wrapper dynamic-method) System.Object:b87f5e4a-9ad1-40ad-ab03-bfde2b4fc051 (intptr,intptr)

## Additional version info (brief)

### Devices tested

- Xamarin Android Player 0.4.4, Nexus 4, Android 4.4, x86 emulator

- LG Optimus L9, Android 4.1.2, armeabi-v7a hardware device
Comment 4 Brendan Zagaeski (Xamarin Team, assistant) 2015-09-08 15:38:26 UTC
Updating status to "verified" as per comment 3.
Comment 5 Marek Safar 2015-12-09 14:42:09 UTC
*** Bug 36763 has been marked as a duplicate of this bug. ***
Comment 6 Brendan Zagaeski (Xamarin Team, assistant) 2015-12-10 17:18:46 UTC
I have now re-verified the test case from Comment 0 using the Cycle 6 Stable versions. I will accordingly mark this bug as CLOSED.

Please file any new reports of similar problems observed with Xamarin.Android 6.0 or higher as new bugs (for example using the Cycle 6 bug entry page [1]). Thanks in advance!

[1] https://bugzilla.xamarin.com/newbug?release=C6

## Verification status: fixed in Cycle 6

> GOOD: Xamarin.Android (3efa14c) Cycle 6 Stable
> GOOD: Xamarin.Android (3192c79) Cycle 6 – Service Release 1 Alpha

## Additional version info (brief)

### Devices tested

- Xamarin Android Player 0.4.4, Nexus 4, Android 4.4, x86 emulator

- LG Optimus L9, Android 4.1.2, armeabi-v7a hardware device