codeanalyzers icon indicating copy to clipboard operation
codeanalyzers copied to clipboard

U2U1006: "Await tasks correctly" is it really warning?

Open picolino opened this issue 6 years ago • 0 comments

Warning: U2U1006: Await tasks correctly

I think you know about exception handling with asynchronous state machine using but just in case: Source code:

private async Task<int> TaskWithAwait() { return await TaskWithException(); } // Use asynchronous state machine

private Task<int> TaskWithoutAwait() { return TaskWithException(); } // Return task directly

private async Task<int> TaskWithException() { throw new Exception("Test exception"); } // Throw exception

public async Task WithAwait()
{
    try
    {
        var result = await TaskWithAwait();
    }
    catch (Exception e)
    {
        // Exception (with call stack):
        // System.Exception : Test exception
        // at Playground.TaskWithException() in Playground\Test.cs:line 49
        // at Playground.TaskWithAwait() in Playground\Test.cs:line 39
        // at Playground.WithAwait() in Playground\Test.cs:line 28
    }
}

public async Task WithoutAwait()
{
    try
    {
        var result = await TaskWithoutAwait();
    }
    catch (Exception e)
    {
        // Exception (with call stack):
        // System.Exception : Test exception
        // at Playground.TaskWithException() in Playground\Test.cs:line 49
        // at Playground.WithoutAwait() in Playground\Test.cs:line 14
    }
}

As you can see, information in call stack in WithoutAwait() method was not fully (TaskWithException() method not mentioned).

I think this must be at least not warning, but just info message with more detailed explanation what user will lose if he refuse of asynchronous state machine. If user has many levels of task requests and will fix this warning - he can made debug-hell code.

picolino avatar Nov 12 '19 08:11 picolino