Search code examples
c#multithreadingasynchronousasync-awaitsynchronizationcontext

Why does SynchronizationContext.Post() get called only once when using multiple awaits?


Consider the following example:

async Task DoWork()
{
    await Task.Run(() =>
    {
        for (int i = 0; i < 25; i++)
        {
            Console.WriteLine("Task run 1: " + Thread.CurrentThread.ManagedThreadId);
        }
    });

    // The SynchronizationContext.Post() gets called after Run 1 and before Run 2

    await Task.Run(() =>
    {
        for (int i = 0; i < 25; i++)
        {
            Console.WriteLine("Task run 2: " + Thread.CurrentThread.ManagedThreadId);
        }
    });

    // I expect it to run after Run 2 and before Run 3 as well but it doesn't

    await Task.Run(() =>
    {
        for (int i = 0; i < 25; i++)
        {
            Console.WriteLine("Task run 3: " + Thread.CurrentThread.ManagedThreadId);
        }
    });
}

I would expect a call to SynchronizationContext.Post() to be made every time an await operation ends but after overriding the Post() like this

public class MySynchronizationContext
{
  public override void Post(SendOrPostCallback d, object? state)
  {
      Console.WriteLine("Continuation: " + Thread.CurrentThread.ManagedThreadId);
      base.Post(d, state);
  }
}

Installed like this at the very start of Main()

SynchronizationContext.SetSynchronizationContext(new MySynchronizationContext());

It only prints the message once, after the first Run() is completed.

I assumed that's because Task.Run() may detect that it's being called on a threadpool thread and just reuse the current thread but that seems not to be the case because some of my tests resulted in Run 2 and Run 3 running on different threads.

Why does the completion of an awaited Task only runs after the first await?


Solution

  • The SynchronizationContext.SetSynchronizationContext method installs the supplied SynchronizationContext on the current thread. In order for the same SynchronizationContext to be captured and reused by subsequent awaits, the implementation of the SynchronizationContext must ensure that the continuation is invoked on the original thread, or that it installs itself on any other thread that it uses for invoking the continuation.

    Your implementation (MySynchronizationContext) doesn't do that. It just delegates the Post call to the base.Post, which invokes the continuation on the ThreadPool. The MySynchronizationContext instance is not installed on any of the ThreadPool threads, so the second await finds nothing to capture, and so the second continuation is invoked on whatever thread the Task.Run method completed, which is also a ThreadPool thread. So essentially you get the same behavior that you would get by using a properly implemented SynchronizationContext, like Stephen Cleary's AsyncContext, and configuring the first await with ConfigureAwait(false).