In a blog post, Microsoft's Sergey Tepliakov says:
you should always provide TaskCreationOptions.RunContinuationsAsynchronously when creating TaskCompletionSource instances.
But in that article, unless I misunderstood it, he also says that all await
continuations basically act the same way as a TaskCompletionSource
without TaskCreationOptions.RunContinuationsAsynchronously
. So this would mean that async
and await
are also inherently dangerous and shouldn't be used, unless await Task.Yield()
is used as well.
(edit: I did misunderstand it. He's saying that await
continuing synchronously is the cause of the problematic SetResult
behaviour. await Task.Yield()
is recommended as a client-side workaround if the task creation can't be controlled at the source. Unless I misunderstood something again.)
I conclude that there must be specific circumstances that make synchronous continuations dangerous, and apparently that those circumstances are common for TaskCompletionSource
use cases. What are those dangerous circumstances?