Skip to content

await Task.WhenAny(new List<Task>()) should not Hang #55578

Closed

Description

@TonyValenti

Description

Run this code:

await Task.WhenAny(new List<Task>())

And it will hang your process.

This is a highly undesirable default behavior. It is not obvious this will hang and there is no easy way to detect that this was the cause.

Other information

I recommend one of the following adjustments:
If using Task.WhenAny() on an empty list/enumerable/array:
A) Throw a Sequence contains no elements exception.
OR
B) Immediately return Task.CompletedTask or Task.FromResult(default(T))

I am OK with either, but I think that A is the best option.

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jul 13, 2021
  2. removed
    untriagedNew issue has not been triaged by the area owner
    on Jul 13, 2021
  3. self-assigned this
    on Jul 13, 2021
  4. stephentoub commented on Jul 13, 2021

    @stephentoub
    Member

    This is a regression in .NET 6 caused by #38896. It throws an ArgumentException as expected on previous versions.

  5. added this to the 6.0.0 milestone on Jul 13, 2021
  6. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 13, 2021
  7. TonyValenti commented on Jul 13, 2021

    @TonyValenti
    Author

    @stephentoub Thanks!

  8. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 14, 2021
  9. ghost locked as resolved and limited conversation to collaborators on Aug 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions