Bug 20849 - Nested async blocks result in an invalid AsyncStateMachineAttribute metadata parameter
Summary: Nested async blocks result in an invalid AsyncStateMachineAttribute metadata ...
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: 3.4.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Marek Safar
: 21812 ()
Depends on:
Reported: 2014-06-24 11:40 UTC by bugzilla
Modified: 2014-08-06 01:54 UTC (History)
2 users (show)

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:

Description bugzilla 2014-06-24 11:40:56 UTC
I found this bug while running a tool which was attempting to load all attribute metadata from an assembly.  The tool worked correctly with assemblies produced by Microsoft's C# compiler but failed on the same code produced by Mono's C# compiler.  It looks like the AsyncStateMachineAttribute produced by the Mono compiler for an async method which contains a nested async delegate references a nonexistent type.

This bug can be reproduced by compiling the following code:

using System;
using System.Threading.Tasks;

namespace AsyncAttributeBug
	class TestClass
		public async Task Test()
			Action a = async delegate {
				await Task.Yield();
			await Task.Yield();

For this program, the compiler generates a nested type of TestClass called <Test>c__async0, and within it a static method called "<>m__0()".  It also generates a double-tested type <Test>c__async1 within <Test>c__async0.  However, the static method "<>m__0()" is attributed with this custom attribute:

.custom instance void class [mscorlib]System.Runtime.CompilerServices.AsyncStateMachineAttribute::'.ctor'(class [mscorlib]System.Type) =  (
		01 00 2B 41 73 79 6E 63 41 74 74 72 69 62 75 74   // ..+AsyncAttribut
		65 42 75 67 2E 54 65 73 74 43 6C 61 73 73 2B 3C   // eBug.TestClass+<
		54 65 73 74 3E 63 5F 5F 61 73 79 6E 63 31 00 00 ) // Test>c__async1..

As you can see, the attribute parameter is ignoring the fact that <Test>c__async1 is nested within <Test>c__async0 and referencing it as though it were directly nested within TestClass itself.
Comment 1 Marek Safar 2014-06-26 11:20:46 UTC
Fixed in master
Comment 2 Marek Safar 2014-08-06 01:52:37 UTC
*** Bug 21808 has been marked as a duplicate of this bug. ***
Comment 3 Marek Safar 2014-08-06 01:54:18 UTC
*** Bug 21812 has been marked as a duplicate of this bug. ***