Bug 19077

Summary: Invalid IL code when using async/await and BeginBackgroundTask()
Product: [Mono] Compilers Reporter: René Ruppert <rene.ruppert>
Component: C#Assignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: mono-bugs+monotouch, mono-bugs+mono, rene.ruppert, sebastien
Priority: ---    
Version: unspecified   
Target Milestone: ---   
Hardware: Macintosh   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:

Description René Ruppert 2014-04-16 05:16:13 UTC
This is from the XAM210 XamU backgrounding sample app.

Using the code sample below I get the this error:

2014-04-16 11:13:36.377 Demo2_LongRunningTasks[2864:70b] Exception: Invalid IL code in Demo2LongRunningTasks.LongRunningTaskViewController/<OnStartTask>c__async6:MoveNext (): IL_0034: stfld     0x0400001a

Changing the code to use Thread.Sleep() instead and remove the "async" modifier on the lambda fixes it. Note that the error does not show if I pass a cancellation token in.

taskId = UIApplication.SharedApplication.BeginBackgroundTask(() => {
			});

			// Kick off .NET Task to run in the background.
			try {
				await Task.Run(/*remove async to make it work*/ async () => {
			        for (long count = 1; running == true ; count++) {

						this.BeginInvokeOnMainThread(() => {
			                LogText("Task {0} running.. {1}", taskId, count);
						});

                                                // Use Thread.Sleep() to make it work.
						//Thread.Sleep(1000);
						await Task.Delay(1000);
			        }
				});
			}
            catch (OperationCanceledException)
            {
                LogText("Task {0} was cancelled.", taskId);
            }
            catch (Exception ex)
            {
                LogText("Exception: {0}", ex.Message);
            }
			finally
			{
                sender.SetTitle(StartText, UIControlState.Normal);
                TaskButton.BackgroundColor = StartColor;
                taskId = 0;
                running = false;
				UIApplication.SharedApplication.EndBackgroundTask(taskId);
            }

=== Xamarin Studio ===

Version 4.2.4 (build 32)
Installation UUID: 220dde4a-6e9c-4c80-a89e-6ef72bea0759
Runtime:
	Mono 3.2.6 ((no/9b58377)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 302060000

=== Xamarin.Android ===

Version: 4.12.3 (Business Edition)
Android SDK: /Users/rene/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.1   (API level 7)
		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.3   (API level 18)
		4.4   (API level 19)
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)

=== Apple Developer Tools ===

Xcode 5.1.1 (5085)
Build 5B1008

=== Xamarin.iOS ===

Version: 7.2.1.40 (Business Edition)
Hash: 86b1dbd
Branch: 
Build date: 2014-04-11 23:42:07-0400

=== Xamarin.Mac ===

Xamarin.Mac: Not Installed

=== Build Information ===

Release ID: 402040032
Git revision: a160c35dac9ab9fd32eeadaa171216316d5a5133
Build date: 2014-03-12 13:55:08-04
Xamarin addins: a779416ceabd54981ce812771a4061c942e8b872

=== Operating System ===

Mac OS X 10.9.2
Darwin iMacBuero.local 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 Sebastien Pouliot 2014-04-16 09:34:52 UTC
Marek, known issue in 3.2.6's mcs ?
Comment 2 Marek Safar 2014-04-16 09:37:13 UTC
Please test with Mono 3.4 (or at least 3.2.8) there were quite few fixes which will probably resolve this issue.
Comment 3 René Ruppert 2014-04-16 10:05:46 UTC
I have only tested this on X.iOS and there the issue persists even with the latest alpha:

=== Xamarin Studio ===

Version 4.3.4 (build 0)
Installation UUID: 220dde4a-6e9c-4c80-a89e-6ef72bea0759
Runtime:
	Mono 3.4.0 ((no/d4511ef)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 304000185

=== Apple Developer Tools ===

Xcode 5.1.1 (5085)
Build 5B1008

=== Xamarin.Mac ===

Xamarin.Mac: Not Installed

=== Xamarin.iOS ===

Version: 7.2.1.40 (Business Edition)
Hash: 86b1dbd
Branch: 
Build date: 2014-04-11 23:42:07-0400

=== Xamarin.Android ===

Version: 4.12.3 (Business Edition)
Android SDK: /Users/rene/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.1   (API level 7)
		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.3   (API level 18)
		4.4   (API level 19)
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)

=== Build Information ===

Release ID: 403040000
Git revision: e2e118a56ebf05c9f15eafbf53379bdebded727c
Build date: 2014-03-12 15:39:07-04
Xamarin addins: 9f2eb9aed862e28ca23d2d2f34e22952087e6e4c

=== Operating System ===

Mac OS X 10.9.2
Darwin imacbuero.fritz.box 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 4 Rolf Bjarne Kvinge [MSFT] 2014-04-22 10:52:58 UTC
Removing NEEDINFO since it happens in Mono 3.4 according to previous comment.
Comment 5 Marek Safar 2014-04-28 08:33:48 UTC
Could you attach complete sample which is failing for you. I cannot reproduce the issue locally.
Comment 7 Marek Safar 2014-04-28 08:51:17 UTC
I still cannot reproduce it. Using your sample with manual changes from your comment c#0 I get 

Demo2_LongRunningTasks[5149:80b] Task 1 running.. 1
Demo2_LongRunningTasks[5149:80b] Task 1 running.. 2
Demo2_LongRunningTasks[5149:80b] Task 1 running.. 3
Demo2_LongRunningTasks[5149:80b] Task 1 running.. 4
Demo2_LongRunningTasks[5149:80b] Task 1 running.. 5
Demo2_LongRunningTasks[5149:80b] Task 1 running.. 6
Demo2_LongRunningTasks[5149:80b] Task 1 running.. 7
Demo2_LongRunningTasks[5149:80b] Task 1 running.. 8
Demo2_LongRunningTasks[5149:80b] Task 1 running.. 9
Demo2_LongRunningTasks[5149:80b] Task 1 running.. 10
....

Are you sure you are using 3.4.0 mono ?
Comment 8 René Ruppert 2014-04-28 08:56:23 UTC
Sorry. Wrong sample attached. I will add the right one in a second. I have just retried and the error persists.
Comment 10 Marek Safar 2014-04-28 09:02:57 UTC
I can reproduce the issue with the latest sample attached
Comment 11 Marek Safar 2014-04-29 07:25:11 UTC
Fixed in master