Bug 36383 - async / await - Custom awaiter crashing on device
Summary: async / await - Custom awaiter crashing on device
Status: VERIFIED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Mono runtime / AOT compiler (show other bugs)
Version: XI 9.1 (iOS 9.1)
Hardware: PC Mac OS
: --- normal
Target Milestone: C6SR1
Assignee: Zoltan Varga
URL:
: 18938 (view as bug list)
Depends on:
Blocks:
 
Reported: 2015-11-30 17:16 UTC by manuel
Modified: 2016-01-28 19:22 UTC (History)
7 users (show)

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


Attachments
Demo crashing on the device (2.35 MB, application/zip)
2015-11-30 17:21 UTC, manuel
Details

Description manuel 2015-11-30 17:16:36 UTC
Guys

we are preparing a few base classes for a new version of our app.
Our goal is use async await pattern and provide a generic approach to void having OpenGL calls while the app is background. 

A typical Scenario

1 - await "download image from the web"

2 - Create a texture

3 - Execute a rendering step to, for example, apply a filter

If the app goes to background we cannot do OpenGL operations. OS will kill us.

We are trying a generic solution to avoid scenarios like this. Our goal was and is to guarantee that await subsequent code will be queued and managed by us. 
Having this in mind, we tried to created a "rendering context" with a queue of methods and ISynchronizeInvoke interface was selected as the entry point.
The code looks like

	var renderingContext = new Rendering_ExecutionContext_iOS ();

	renderingContext.Start ();

	...

	var task1 = Task.Run (new Action (() => {
		Debug.WriteLine ("Task 1");
	}));

	await task1.ConfigureAwait (renderingContext);

We were happy with the tests in the simulator. It seems to be working. On the device, though, it crashes and it is not clear why looking like a bug in the "magic" code created by the compiler to implement async / await state machine.
Comment 1 manuel 2015-11-30 17:21:39 UTC
Created attachment 14039 [details]
Demo crashing on the device

demo code in main
Comment 2 manuel 2015-11-30 17:23:43 UTC
Comment on attachment 14039 [details]
Demo crashing on the device

test case in main
Comment 3 manuel 2015-11-30 17:44:49 UTC
=== Xamarin Studio ===

Version 5.10.1 (build 1)
Installation UUID: 5f421f3f-40c4-4caa-8ae3-df7a8094edf4
Runtime:
	Mono 4.2.1 (explicit/6dd2d0d)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402010102

=== Xamarin.Profiler ===

Version: 0.0.0.0
Location: /Users/nativolabs/Documents/profiler-mac-0.9-0/XamarinProfiler.Mac.app/Contents/MacOS/XamarinProfiler.Mac

=== Apple Developer Tools ===

Xcode 7.1.1 (9081)
Build 7B1005

=== Xamarin.Android ===

Version: 6.0.0.34 (Business Edition)
Android SDK: /Users/nativolabs/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
		5.1    (API level 22)
		6.0    (API level 23)

SDK Tools Version: 24.3.4
SDK Platform Tools Version: 23
SDK Build Tools Version: 21.1.2

Java SDK: /usr
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

=== Xamarin Android Player ===

Version: 0.2.5
Location: /Applications/Xamarin Android Player.app

=== Xamarin.iOS ===

Version: 9.2.1.52 (Business Edition)
Hash: d070a7b
Branch: master
Build date: 2015-11-23 12:29:45-0500

=== Xamarin.Mac ===

Not Installed

=== Build Information ===

Release ID: 510010001
Git revision: bee8b40553477ae2f5a5fdb19b7c9aa2505c3b02
Build date: 2015-11-23 15:39:53-05
Xamarin addins: d456dff337fe54102a1d98f6cf49c5abe0747ee3
Build lane: monodevelop-lion-cycle6-baseline

=== Operating System ===

Mac OS X 10.11.1
Darwin Nativos-MacBook-Pro.local 15.0.0 Darwin Kernel Version 15.0.0
    Sat Sep 19 15:53:46 PDT 2015
    root:xnu-3247.10.11~1/RELEASE_X86_64 x86_64
Comment 4 Rolf Bjarne Kvinge [MSFT] 2015-11-30 18:40:38 UTC
This is the exception:

> 2015-11-30 19:39:24.139 CustomAwaiter[4076:3443804] System.ExecutionEngineException: Not yet supported.
>   at (wrapper managed-to-native) System.Object:__icall_wrapper_mono_gsharedvt_constrained_call (intptr,intptr,intptr,intptr,intptr)
>   at System.Runtime.CompilerServices.AsyncVoidMethodBuilder.AwaitOnCompleted[TAwaiter,TStateMachine] (System.Runtime.CompilerServices.TAwaiter& awaiter, System.Runtime.CompilerServices.TStateMachine& stateMachine) [0x00086] in /work/maccore/master/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:131 
> --- End of stack trace from previous location where exception was thrown ---
>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /work/maccore/master/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
>   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__1 (System.Object state) [0x00000] in /work/maccore/master/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1034 
>   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context (System.Object state) [0x0000e] in /work/maccore/master/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/threading/threadpool.cs:1289 
>   at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, Boolean preserveSyncCtx) [0x0008d] in /work/maccore/master/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/threading/executioncontext.cs:957 
>   at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, Boolean preserveSyncCtx) [0x00000] in /work/maccore/master/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/threading/executioncontext.cs:904 
>   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x0002a] in /work/maccore/master/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/threading/threadpool.cs:1266 
>   at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00096] in /work/maccore/master/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/threading/threadpool.cs:857 
>   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00000] in /work/maccore/master/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/threading/threadpool.cs:1194 

