Don't marshal async continuations to the captured context #19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently,
async
/await
continuations andTask.ContinueWith(...)
continuations are marshalled back to the captured context (TaskScheduler
orSynchronizationContext
) as is the default behavior with these tools. I believe there is no reason for this in this library. An example of this is if I use this library in an ASP.NET Core web project currently, this library will post its continuations to the web framework's single-threaded UI synchronization context, resulting in worse performance and even deadlocks if asynchronous code is blocked on.This resolves the issue by 1) using
Task.ConfigureAwait(false)
inasync
/await
code to instruct the async state machines to schedule continuations onto the default thread pool and 2) passingTaskScheduler.Default
toTask.ContinueWith(...)
calls to ensure the continuations are run on the default thread pool.Notably,
Task.Run(...)
schedules work onto the default thread pool by default, so no changes are needed there. Only continuations are subject to this "marshalling onto the captured context" behavior by default.Resolves #17.