Bug 14585 - Incorrect Task.WhenAny() behavior with Task.Delay
Summary: Incorrect Task.WhenAny() behavior with Task.Delay
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
Depends on:
Reported: 2013-09-10 04:44 UTC by Shay Rojansky
Modified: 2013-10-08 04:16 UTC (History)
4 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 Shay Rojansky 2013-09-10 04:44:05 UTC
Consider the following:

var delay1 = Task.Delay(50);
var delay2 = Task.Delay(25);
Task.WhenAny(new[] { delay1, delay2 }).Wait();
Console.WriteLine("State for 1: " + delay1.Status);
Console.WriteLine("State for 2: " + delay2.Status);

The expected/correct output, which I get on MS, is RanToCompletion for delay2 and something else for delay1 (WaitingForActivation on MS).

On the latest mono trunk (3.2.3, master/6531ed7) running on Linux I get:

State for 1: RanToCompletion
State for 2: Running

It clearly doesn't make sense for a 50ms delay task to finish before a 25ms delay task...
Comment 1 Rodrigo Kumpera 2013-09-12 10:43:35 UTC
The 50/25 difference is due to timer resolution in how mono implements WaitAny.

You change from 50 to 5000 you get the expected behavior.

Timeout resolution is platform dependent and should not be relied upon.
Comment 2 Shay Rojansky 2013-09-15 14:46:37 UTC
Just making sure here...

I changed the numbers to 700ms and 250ms, and the 700ms delay task still finishes before the 250ms one.

So timer resolution may be platform-dependent when we're in the milliseconds, but we're talking about almost half a second here...?
Comment 3 Rodrigo Kumpera 2013-09-16 12:02:53 UTC
I believe we need to special case Task.Delay otherwise resolution will be really crappy.
Comment 4 Marek Safar 2013-09-16 12:36:05 UTC
Yeah, we probably need ThreadPool timer dedicated slot
Comment 5 Marek Safar 2013-09-24 13:41:59 UTC
This seems to be fixed by the latest ThreadPool changes
Comment 6 Rodrigo Kumpera 2013-09-24 14:03:33 UTC
No, the latest ThreadPool changes just hides this problem further.

Yo'll have the same problem if you wait for 64 tasks instead.
Comment 7 Marek Safar 2013-10-08 04:16:52 UTC
Fixed in master