Bug 892 - TaskCompletionSource.Task.Result does not block until result is ready
Summary: TaskCompletionSource.Task.Result does not block until result is ready
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Core ()
Version: 2.10.x
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Jérémie Laval
URL:
Depends on:
Blocks:
 
Reported: 2011-09-19 07:22 UTC by Brandon Wallace
Modified: 2011-09-19 12:37 UTC (History)
2 users (show)

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


Attachments
VS2010 project exhibiting the problem. (2.94 KB, application/octet-stream)
2011-09-19 07:22 UTC, Brandon Wallace
Details


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:
Status:
RESOLVED FIXED

Description Brandon Wallace 2011-09-19 07:22:33 UTC
Created attachment 418 [details]
VS2010 project exhibiting the problem.

When I create a task completion source:

            Task<string> actualTask = ...
            var tcs = new TaskCompletionSource<string>();
            actualTask.ContinueWith(t => tcs.SetResult(t.Result), TaskContinuationOptions.ExecuteSynchronously);
            var observedTask = tcs.Task;

And then get the observedTask.Result property, the get request does not block until the result is ready.  If the task is not complete, I get the default value for the TResult type (null for reference types and 0 for integer types).

According to http://msdn.microsoft.com/en-us/library/dd321468.aspx
Task(TResult).Result should block until the result is ready.

On Windows, the attached program correctly tells me the result of my task is "Hello World".  On Mono, it incorrectly tells me the result of my task is null.

Calling observedTask.Wait() before accessing the Result property works around the issue.

When working correctly, the program should produce this output:

The answer is 'Hello World' (it should be 'Hello World')
IsCompleted = 'True' (it should be 'True').
TaskStatus = 'RanToCompletion' (it should be 'RanToCompletion')
Waiting for task to complete.  This should not be required!
The answer is 'Hello World' (it should be 'Hello World')
IsCompleted = 'True' (it should be 'True').
TaskStatus = 'RanToCompletion' (it should be 'RanToCompletion')



On Mono, it instead produces this output:

The answer is '(null)' (it should be 'Hello World')
IsCompleted = 'False' (it should be 'True').
TaskStatus = 'WaitingForActivation' (it should be 'RanToCompletion')
Waiting for task to complete.  This should not be required!
The answer is 'Hello World' (it should be 'Hello World')
IsCompleted = 'True' (it should be 'True').
TaskStatus = 'RanToCompletion' (it should be 'RanToCompletion')
Comment 1 Jérémie Laval 2011-09-19 12:35:09 UTC
Hey, problem fixed in master and mono-2-10 branch.

Thanks for the report!
Comment 2 Brandon Wallace 2011-09-19 12:37:40 UTC
thanks for the quick fix.