title: Locking issue with LimitedConcurrencyLevelTaskScheduler and aync/await
body: <p>I'm struggling to understand what's happening in this simple program.</p>
  
  <p>In the example below I have a task factory that uses the  LimitedConcurrencyLevelTaskScheduler from ParallelExtensionsExtras with maxDegreeOfParallelism set to 2. </p>
  
  <p>I then start 2 tasks that each call an async method (e.g. an async Http request), then gets the awaiter and the result of the completed task.</p>
  
  <p>The problem seem to be that <code>Task.Delay(2000)</code> never completes. If I set maxDegreeOfParallelism to 3 (or greater) it completes. But with maxDegreeOfParallelism = 2 (or less) my guess is that there is no thread available to complete the task. Why is that?</p>
  
  <p>It seems to be related to async/await since if I remove it and simply do <code>Task.Delay(2000).GetAwaiter().GetResult()</code> in <code>DoWork</code> it works perfectly. Does async/await somehow use the parent task's task scheduler, or how is it connected?</p>
  
  <pre><code>using System;
  using System.Linq;
  using System.Threading.Tasks;
  using System.Threading.Tasks.Schedulers;
  
  namespace LimitedConcurrency
  {
      class Program
      {
          static void Main(string[] args)
          {
              var test = new TaskSchedulerTest();
              test.Run();
          }
      }
  
      class TaskSchedulerTest
      {
          public void Run()
          {
              var scheduler = new LimitedConcurrencyLevelTaskScheduler(2);
              var taskFactory = new TaskFactory(scheduler);
  
              var tasks = Enumerable.Range(1, 2).Select(id =&gt; taskFactory.StartNew(() =&gt; DoWork(id)));
              Task.WaitAll(tasks.ToArray());
          }
  
          private void DoWork(int id)
          {
              Console.WriteLine($"Starting Work {id}");
              HttpClientGetAsync().GetAwaiter().GetResult();
              Console.WriteLine($"Finished Work {id}");
          }
  
          async Task HttpClientGetAsync()
          {
              await Task.Delay(2000);
          }
      }
  }
  </code></pre>
  
  <p>Thanks in advance for any help</p>