Skip to content

LINQ bug (ConcatNIterator.ToArray()?) #7696

Closed

Description

@EgorBo

Steps to Reproduce

using System;
using System.Collections.Generic;
using System.Linq;

public static class Program
{
    public static void Main()
    {
        var slots = GetSlots().Concat(new[] { new Slot("q") });
        var flatten = slots.Flatten(container => container.AllParentSlots)/*.Select(i => i)*/.ToArray();

        var str = string.Join(", ", flatten.Select(x => x == null ? "NULL" : x.Id));
        Console.WriteLine(str);
        // Mono 5.x including master:
        //   "a, b, c, d, e, f, g, q, x, NULL"
        // .NET, .NET Core, Mono 4.2:
        //   "a, b, c, d, e, f, g, q, x, y"
    }
    
    public class Slot
    {
        public readonly string Id;
        public Slot(string id) => Id = id;
        public SlotsHolder ContainedSlotsHolder;
        public IEnumerable<Slot> AllParentSlots => ContainedSlotsHolder == null ? new Slot[0] : ContainedSlotsHolder.Slots;
    }

    public class SlotsHolder
    {
        public Slot[] Slots;
    }
    
    static IEnumerable<T> Flatten<T>(this IEnumerable<T> e, Func<T, IEnumerable<T>> f) 
        => e.Concat(e.SelectMany(c => f(c).Flatten(f)));

    static IEnumerable<Slot> GetSlots()
    {
        yield return new Slot("a");
        yield return new Slot("b");
        yield return new Slot("c");
        yield return new Slot("d");
        yield return new Slot("e");
        yield return new Slot("f");
        yield return new Slot("g") {
            ContainedSlotsHolder = new SlotsHolder { Slots = new[] {
                new Slot("x"),
                new Slot("y")
            }}};
    }
}

It feels like the issue is somewhere in ConcatNIterator.ToLazyArray

Current Behavior

The code prints a, b, c, d, e, f, g, q, x, NULL

Expected Behavior

The code should print a, b, c, d, e, f, g, q, x, y just like .NET/.NET Core do.

On which platforms did you notice this

[ ] macOS
[ ] Linux
[x] Windows

Version Used:
mono 5.0-5.13
Does not reproduce on old versions of mono when we had a "slow" Enumerable.ToArray implementation.

Activity

  1. mderoy commented on Mar 21, 2018

    @mderoy
    Contributor
  2. self-assigned this
    on Mar 22, 2018
  3. added this to the 2017-12 (5.10.xx) milestone on Mar 22, 2018
  4. KosmicTask commented on Mar 22, 2018

    @KosmicTask

    Not sure if this is the same issue but it may have a connection.

    The following Distinct() method call succeeds on .Net but raises with a Null ref exception on Mono macOS 10.13
    .
    var orders = items.SelectMany(e => e.Deductions).Select(e => e.Order).Distinct();
    foreach (var order in orders) {
    … // order is Null here and raises
    }

    On Mono order is Null though orders is an instance of System.Linq.Enumerable.DistinctIterator.
    In the local vars browser orders IEnumerator is Null.

    Sorry I don't have a test case. It's something that turned up on production code.

  5. added a commit that references this issue on Mar 22, 2018
  6. marek-safar commented on Mar 23, 2018

    @marek-safar
    Member

    The issue was resolved.

    @mugginsoft please test it with mono master or include full test case I could try to test.

  7. KosmicTask commented on Mar 23, 2018

    @KosmicTask

    Will do. If my issue persist I will try and pull it out of its production context.

  8. added a commit that references this issue on Mar 24, 2018
  9. KosmicTask commented on Mar 24, 2018

    @KosmicTask

    So I tried the nightly build that includes this fix in my production code and too many things break before I can get anywhere near exercising this particular issue. Will try and pull out a test case.

  10. added a commit that references this issue on Mar 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions