Force secondary await continuations to run asynchronously (dotnet/coreclr#25280)
authorStephen Toub <stoub@microsoft.com>
Mon, 24 Jun 2019 13:27:47 +0000 (09:27 -0400)
committerGitHub <noreply@github.com>
Mon, 24 Jun 2019 13:27:47 +0000 (09:27 -0400)
commitd27701617ae734904a6bb7e31400d30ec857b727
treee195554fb903c6697b59c1282e3ed90ca26881b2
parent1dfa0845beb9f9a536035360a8843df57e175f48
Force secondary await continuations to run asynchronously (dotnet/coreclr#25280)

* Force secondary await continuations to run asynchronously

For performance reasons, await continuations have been invoked synchronously, meaning they're invoked as part of the antecedent task's completion (as long as that task allows it, as long as there's sufficient stack space, etc.)  This generally works out well in the case where there's a single await continuation, which is far and away the common case.  However, it can cause problems in situations where there are multiple await continuations, as those continuations will end up being serialized, which can lead to slowdowns and deadlocks in niche situations.  To address that, this commit backs off a bit.  The first await continuation is still invoked synchronously, but subsequent await continuations are invoked asynchronously, such that they are not blocked by a previously registered await continuation.

* Fix nits

Commit migrated from https://github.com/dotnet/coreclr/commit/d58a283f2a69da1509b8d76dfec0959b93e061c8
src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs