Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Task.ConfigureAwait(false) on the last method line affect anything?

I understand that calling ConfigureAwait(false) on a task that is being awaited will sometimes have a performance benefit, as it prevents an unnecessary return to the original SynchroniZationContext.

For example:

async Task Something()
{
   // Let's say I'm on the UI context
   //...
   await AnotherTask.ConfigureAwait(false);

   // Code here is no longer running on the UI context.
   // It runs in a thread pool synchronization context (i.e. null).
}

My question is this: If the task call is on the last line of the method, and we skip the ConfigureAwait(false), is the compiler smart enough to prevent an unnecessary return to the original context?

async Task Something()
{
   // Let's say I'm on the UI context
   //...
   await AnotherTask; // Dropped -> .ConfigureAwait(false);        
}

Will there be a performance penalty or potential deadlock possible here, even though there is nothing in the method after the await call?

like image 837
Paymon Avatar asked Mar 10 '23 01:03

Paymon


1 Answers

is the compiler smart enough to prevent an unnecessary return to the original context?

Not yet.

Will there be a performance penalty or potential deadlock possible here, even though there is nothing in the method after the await call?

Yes.

like image 197
Stephen Cleary Avatar answered Mar 13 '23 01:03

Stephen Cleary