AsyncFixer icon indicating copy to clipboard operation
AsyncFixer copied to clipboard

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

Open devax opened this issue 4 years ago • 1 comments

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?

devax avatar Feb 14 '21 06:02 devax

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)

semihokur avatar Feb 17 '21 00:02 semihokur