Bug 39866 - Await + finally produces exe that causes InvalidProgramException on Windows
Summary: Await + finally produces exe that causes InvalidProgramException on Windows
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: 4.0.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Marek Safar
Depends on:
Reported: 2016-03-23 14:59 UTC by c
Modified: 2016-04-09 07:51 UTC (History)
1 user (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 c 2016-03-23 14:59:55 UTC
A test case that can reproduce this is found at https://github.com/Unarmed/mono-invalidprogram

Building the supplied code on mono and then running it on Windows 8.1 x64 causes a crash. Exception details below.

To reproduce do:
mcs Program.cs

and reproduce crash with

If this is instead run with mono (as in "mono Program.exe") then the program will print the expected output "If you see this then everything is OK!" and exit cleanly. It will do so on all platforms as far as I can tell.

If I instead run the exe without mono (as in just "Program.exe") then at least on my Windows 8.1 x64 I instead see:
Unhandled Exception: System.InvalidProgramException: Common Language Runtime detected an invalid program.
   at invalid_program.Program.<DoStuff>c__async0.MoveNext()
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine)
   at invalid_program.Program.DoStuff()
   at invalid_program.Program.Main(String[] args)

I have tried building with mono on windows and from the mono: docker image, both with the same results. 
I have not attempted to reproduce crash on more windows platforms.
I have .Net 4, 4.5 and 4.6 installed on the machine where the crash was found.
Rebuilding through Visual Studio 2015 produces an exe that can be run both with mono and without it.
Comment 1 Marek Safar 2016-04-09 07:51:43 UTC
Fixed in master