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

Unexpected behaviour after disposing #12

Closed
TomPeters opened this issue May 8, 2017 · 1 comment
Closed

Unexpected behaviour after disposing #12

TomPeters opened this issue May 8, 2017 · 1 comment

Comments

@TomPeters
Copy link

Consider the following example

var enumerable = new AsyncEnumerable<int>(async yield =>
    {
        await yield.ReturnAsync(1);
        await yield.ReturnAsync(2);
        await yield.ReturnAsync(3);
    });
var enumerator = await enumerable.GetAsyncEnumeratorAsync();
await enumerator.MoveNextAsync();
enumerator.Dispose();
await enumerator.MoveNextAsync(); // What is the expected behavior here?

If you compare this to an IEnumerable that is disposable, such as the following:

IEnumerable<int> GetItems()
{
	using (var disposable = new Disposable())
	{
		yield return 1;
		yield return 2;
		yield return 3;
	}
}

This has the following behavior:

IEnumerable<int> enumerable = GetItems();
var enumerator = enumerable.GetEnumerator();
enumerator.MoveNext(); // true;
enumerator.Current; // 1
enumerator.Dispose();
enumerator.MoveNext(); // false;
enumerator.Current; // 1

So disposing the enumerator acts as if you had reached the end of the enumeration. Whereas if you do the same thing on an IAsyncEnumerator, it will actually restart the enumeration from the start, because the implementation of MoveNextAsync will lazily start enumeration if _yield and _enumerationTask are null, and they are both set to null during Dispose.

@kind-serge
Copy link
Member

This is fixed in version 2.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants