Bug 56578 - Task.Run() blocks other Task.Run() from running
Summary: Task.Run() blocks other Task.Run() from running
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: General (show other bugs)
Version: 4.8.0 (C9)
Hardware: Other Linux
: --- normal
Target Milestone: ---
Assignee: Zoltan Varga
URL:
Depends on:
Blocks:
 
Reported: 2017-05-17 20:59 UTC by jake.a.baldwin
Modified: 2017-10-06 22:29 UTC (History)
3 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 jake.a.baldwin 2017-05-17 20:59:43 UTC
There seems to be a threading issue when trying to run multiple tasks with Task.Run(...) at the same time. My co-worker was finally able to come up with a short code snippet to reproduce this issue. This code snippet runs as expected in Windows (not mono for Windows, just natively in Windows) but behaves strangely when run with mono in Linux (Debian 8.7)

Natively in Windows task 2 starts and runs all the way through correctly, sits and waits for the user to press Enter, then task 1 finishes. In Linux, however, Task 2 will not run until Task 1 finishes. Apparently Task 2 is started but it doesn't run until an Enter press terminates Task 1; then Task 2 runs all the way through.

class Program
{
  static void Main(string[] args)
  {
    bool quit = false;

    Console.WriteLine($"Beginning of main. Thread ID: {Thread.CurrentThread.ManagedThreadId}");

    Console.WriteLine($"Starting task 1 from Thread ID: {Thread.CurrentThread.ManagedThreadId}");
    Task t1 = Task.Run(() => 
    {
      Console.WriteLine($"Beginning task 1. Thread ID: {Thread.CurrentThread.ManagedThreadId}");
      do
      {
        Task.Run((Action)Act).Wait();
        Thread.Sleep(10);
        } while (!quit);
        Console.WriteLine($"End of task 1. Thread ID: {Thread.CurrentThread.ManagedThreadId}");
    });

    Console.WriteLine($"Started task 1 SUCCESSFULLY from Thread ID: {Thread.CurrentThread.ManagedThreadId}");

    Console.WriteLine($"Starting task 2 from Thread ID: {Thread.CurrentThread.ManagedThreadId}");
    Task t2 = Task.Run(() =>
    {
      Console.WriteLine($"Beginning task 2. Thread ID: {Thread.CurrentThread.ManagedThreadId}");
      Console.WriteLine($"Ending task 2. Thread ID: {Thread.CurrentThread.ManagedThreadId}");
    });

    Console.WriteLine($"Started task 2 SUCCESSFULLY from Thread ID: {Thread.CurrentThread.ManagedThreadId}");

    Console.ReadLine();
    quit = true;
    Task.WaitAll(t1, t2);
  }

  static void Act()
  {
    Thread.Sleep(0);
  }
}
Comment 1 Ludovic Henry 2017-10-06 22:29:48 UTC
I cannot reproduce with Mono 5.2.0.215 on Ubuntu 16.04 64bits. Please reopen if you can reproduce with latest version of Mono. Thank you