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 @@ -28,51 +28,45 @@ 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
// REVIEW: fire-and-forget DisposeAsync?
await using (var e = _source.GetAsyncEnumerator(ctd.Token))
quinmars marked this conversation as resolved.
Show resolved Hide resolved
{
hasNext = await e.MoveNextAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
if (!ctd.Token.IsCancellationRequested)
do
{
observer.OnError(ex);
await e.DisposeAsync().ConfigureAwait(false);
}
bool hasNext;
try
{
hasNext = await e.MoveNextAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
if (!ctd.Token.IsCancellationRequested)
{
observer.OnError(ex);
}

return;
}
return;
}

if (hasNext)
{
observer.OnNext(e.Current);
if (!hasNext)
{
observer.OnCompleted();
return;
}

if (!ctd.Token.IsCancellationRequested)
{
Core();
observer.OnNext(e.Current);
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.
}
else
{
observer.OnCompleted();
await e.DisposeAsync().ConfigureAwait(false);
while (!ctd.Token.IsCancellationRequested);
}
}

// 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