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

Nested enumeration of one Container's Elements causes missing elements in outer enumerator #19

Closed
ghemberg opened this issue Oct 12, 2017 · 1 comment · Fixed by #30
Closed
Labels

Comments

@ghemberg
Copy link

Consider the following code in a Visitor:

private List currentList;

public override VisitList(List list)
{
    this.currentList = list;
    base.VisitList(list);
}

public override VisitItem(Item item)
{
    Console.WriteLine(this.currentList.Elements.Count());
    base.VisitItem(item);
}

For a List containing 4 Item elements, you'd expect to see the following on the Console:

4
4
4
4

What you actually get is this:

4
@ghemberg
Copy link
Author

This is caused by CachedEnumerableImpl<T>.GetEnumerator():

public IEnumerator<T> GetEnumerator()
{
    foreach (var item in cache)
    {
        yield return item;
    }

    if (enumerator == null)
        enumerator = enumerable.GetEnumerator();

    while (enumerator.MoveNext())
    {
        cache.Add(enumerator.Current);
        yield return enumerator.Current;
    }
}

The VisitList(List) enumerator gets to the second yield statement and calls VisitItem(Item) for the first Item it gets. VisitItem(Item) then re-uses the same enumerator instance and enumerates over the remaining elements. As VisitList(List)'s enumerating resumes, enumerator has moved to the end of the enumeration and enumerator.MoveNext() returns false (and the remaining Item elements are skipped)...

kzu added a commit that referenced this issue Jan 28, 2021
When a visitor enumerates elements while an outside enumeration is still happening, elements are missed because the cache is still being built.

Fixes #19.
kzu added a commit that referenced this issue Jan 28, 2021
When a visitor enumerates elements while an outside enumeration is still happening, elements are missed because the cache is still being built.

Fixes #19.
kzu added a commit that referenced this issue Jan 28, 2021
When a visitor enumerates elements while an outside enumeration is still happening, elements are missed because the cache is still being built.

Fixes #19.
@kzu kzu closed this as completed in #30 Jan 28, 2021
kzu added a commit that referenced this issue Jan 28, 2021
When a visitor enumerates elements while an outside enumeration is still happening, elements are missed because the cache is still being built.

Fixes #19.
@kzu kzu added the bug label Jan 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants