semihokur / AsyncFixer

Advanced async/await Diagnostics and CodeFixes for C#
Apache License 2.0
165 stars 11 forks source link

Feature request: Prefer "await using" over "using" inside async methods #14

Open devax opened 3 years ago

devax commented 3 years ago

Thank you for this great extension!

I have realized that I often miss the presence of IAsyncDisposable interfaces and there is no warning that indicates this. Example:

static async Task Main(string[] args)
{
    using SqlConnection connection = new SqlConnection();
    // Do something with connection
    // ...
}

This could (should?) be converted to:

static async Task Main(string[] args)
{
    await using SqlConnection connection = new SqlConnection();
    // Do something with connection
    // ...
}

Would it be a good idea to add a warning or an information, that await using could be used instead of using?

semihokur commented 3 years ago

Thanks for the suggestion. As far as I know, most DisposeAsync implementations are not truly asynchronous. There is actually a perf penalty when using DisposeAsync for those IAsyncDisposable interfaces. Because they fake async using thread-pool threads, we waste at least one thread blocking on I/O.

It is not possible to understand whether DisposeAsync operation is truly asynchronous and does not fake it. Sometimes, this even depends on the parameters of the constructor: new FileStream(..., useAsync: true)