8000 `Merge` masks original exception with an `ArgumentException` about no tasks · Issue #1056 · morelinq/MoreLINQ · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Merge masks original exception with an ArgumentException about no tasks #1056

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

Closed
atifaziz opened this issue Jan 19, 2024 · 0 comments · Fixed by #1059
Closed

Merge masks original exception with an ArgumentException about no tasks #1056

atifaziz opened this issue Jan 19, 2024 · 0 comments · Fixed by #1059
Assignees
Labels
Milestone

Comments

@atifaziz
Copy link
Member

Code to reproduce:

var items =
    Enumerable.Range(1, 3)
              .Select(n => Source(n, n % 2 is 0))
              .Merge();

await foreach (var item in items)
    Console.WriteLine(item);

async IAsyncEnumerable<int> Source(int n, bool fail = false)
{
    await Task.Delay(TimeSpan.FromSeconds(n));
    yield return n;
    if (fail)
        throw new();
}

When run, it will output:

1
2
System.ArgumentException: The tasks argument contains no tasks. (Parameter 'tasks')
   at System.Threading.Tasks.Task.WhenAny[TTask](ReadOnlySpan`1 tasks)
   at System.Threading.Tasks.Task.WhenAny[TTask](IEnumerable`1 tasks)
   at System.Threading.Tasks.Task.WhenAny[TResult](IEnumerable`1 tasks)
   at MoreLinq.Experimental.Async.ExperimentalEnumerable.<>c__DisplayClass1_0`1.<<Merge>g__Async|0>d.MoveNext()
--- End of stack trace from previous location ---
   at MoreLinq.Experimental.Async.ExperimentalEnumerable.<>c__DisplayClass1_0`1.<<Merge>g__Async|0>d.System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult(Int16 token)
   at UserQuery.Main() in C:\Users\johndoe\AppData\Local\Temp\LINQPad8\_hiuxehhq\uljkai\LINQPadQuery:line 7
   at UserQuery.Main() in C:\Users\johndoe\AppData\Local\Temp\LINQPad8\_hiuxehhq\uljkai\LINQPadQuery:line 7

It throws ArgumentException when it should have thrown Exception.

The problem seems to be due to the following clean-up loop, which can call Task.WhenAny with zero tasks:

if (pendingTaskList is { Count: > 0 })
{
while (await Task.WhenAny(pendingTaskList)
.ConfigureAwait(false) is { } completedTask)
{
_ = pendingTaskList.Remove(completedTask);
}
}

@atifaziz atifaziz added the bug label Jan 19, 2024
@atifaziz atifaziz added this to the vNext milestone Jan 19, 2024
@atifaziz atifaziz self-assigned this Jan 19, 2024
@atifaziz atifaziz modified the milestones: vNext, 4.2.0 Jan 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant
0