Bug 13318 - Task.Delay hangs with custom TaskScheduler
Summary: Task.Delay hangs with custom TaskScheduler
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: master
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
: 13788 ()
Depends on:
Reported: 2013-07-18 10:17 UTC by dispose835
Modified: 2013-08-16 11:31 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 dispose835 2013-07-18 10:17:18 UTC
As of Git commit b65deac48924a2f24b2931ea1a60bf48cea69370 Task.Delay does not correctly set Task.Status to WaitingToRun before calling scheduler.QueueTask.

TpScheduler still works because it doesn't check task.Status before executing, but custom TaskScheduler implementations can't do that, so the Delay task hangs.

The steps through the code are:

// scheduling should always be done through this method (or equivalent) to ensure Task is WaitingToRun when the scheduler sees it:

class Task
	internal void Schedule ()
		Status = TaskStatus.WaitingToRun;
		scheduler.QueueTask (this);

// but Task.Delay doesn't do that:

class Task
	public static Task Delay (int millisecondsDelay, CancellationToken cancellationToken)
		if (millisecondsDelay < -1)
			throw new ArgumentOutOfRangeException ("millisecondsDelay");

		var task = new Task (TaskActionInvoker.Delay, millisecondsDelay, cancellationToken, TaskCreationOptions.None, null, TaskConstants.Finished);
		task.SetupScheduler (TaskScheduler.Current);
		if (millisecondsDelay != Timeout.Infinite)
			task.scheduler.QueueTask (task); // *** Problem: task.Status != WaitingToRun - should probably be: task.Schedule();

		return task;

// TaskScheduler will only execute WaitingToRun tasks:

class TaskScheduler
	internal protected bool TryExecuteTask (Task task)
		if (task.IsCompleted)
			return false;

		if (task.Status == TaskStatus.WaitingToRun) { // *** Delay Task is still WaitingForActivation, so is not executed
			task.Execute ();
			if (task.WaitOnChildren ())
				task.Wait ();

			return true;

		return false;

// The problem doesn't appear using Mono's TpScheduler, which dodges it by skipping TryExecuteTask and going straight to task.Execute() which does not have the if (task.Status == TaskStatus.WaitingToRun) guard:

class TpScheduler
	protected internal override void QueueTask (Task task)
		if ((task.CreationOptions & TaskCreationOptions.LongRunning) != 0) {
			var thread = new Thread (l => ((Task)l).Execute ()) {
				IsBackground = true

			thread.Start (task);

		ThreadPool.UnsafeQueueUserWorkItem (callback, task);

// However a custom TaskScheduler can't use the internal-only Task.Execute(), so has to call TaskScheduler.TryExecuteTask, resulting in tasks created by Task.Delay never executing.

Replacing task.scheduler.QueueTask (task) with task.Schedule() should fix this.
Comment 1 Marek Safar 2013-07-20 03:19:32 UTC
Fixed in master
Comment 2 Marek Safar 2013-08-16 11:31:45 UTC
*** Bug 13788 has been marked as a duplicate of this bug. ***