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...
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# (show other bugs)
Version: 4.8.0 (C9)
Hardware: PC All
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2017-03-31 08:39 UTC by Maxim Mikityanskiy
Modified: 2017-05-30 09:48 UTC (History)
2 users (show)

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


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

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

Note You need to log in before you can comment on or make changes to this bug.