it still happens with current master.
Comment 5 Zoltan Varga 2015-12-01 17:14:23 UTC
Fixed in mono master 3d24eb5a439294835a78082f395920b8b90313a7, maccore master was bumped to it in 3f5c89349db6e7cfaace0d43648a92bbf04b55a7.
Comment 6 Zoltan Varga 2015-12-01 21:33:49 UTC
*** Bug 18938 has been marked as a duplicate of this bug. ***
Comment 7 Zoltan Varga 2015-12-01 21:34:11 UTC
-> FIXED.
Comment 8 manuel 2015-12-09 12:49:22 UTC
Hi guys, when do you expect to have the fix available? Just tried Xamarin.iOS 9.2.2.29 (current alpha) and it seems to be happening.
Comment 9 Udham Singh 2015-12-14 18:42:43 UTC
I have checked this issue with sample app attached in comment 1 with latest master lane builds MonoFramework-MDK-4.3.2.28.macos10.xamarin.universal_9900f812e2fc72b1e9389c4e7a0602007f256718 + monotouch-9.5.0.40_8f37569e95ab03d38f477576a7b22e87e0055737 (deleted f5286774b43e4412848b715e28dd05e7) and observed that with these builds app is not getting crashed on iOS device (iPhone 6+ iOS 9.2) with these builds.

I have checked the same with Cycle 6 lane builds and observed that app is getting crashed with Cycle 6 lane builds.

Environment Info : 

=== Xamarin Studio ===

Version 5.10.2 (build 38)
Installation UUID: ce927b2a-2c07-44c5-b186-09cfdafba6dc
Runtime:
	Mono 4.3.2 (master/9900f81)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 403020028

=== Apple Developer Tools ===

Xcode 7.1 (9079)
Build 7B91b

=== Xamarin.iOS ===

Version: 9.5.0.40 (Business Edition)
Hash: 8f37569
Branch: master
Build date: 2015-12-12 12:13:47-0500

=== Xamarin Android Player ===

Version: 0.3.7
Location: /Applications/Xamarin Android Player.app

=== Xamarin.Mac ===

Version: 2.4.0.113 (Business Edition)

=== Build Information ===

Release ID: 510020038
Git revision: 13e6578b27902f4c2f56b2d3da513ffa98d81bb6
Build date: 2015-12-10 18:24:12-05
Xamarin addins: f20bfc8b9e98eb86a58bb02e29bf2a309281f224
Build lane: monodevelop-lion-cycle6

=== Operating System ===

Mac OS X 10.10.5
Darwin Xamarin76s-Mac-mini.local 14.5.0 Darwin Kernel Version 14.5.0
    Wed Jul 29 02:26:53 PDT 2015
    root:xnu-2782.40.9~1/RELEASE_X86_64 x86_64
Comment 10 Arpit Jha 2015-12-21 11:46:04 UTC
I have checked this issue with latest C6SR1 build and its working fine.

Now able to build and deploy attached sample app in comment1  on iPhone 6s (iOS 9.2) without any crash.

Screencast: http://www.screencast.com/t/4dU4BE8Ue

Environment Info:
=== Xamarin Studio ===

Version 5.10.2 (build 49)
Installation UUID: 3d25a767-a003-4a7d-9f5e-e57987cf6cf0
Runtime:
	Mono 4.2.2 (explicit/11c1351)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402010003

=== Xamarin.Profiler ===

Version: 0.23.35.0
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 7.2 (9548)
Build 7C68

=== Xamarin.iOS ===

Version: 9.4.1.13 (Enterprise Edition)
Hash: 7d6b9a7
Branch: master
Build date: 2015-12-18 06:22:20-0500

=== Xamarin.Android ===

Version: 6.0.1.5 (Enterprise Edition)
Android SDK: /Users/mac360_xamarin/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.3    (API level 10)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
		5.1    (API level 22)
		6.0    (API level 23)

SDK Tools Version: 24.4
SDK Platform Tools Version: 23.0.1
SDK Build Tools Version: 23

Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

=== Xamarin Android Player ===

Version: 0.6.1
Location: /Applications/Xamarin Android Player.app

=== Xamarin.Mac ===

Version: 2.4.0.109 (Enterprise Edition)

=== Build Information ===

Release ID: 510020049
Git revision: deed4cd240ff3611c8742bbc62296e207f46173d
Build date: 2015-12-17 11:13:36-05
Xamarin addins: 8bd975e7b465f6fe812b64d736f3b7e8c1a79a3e
Build lane: monodevelop-lion-cycle6-c6sr1

=== Operating System ===

Mac OS X 10.10.5
Darwin mac360-xamarins-Mac-mini.local 14.5.0 Darwin Kernel Version 14.5.0
    Wed Jul 29 02:26:53 PDT 2015
    root:xnu-2782.40.9~1/RELEASE_X86_64 x86_64

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