Bug 1101 - TPL allows to start cancelled task
Summary: TPL allows to start cancelled task
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: master
Hardware: PC Linux
: --- major
Target Milestone: Untriaged
Assignee: Jérémie Laval
URL:
Depends on:
Blocks:
 
Reported: 2011-09-28 10:10 UTC by Marek Safar
Modified: 2011-10-11 14:05 UTC (History)
2 users (show)

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

Description Marek Safar 2011-09-28 10:10:45 UTC
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main (string[] args)
		{
			var src = new CancellationTokenSource ();

			Task t = new Task (delegate { }, src.Token);
			src.Cancel ();

			t.Start ();
		}
	}
}


.NET

Unhandled Exception: System.InvalidOperationException: Start may not be called o
n a task that has completed.
   at System.Threading.Tasks.Task.Start(TaskScheduler scheduler)
   at System.Threading.Tasks.Task.Start()


Mono

nothing
Comment 1 Marek Safar 2011-09-28 10:24:46 UTC
Same applies to ContinueWith

Code extracted from Task nunit tests
Comment 2 Brandon Wallace 2011-09-28 11:46:53 UTC
I just was getting ready to post almost the same bug.  This unit tests passes using MS .NET but fails under Mono 2.10.x:

        [Test]
        public void WhenCancellationTokenIsSetAfterTaskCreatedButBeforeStartedItGoesToCanceledState()
        {
            var cts = new CancellationTokenSource();
            var task = new Task(() => { }, cts.Token);
            Assert.AreEqual(TaskStatus.Created, task.Status);
            cts.Cancel();
            Assert.True(task.IsCanceled, "Task should be canceled as soon as cancellation is requested.  Status=" + task.Status);
        }


Under Mono the final assertion fails because Status is still Created:

1) Test Failure : Shell.EP.Cdis.Utilities.Test.TaskExtensionsTest.WhenCancellationTokenIsSetAfterTaskCreatedButBeforeStartedItGoesToCanceledState
     Task should be canceled as soon as cancellation is requested.  Status=Created
  Expected: True
  But was:  False
Comment 3 Marek Safar 2011-09-28 13:02:57 UTC
Another test case exposing similar problem

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main (string[] args)
		{
			var tcs = new TaskCompletionSource<int> ();
			tcs.SetCanceled ();
			Console.WriteLine (tcs.Task.Result);
		}
	}
}

It throws on .net

Unhandled Exception: System.AggregateException: One or more errors occurred. ---
> System.Threading.Tasks.TaskCanceledException: A task was canceled.
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotifica
tion)
   at ConsoleApplication1.Program.Main(String[] args)
Comment 4 Jérémie Laval 2011-09-30 06:01:50 UTC
*** Bug 996 has been marked as a duplicate of this bug. ***
Comment 5 Marek Safar 2011-10-11 14:05:49 UTC
Fixed in master