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

Ix: Do not dispose the enumerator while enumerating in the ToObservable operator. #915

Merged
merged 9 commits into from Nov 20, 2019
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Threading.Tasks;

namespace System.Linq
{
Expand All @@ -28,51 +29,40 @@ public ToObservableObservable(IAsyncEnumerable<T> source)
public IDisposable Subscribe(IObserver<T> observer)
{
var ctd = new CancellationTokenDisposable();
var e = _source.GetAsyncEnumerator(ctd.Token);

async void Core()
{
bool hasNext;
try
{
hasNext = await e.MoveNextAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
if (!ctd.Token.IsCancellationRequested)
await foreach (var element in _source.WithCancellation(ctd.Token).ConfigureAwait(false))
{
observer.OnError(ex);
await e.DisposeAsync().ConfigureAwait(false);
}
observer.OnNext(element);

return;
if (ctd.Token.IsCancellationRequested)
{
return;
}
}
}

if (hasNext)
catch (Exception error)
{
observer.OnNext(e.Current);

if (!ctd.Token.IsCancellationRequested)
{
Core();
observer.OnError(error);
quinmars marked this conversation as resolved.
Show resolved Hide resolved
}

// In case cancellation is requested, this could only have happened
// by disposing the returned composite disposable (see below).
// In that case, e will be disposed too, so there is no need to dispose e here.
return;
}
else

if (!ctd.Token.IsCancellationRequested)
{
observer.OnCompleted();
await e.DisposeAsync().ConfigureAwait(false);
}
}

// Fire and forget
Core();

// REVIEW: Safety of concurrent dispose operation; fire-and-forget nature of dispose?

return Disposable.Create(ctd, Disposable.Create(() => { e.DisposeAsync(); }));
return ctd;
}
}
}
Expand Down