dotnet / dotnet/reactive

All IAsyncEnumerable LINQ operators throws OperationCanceledException in GetAsyncEnumerator when token is already canceled.

Open
#1,207 5 comments 0 reactions 0 assignees View on GitHub
[area] Ix
Dominant language
C#
Stars
7.2k
Forks
798
PR merge metrics
No merged PRs in 30d

Description

Compiler generated enumerator does not throws exception on `GetAsyncEnumerator`.
But all Async-LINQ operator throws OperationCanceledException when pass canceled token.

```csharp
static async IAsyncEnumerable FooAsync([EnumeratorCancellation]CancellationToken cancellationToken = default)
{
yield return 1;
await Task.Delay(10, cancellationToken);
}

static void Main(string[] args)
{
// Create Canceled token.
var cts = new CancellationTokenSource();
cts.Cancel();

// OK, don't throw.
var e1 = FooAsync(cts.Token).GetAsyncEnumerator(cts.Token);
Console.WriteLine("OK:FooAsyunc().GetAsyncEnumerator()");

// Ix.Async LINQ Operator throws OperationCanceledException
var e2 = FooAsync(cts.Token).Select(x => x).GetAsyncEnumerator(cts.Token);
}
```

Is this the expected behavior?
The source code has a reference to [LDM-2018-11-28].

```csharp
public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior.
```

However, I think it doesn't equivalent behavior.

---
Environment:
`netcoreapp3.1`
``

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.