|
@@ -32,6 +32,8 @@ namespace System.Linq
|
|
|
|
|
|
|
|
private Task? _loserTask;
|
|
private Task? _loserTask;
|
|
|
|
|
|
|
|
|
|
+ private CancellationTokenSource? _sourceCTS;
|
|
|
|
|
+
|
|
|
public TimeoutAsyncIterator(IAsyncEnumerable<TSource> source, TimeSpan timeout)
|
|
public TimeoutAsyncIterator(IAsyncEnumerable<TSource> source, TimeSpan timeout)
|
|
|
{
|
|
{
|
|
|
Debug.Assert(source != null);
|
|
Debug.Assert(source != null);
|
|
@@ -67,7 +69,8 @@ namespace System.Linq
|
|
|
switch (_state)
|
|
switch (_state)
|
|
|
{
|
|
{
|
|
|
case AsyncIteratorState.Allocated:
|
|
case AsyncIteratorState.Allocated:
|
|
|
- _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
|
|
|
|
|
|
|
+ _sourceCTS = CancellationTokenSource.CreateLinkedTokenSource(_cancellationToken);
|
|
|
|
|
+ _enumerator = _source.GetAsyncEnumerator(_sourceCTS.Token);
|
|
|
|
|
|
|
|
_state = AsyncIteratorState.Iterating;
|
|
_state = AsyncIteratorState.Iterating;
|
|
|
goto case AsyncIteratorState.Iterating;
|
|
goto case AsyncIteratorState.Iterating;
|
|
@@ -77,7 +80,7 @@ namespace System.Linq
|
|
|
|
|
|
|
|
if (!moveNext.IsCompleted)
|
|
if (!moveNext.IsCompleted)
|
|
|
{
|
|
{
|
|
|
- using var delayCts = new CancellationTokenSource();
|
|
|
|
|
|
|
+ using var delayCts = CancellationTokenSource.CreateLinkedTokenSource(_cancellationToken);
|
|
|
|
|
|
|
|
var delay = Task.Delay(_timeout, delayCts.Token);
|
|
var delay = Task.Delay(_timeout, delayCts.Token);
|
|
|
|
|
|
|
@@ -101,6 +104,8 @@ namespace System.Linq
|
|
|
|
|
|
|
|
_loserTask = next.ContinueWith((_, state) => ((IAsyncDisposable)state).DisposeAsync().AsTask(), _enumerator);
|
|
_loserTask = next.ContinueWith((_, state) => ((IAsyncDisposable)state).DisposeAsync().AsTask(), _enumerator);
|
|
|
|
|
|
|
|
|
|
+ _sourceCTS!.Cancel();
|
|
|
|
|
+
|
|
|
throw new TimeoutException();
|
|
throw new TimeoutException();
|
|
|
}
|
|
}
|
|
|
|
|
|