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)

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


Attachments

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

Note You need to log in before you can comment on or make changes to this bug.