Bug 54322

Summary: await in catch-block inside a loop causes the same exception to be caught multiple times
Product: [Mono] Compilers Reporter: Maxim Mikityanskiy <maxtram95>
Component: C#Assignee: Marek Safar <masafa>
Severity: normal CC: maxtram95, mono-bugs+mono
Priority: ---    
Version: 4.8.0 (C9)   
Target Milestone: ---   
Hardware: PC   
OS: All   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Test case

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