Bug 54322 - await in catch-block inside a loop causes the same exception to be caught multiple times
Summary: await in catch-block inside a loop causes the same exception to be caught mul...
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: 4.8.0 (C9)
Hardware: PC All
: --- normal
Target Milestone: ---
Assignee: Marek Safar
Depends on:
Reported: 2017-03-31 08:39 UTC by Maxim Mikityanskiy
Modified: 2017-05-30 09:48 UTC (History)
2 users (show)

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

Test case (496 bytes, text/plain)
2017-03-31 08:39 UTC, Maxim Mikityanskiy

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 Maxim Mikityanskiy 2017-03-31 08:39:05 UTC
Created attachment 21086 [details]
Test case

Look at the attached source file for an example of a program that causes the bug.

Expected result:

On the first iteration of the loop (i == 0), Func throws an exception that is caught by the catch-block, "0 caught" is printed. On the consequent iterations (i > 0), Func just returns true and doesn't throw any exception, so only "1 result True", "2 result True" etc should be printed.

Actual result:

On the first iteration of the loop (i == 0), Func throws an exception, "0 caught" is printed, just as before. But on the consequent iterations (i > 0), BOTH try-block and catch-block are executed completely. It prints:

0 caught
1 result True
1 caught
2 result True
2 caught
3 result True
3 caught
4 result True
4 caught

However, the following text should be printed:

0 caught
1 result True
2 result True
3 result True
4 result True

From the actual output it can be concluded that on the second and consequent iterations (i > 0), try-block executes completely (Func returns the result and it's printed to the terminal), but then, for some unknown reason, catch-block also executes ignoring the fact that no exception was thrown on this iteration and the exception thrown on the first iteration is already handled.

Additional comments:

If you comment out "await Nothing();" line, everything works as expected, so I think await in a catch-block in a loop triggers the bug.
Comment 1 Maxim Mikityanskiy 2017-03-31 08:40:04 UTC
Link to Ideone where the bug can be reproduced: https://ideone.com/IyW8eS
Comment 2 Marek Safar 2017-05-30 09:48:49 UTC
Fixed in master and 2017-04 branch