Skip to content

.Skip allows a Collection to be modified during enumeration #40415

@joshbartley

Description

@joshbartley

Description

I flipped a variable and found out that .Skip allows the IEnumerable to be modified during enumeration which caused an infinite loop. Related to a similar issue that was moved in [dotnet/corefx] (#32278).

            var example = new List<string>() { "test0","test1" };  
            var i = 0;  
            foreach (var item in example.Skip(1))
            {
                example.Add("test");
                i++;
                if (i > 1000)
                {
                    Console.WriteLine("Modified IEnumerable through LINQ");
                    break;
                }
            }

Tested without LINQ, and the exception is thrown that you cannot modify a IEnumerable in progress.

Tried with .OrderBy() and it allowed the code to run but did not add new entries to the end of the IEnumerable and would only run twice, which could be a separate bug.

I have not tried with a LINQ/Lambda only statement as the first time this happened it locked a 24gb Ryzen machine.

Configuration

.Net 3.1 ,Windows 10 x64

Regression?

Previously any modification of the IEnumerable while enumerating caused an exception.

Other information

An optimization caused an issue with a similar bug report for 2.1 dealing with infinite loops and IEnumerable [dotnet/corefx] (#32278). It was decided to roll back that merge request to fix the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.LinqenhancementProduct code improvement that does NOT require public API changes/additionsneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions