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

Fix Task.WhenAny failure mode when passed ICollection of zero tasks #55580

Merged
merged 1 commit into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6305,8 +6305,14 @@ public static Task<Task> WhenAny(IEnumerable<Task> tasks)
return WhenAny(taskArray);
}

int count = taskCollection.Count;
if (count <= 0)
{
ThrowHelper.ThrowArgumentException(ExceptionResource.Task_MultiTaskContinuation_EmptyTaskList, ExceptionArgument.tasks);
}

int index = 0;
taskArray = new Task[taskCollection.Count];
taskArray = new Task[count];
foreach (Task task in tasks)
{
if (task == null) ThrowHelper.ThrowArgumentException(ExceptionResource.Task_MultiTaskContinuation_NullTask, ExceptionArgument.tasks);
Expand Down
14 changes: 14 additions & 0 deletions src/libraries/System.Threading.Tasks/tests/MethodCoverage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@ public static void Task_WhenAny_TwoTasks_InvalidArgs_Throws()
AssertExtensions.Throws<ArgumentNullException>("task2", () => Task.WhenAny(Task.FromResult(2), null));
}

[Fact]
public static void Task_WhenAny_NoTasks_Throws()
{
AssertExtensions.Throws<ArgumentException>("tasks", () => { Task.WhenAny(new Task[0]); });
AssertExtensions.Throws<ArgumentException>("tasks", () => { Task.WhenAny(new List<Task>()); });
AssertExtensions.Throws<ArgumentException>("tasks", () => { Task.WhenAny(EmptyIterator<Task>()); });

AssertExtensions.Throws<ArgumentException>("tasks", () => { Task.WhenAny(new Task<int>[0]); });
AssertExtensions.Throws<ArgumentException>("tasks", () => { Task.WhenAny(new List<Task<int>>()); });
AssertExtensions.Throws<ArgumentException>("tasks", () => { Task.WhenAny(EmptyIterator<Task<int>>()); });

static IEnumerable<T> EmptyIterator<T>() { yield break; }
}

[Fact]
public static async Task Task_WhenAny_TwoTasks_OnePreCompleted()
{
Expand Down