runtime: Unable to run asyncronous tasks on the foreground thread by any means

dotnet/runtime#18449 was closed as unactionalbe but the root issue exists with no resolution.

If a threadpool thread (think any MVC thread) calls into a library that eventually calls Task.Result; the entire application is subject to deadlock.

The general rule of pushing async down by replacing Object.MethodAsync().Result with await Object.Method() and adding async to the caller has finally failed by trying to push into interface IEnumerable<out T> and rewrite all Linq methods.

Issue described here as well: https://what.thedailywtf.com/topic/22986/async-await-with-ienumerable-in-c

I’m not sure what exactly the resolution should be, but this problem needs a real resolution. Random deadlocking by thread pool exhaustion is not appropriate.

It really feels like the solution ought to be a way to actually say “run this async method on this thread”.

I have constructed a demonstration that deliberately crushes the threadpool size to 1 to make reproduction trivial.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 18 (10 by maintainers)

Most upvoted comments

In your particular example, it seems like the upcoming IAsyncEnumerable would benefit you here since you could now write this:

    private static async IAsyncEnumerable<object> DownloadObjects()
    {
        var g = await Generator.Start();
        while (await g.IsNextObject())
            yield return await g.GetSomeObject();
    }

And then use

 await foreach(var objs in DownloadObjects())

Since this hasn’t shipped yet, Ix.NET is something you can use now to have async enumerables. That would let you go “async” all the way down and avoid blocking behavior.