Skip to content

Remove CancelableEnumerator #10099

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

Merged
merged 2 commits into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 0 additions & 27 deletions src/SignalR/common/Shared/AsyncEnumerableAdapters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,10 @@ public IAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellati
{
Copy link

@dcarr42 dcarr42 May 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

registration not referenced. Actually is the private class still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class is still needed because we need to be able to cancel the inner stream operation from the token passed into GetASyncEnumerator but you bring up a good point about the registration. It should be disposed

((CancellationTokenSource)ctsState).Cancel();
}, _cts);

return new CancelableEnumerator<TResult>(_asyncEnumerable.GetAsyncEnumerator(), registration);
}

return enumerator;
}

private class CancelableEnumerator<T> : IAsyncEnumerator<T>
{
private IAsyncEnumerator<T> _asyncEnumerator;
private readonly CancellationTokenRegistration _cancellationTokenRegistration;

public T Current => (T)_asyncEnumerator.Current;

public CancelableEnumerator(IAsyncEnumerator<T> asyncEnumerator, CancellationTokenRegistration registration)
{
_asyncEnumerator = asyncEnumerator;
_cancellationTokenRegistration = registration;
}

public ValueTask<bool> MoveNextAsync()
{
return _asyncEnumerator.MoveNextAsync();
}

public ValueTask DisposeAsync()
{
_cancellationTokenRegistration.Dispose();
return _asyncEnumerator.DisposeAsync();
}
}
}

/// <summary>Converts an IAsyncEnumerable of T to an IAsyncEnumerable of object.</summary>
Expand Down
5 changes: 3 additions & 2 deletions src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,11 @@ private async Task StreamResultsAsync(string invocationId, HubConnectionContext

try
{
await foreach (var streamItem in enumerable)
var enumerator = enumerable.GetAsyncEnumerator(streamCts.Token);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the enumerator be disposed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave the await foreach and call .WithCancellation instead:

await foreach (var streamItem in enumerable.WithCancellation(streamCts.Token))
{
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it should

while (await enumerator.MoveNextAsync())
{
// Send the stream item
await connection.WriteAsync(new StreamItemMessage(invocationId, streamItem));
await connection.WriteAsync(new StreamItemMessage(invocationId, enumerator.Current));
}
}
catch (ChannelClosedException ex)
Expand Down