Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StartAsync breaks WhenAll #1579

Open
CEbbinghaus opened this issue Jul 3, 2024 · 0 comments
Open

StartAsync breaks WhenAll #1579

CEbbinghaus opened this issue Jul 3, 2024 · 0 comments
Labels
bug Something isn't working needs triage

Comments

@CEbbinghaus
Copy link

CEbbinghaus commented Jul 3, 2024

Describe the bug
When returning a Task from within the AnsiConsole we don't receive back the original task but rather the task of the AnsiConsole. This is fine in 99.99% of cases except for those in which awaiting a task behaves differently to what one would want to do. (e.g Task.WhenAll)

This is due to

return await progress.StartAsync(async ctx =>
{
var statusContext = new StatusContext(ctx, ctx.AddTask(status), spinnerColumn);
return await func(statusContext).ConfigureAwait(false);
}).ConfigureAwait(false);

which awaits the resulting task causing the behavior of WhenAll to change

To Reproduce

using System;
using System.Threading;
using Spectre.Console;

async Task DoThing() {
    await Task.Delay(100);
    throw new ApplicationException("Something went wrong");
}

async Task DoOtherThing() {
    await Task.Delay(1000);
    throw new ApplicationException("Something other went wrong");
} 

var result = AnsiConsole.Status().StartAsync("Running Test", context =>
{
    return Task.WhenAll(DoThing(), DoOtherThing());
});

try
{
    await result;
}
catch (Exception ex)
{
    AnsiConsole.MarkupLine($"[red]{result.Exception.Message}[/]");
    return;
}

Expected behavior
StartAsync to behave entirely transparently and not change the behavior of the task within.

The expected output of the above repro would be:
One or more errors occurred. (Something went wrong) (Something other went wrong)
the actual result however is:
One or more errors occurred. (Something went wrong)

Additional context
This relates to a design decision which the dotnet team have made making WhenAll not return the AggregateException when awaited. Instead one must capture the original task and check its exception to get the proper AggregateException.

More Context: https://stackoverflow.com/questions/12007781/why-doesnt-await-on-task-whenall-throw-an-aggregateexception


Please upvote 👍 this issue if you are interested in it.

@CEbbinghaus CEbbinghaus added bug Something isn't working needs triage labels Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
Status: Todo 🕑
Development

No branches or pull requests

1 participant