ConfigureAwaitChecker icon indicating copy to clipboard operation
ConfigureAwaitChecker copied to clipboard

"Intermittent" CAC001 on await using code

Open martyt opened this issue 2 years ago • 1 comments

Here's a minimal .NET 6 console app repro - using ConfigureAwaitChecker.Analyzer v5.0.0.1, in VS2022 Professional v17.2.5


Console.WriteLine("Hello, World!");

var y = await DoSomethingElseAsync().ConfigureAwait(false);

async Task<int> DoSomethingElseAsync()
{
    var foo = new Foo();
    await using var s = await foo.GetStreamAsync().ConfigureAwait(false);

    return 0;
}

public class Foo
{
    public async Task<Stream> GetStreamAsync()
    {
        return await this.AllocateStreamAsync().ConfigureAwait(false);
    }

    private async Task<Stream> AllocateStreamAsync()
    {
        return await Task.FromResult(new MemoryStream()).ConfigureAwait(false);
    }
}

I'm getting an "intermittent" CAC001 error on the await using call at line 8. And by "intermittent" I mean sometimes it's there, sometimes it's not - see the following screen captures: image After doing nothing but deleting the blank line after Console.WriteLine() I get this image And then sometimes it disappears completely and comes back seemingly at random based on unrelated code changes.

martyt avatar Jun 28 '22 20:06 martyt

Not sure why it's intermittent, but I'll check how's the analyzer called and how the tree looks like.

The "fixed" code should be something like await using var s = (await foo.GetStreamAsync().ConfigureAwait(false)).ConfigureAwait(false); (or maybe rather var x = await foo.GetStreamAsync().ConfigureAwait(false); await using var s = x.ConfigureAwait(false);, but that's related to #28).

Note to self: Add tests for await using var x = (await foo.FooAsync().ConfigureAwait(false)).ConfigureAwait(false);.

cincuranet avatar Jun 29 '22 06:06 cincuranet