Skip to content

Incorrect recommendation to use ConfigureAwait(false) in synchronous waitΒ #712

@KalleOlaviNiemitalo

Description

@KalleOlaviNiemitalo

https://learn.microsoft.com/en-us/entra/msal/dotnet/how-to/synchronous-programming#watch-out-for-exceptions-and-deadlocks recommends using ConfigureAwait(false) when synchronously waiting an asynchronous operation:

// make sure to have ConfigureAwait(false) to avoid any potential deadlocks
var authResult = builder.ExecuteAsync()
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();

However, .ConfigureAwait(false) does not help avoid deadlocks when it is used with .GetAwaiter().GetResult().

In the Task<TResult> class, the ConfigureAwait(bool continueOnCapturedContext) method returns a ConfiguredTaskAwaitable<TResult> value, whose GetAwaiter() method returns a ConfiguredTaskAwaitable<TResult>.ConfiguredTaskAwaiter value, whose m_options field depends on the bool continueOnCapturedContext parameter. The m_options field would then affect the bool IsCompleted property, the void OnCompleted(Action continuation) method, or the void UnsafeOnCompleted(Action continuation) method; but it does not affect the TResult GetResult() method, which is used in the sample.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions