Bug 13356 - Invalid IL code when using .Wait () instead of await
Summary: Invalid IL code when using .Wait () instead of await
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2013-07-19 19:18 UTC by Timothy Risi
Modified: 2015-05-30 01:24 UTC (History)
4 users (show)

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

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 GitHub or Developer Community 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 FIXED

Description Timothy Risi 2013-07-19 19:18:03 UTC
I'm working on adding async to the MonoCatalog-MonoDevelop sample (code attached).  There's one call that works correctly if I await it, but crashes with 'System.AggregateException:  ---> System.Exception: Invalid IL code in ...' if I instead use .Wait ().

Full stacktrace:

System.AggregateException:  ---> System.Exception: Invalid IL code in MonoCatalog.MonoDataSqliteController/ItemsDataSource/<GetKeyValuePairCell>c__async5/<GetKeyValuePairCell>c__AnonStorey10/<GetKeyValuePairCell>c__asyncF:MoveNext (): IL_00c2: stloc.2   
 
 
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[<GetKeyValuePairCell>c__asyncF] (MonoCatalog.<GetKeyValuePairCell>c__asyncF& stateMachine) [0x0001b] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.CompilerServices/AsyncTaskMethodBuilder.cs:112
  at MonoCatalog.MonoDataSqliteController+ItemsDataSource+<GetKeyValuePairCell>c__async5+<GetKeyValuePairCell>c__AnonStorey10.<>m__1C (Mono.Data.Sqlite.SqliteCommand c) [0x00010] in /Users/Tim/Projects/Xamarin/monotouch-samples/MonoCatalog-MonoDevelop/MonoDataSqlite.xib.cs:179
  at MonoCatalog.MonoDataSqliteController+<WithCommandAysnc>c__async3+<WithCommandAysnc>c__AnonStorey19+<WithCommandAysnc>c__async18.MoveNext () [0x00044] in /Users/Tim/Projects/Xamarin/monotouch-samples/MonoCatalog-MonoDevelop/MonoDataSqlite.xib.cs:282
  at --- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:62
  at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00043] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.CompilerServices/TaskAwaiter.cs:60
  at MonoCatalog.MonoDataSqliteController+<WithConnectionAsync>c__async2.MoveNext () [0x000e2] in /Users/Tim/Projects/Xamarin/monotouch-samples/MonoCatalog-MonoDevelop/MonoDataSqlite.xib.cs:271
  at --- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:62
  at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00043] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.CompilerServices/TaskAwaiter.cs:60
  at MonoCatalog.MonoDataSqliteController+<WithCommandAysnc>c__async3.MoveNext () [0x0003e] in /Users/Tim/Projects/Xamarin/monotouch-samples/MonoCatalog-MonoDevelop/MonoDataSqlite.xib.cs:280
  --- End of inner exception stack trace ---
  at System.Threading.Tasks.Task.Wait (Int32 millisecondsTimeout, CancellationToken cancellationToken) [0x00048] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:651
  at System.Threading.Tasks.Task.Wait () [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:621
  at MonoCatalog.MonoDataSqliteController+ItemsDataSource+<GetKeyValuePairCell>c__async5.MoveNext () [0x0004e] in /Users/Tim/Projects/Xamarin/monotouch-samples/MonoCatalog-MonoDevelop/MonoDataSqlite.xib.cs:186
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[MonoTouch.UIKit.UITableViewCell].Start[<GetKeyValuePairCell>c__async5] (MonoCatalog.<GetKeyValuePairCell>c__async5& stateMachine) [0x0001b] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.CompilerServices/AsyncTaskMethodBuilder_T.cs:112
  at MonoCatalog.MonoDataSqliteController+ItemsDataSource.GetKeyValuePairCell (MonoTouch.UIKit.UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) [0x00000] in <filename unknown>:0
  at MonoCatalog.MonoDataSqliteController+ItemsDataSource.GetCell (MonoTouch.UIKit.UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) [0x00015] in /Users/Tim/Projects/Xamarin/monotouch-samples/MonoCatalog-MonoDevelop/MonoDataSqlite.xib.cs:95
  at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at MonoTouch.Application.Main (System.String[] args) [0x00004] in /Users/Tim/Projects/Xamarin/monotouch-samples/MonoCatalog-MonoDevelop/Main.cs:16

Expected Behavior: 

Method executes and parent method continues executing synchronously


Steps to reproduce:

1)  Run sample app
2)  Select the row "Mono.Data.Sqlite" - Exception occurs at like 186 in MonoDataSqlite.xib.cs

The problem occurs in the file MonoDataSqlite.xib.cs with the method call to WithCommandAsync starting at line 179.

WithCommandAysnc (async c => {
        c.CommandText = query;
        var r = await c.ExecuteReaderAsync ();
	while (await r.ReadAsync ()) {
	        key   = r ["Key"].ToString ();
	        value = r ["Value"].ToString ();
        }
}).Wait ();

using 'await WithCommandAsync (...}' instead works correctly.


Running on OS X 10.8.4
Xamarin Studio
Version 4.0.10 (build 3)
Installation UUID: 163f9b0a-f1be-4283-ae88-7b65a18efe5b
Runtime:
	Mono 3.1.2 ((no/4cb023e)
	GTK 2.24.20
	GTK# (2.12.0.0)
	Package version: 301020000

Apple Developer Tools
Xcode 4.6.3 (2068)
Build 4H1503

Xamarin.iOS
Version: 6.4.0.2 (Business Edition)
Hash: c9f7659
Branch: 
Build date: 2013-18-07 21:36:03-0400
Comment 1 Timothy Risi 2013-07-19 19:55:30 UTC
Internet died, will upload the example  when I get internet back
Comment 2 Timothy Risi 2013-07-20 19:46:35 UTC
Added test case url
Comment 3 Jakub Arnold 2013-10-13 12:46:09 UTC
I'm having the same issue when I mark one of my lambda callbacks with `async`.


            SearchForAddress(query, (async (IList<IAddressResult> results, Exception error) =>
            {
                if (error != null)
                {
                    tcs.SetException(error);
                }
                else
                {
                    tcs.SetResult(results);
                }
            }));

If I remove the async modifier everything works fine, but I get the runtime "Invalid IL code" with the async modifier.
Comment 4 Dillon Buchananq 2013-10-13 15:17:37 UTC
Great... I get something similar too when I do some BS like:

var sheet = new UIActionSheet();
sheet.Clicked = async (s, e) => {
  try
  {
    if (someval)
    {
       SomeNonAwaitTask();
    }
    else
    {
      await SomeAwaitTask();
    }
  }
  catch (exception)
  {
  }
};

I think the compiler miscompiles this because not all conditional paths result in some sort of await. If I simply add a 'await Task.Delay(1)' after the 'SomeNonAwait' the problem goes away. Either way, this is redonkulous because this is not the first 'async/await' issue I've encountered with Xamarin.iOS since the 7.0 release. It makes me nervous to release an App with any async/await methods in it for fear that another goofy exception, like this one, will cause the app to crash along with the ratings.
Comment 5 Marek Safar 2015-05-30 01:24:26 UTC
This should be fixed in mono master