1006

I can't get to the bottom of this error, because when the debugger is attached, it does not seem to occur.

Collection was modified; enumeration operation may not execute

Below is the code.

This is a WCF server in a Windows service. The method NotifySubscribers() is called by the service whenever there is a data event (at random intervals, but not very often - about 800 times per day).

When a Windows Forms client subscribes, the subscriber ID is added to the subscribers dictionary, and when the client unsubscribes, it is deleted from the dictionary. The error happens when (or after) a client unsubscribes. It appears that the next time the NotifySubscribers() method is called, the foreach() loop fails with the error in the subject line. The method writes the error into the application log as shown in the code below. When a debugger is attached and a client unsubscribes, the code executes fine.

Do you see a problem with this code? Do I need to make the dictionary thread-safe?

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class SubscriptionServer : ISubscriptionServer
{
    private static IDictionary<Guid, Subscriber> subscribers;

    public SubscriptionServer()
    {            
        subscribers = new Dictionary<Guid, Subscriber>();
    }

    public void NotifySubscribers(DataRecord sr)
    {
        foreach(Subscriber s in subscribers.Values)
        {
            try
            {
                s.Callback.SignalData(sr);
            }
            catch (Exception e)
            {
                DCS.WriteToApplicationLog(e.Message, 
                  System.Diagnostics.EventLogEntryType.Error);

                UnsubscribeEvent(s.ClientId);
            }
        }
    }
    
    public Guid SubscribeEvent(string clientDescription)
    {
        Subscriber subscriber = new Subscriber();
        subscriber.Callback = OperationContext.Current.
                GetCallbackChannel<IDCSCallback>();

        subscribers.Add(subscriber.ClientId, subscriber);
        
        return subscriber.ClientId;
    }

    public void UnsubscribeEvent(Guid clientId)
    {
        try
        {
            subscribers.Remove(clientId);
        }
        catch(Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Unsubscribe Error " + 
                    e.Message);
        }
    }
}
Dale K
  • 16,372
  • 12
  • 37
  • 62
cdonner
  • 34,608
  • 21
  • 96
  • 146
  • in my case it was a collateral effect because I was using a few .Include("table") that got modified during the process - not very obvious when reading the code. however I was lucky that those Includes were not needed (yeah! old, unmaintained code) and I resolved my issue by just removing them – Adi Mar 09 '20 at 22:35
  • Please take a look at the answer @joe provided. It is a much better solution in many cases. https://stackoverflow.com/a/57799537/10307728 – orangecaterpillar Jul 26 '20 at 16:43

17 Answers17

1792

What's likely happening is that SignalData is indirectly changing the subscribers dictionary under the hood during the loop and leading to that message. You can verify this by changing

foreach(Subscriber s in subscribers.Values)

To

foreach(Subscriber s in subscribers.Values.ToList())

If I'm right, the problem will disappear.

Calling subscribers.Values.ToList() copies the values of subscribers.Values to a separate list at the start of the foreach. Nothing else has access to this list (it doesn't even have a variable name!), so nothing can modify it inside the loop.

Dale K
  • 16,372
  • 12
  • 37
  • 62
JaredPar
  • 673,544
  • 139
  • 1,186
  • 1,421
  • 16
    BTW .ToList() is present in System.Core dll which is not compatible with .NET 2.0 applications. So you might need to change your target app to .Net 3.5 – mishal153 May 19 '10 at 09:25
  • 5
    This is great. I did it with an ArrayList and it also worked perfectly (obviously with ToArray()) – aleafonso Sep 19 '11 at 09:46
  • 65
    I do not understand why you did a ToList and why that fixes everything – PositiveGuy Feb 29 '12 at 06:13
  • 215
    @CoffeeAddict: The issue is that `subscribers.Values` is being modified inside the `foreach` loop. Calling `subscribers.Values.ToList()` copies the values of `subscribers.Values` to a separate list at the start of the `foreach`. Nothing else has access to this list *(it doesn't even have a variable name!)*, so nothing can modify it inside the loop. – BlueRaja - Danny Pflughoeft Apr 20 '12 at 12:37
  • @BlueRaja-DannyPflughoeft so do you mean while `ToList` is no applied we work with references to same list? Even though we already started looping through new list? – Johnny_D Apr 30 '14 at 09:37
  • 39
    Note that `ToList` could also throw if collection was modified while `ToList` is executing. – Sriram Sakthivel May 15 '15 at 08:43
  • 1
    @Zapnologica You will not a null pointer becuase C# is managed code. The instance will be disposed by the GC only after all the references are released. What you say will be true if it were C++. But, I do agree, it might lead to inconsistencies because you may be working with a stale enumeration. – Vishnu Pedireddi Jun 09 '15 at 21:31
  • 23
    I am pretty sure think doesn't fix the issue, but merely makes it harder to reproduce. `ToList` isn't an atomic operation. What's even funnier, `ToList` bascially does its own `foreach` internally to copy items into a new list instance, meaning you fixed a `foreach` problem by adding an additional (although quicker) `foreach` iteration. – Groo Jun 15 '15 at 13:26
  • 6
    I think the concerns about ToList not being atomic are very odd. This is not multi-threaded code. To think that the loop may start before ToList completes is fundamentally misunderstanding how programming languages work. It will not multithread parts of your code at random, or execute the code out of order. Also, InstanceContextMode.Single ensures that it only handles one request at a time. – KthProg Jan 27 '17 at 17:30
  • @SriramSakthivel how about using ToArray() ? – Kugan Kumar Jun 27 '17 at 08:14
  • 1
    @KuganKumar `ToArray` isn't any different from `ToList`. It will also throw exception if modified in another thread while executing the `ToArray` method. – Sriram Sakthivel Jun 28 '17 at 17:28
  • Had this issue while looping through a dictionary of items and adding mid flow, and the above `.ToList()` method fixed this. – Mike Upjohn Sep 04 '17 at 16:28
  • 1
    What if you get this problem on a collection which is *already* a list? – NickG Oct 25 '17 at 08:27
  • 1
    For what it's worth... the poster didn't say it would fix the issue... He said it could be used to verify/confirm the issue – Ben Call Sep 28 '18 at 16:19
  • 1
    @NickG That doesn't matter, as `.ToList()` will always create a new list, regardless of the actual type of the enumerable you use it on – Tom Lint Aug 27 '19 at 13:57
  • @KuganKumar as @SriramSakthivel said methods like `ToList` `ToArray` will still need to look through the list, so they can still throw in a concurrent scenario. The reason this most rated solution **seems** to be working is that unlike `foreach` , `ToList` doesn't need to do any process, it's much faster. But unlikely doesn't mean deterministic. see this [example](https://dotnetfiddle.net/IqVbZ8). A solution to this is by using immutable classes. This answer needs to be improved. – joe Sep 05 '19 at 05:05
  • Be cafreful around fixing the error, it might be presenting a bigger problem. I'm using a custom nopcommerce site, and the copy from product service is where the error was. it was because I was editing a product, then trying to copy from the same product into itself. In this situation you don't want to allow this, so maybe some kind of user friendly error explaining to the user that they can't use "copy from" to copy the product they are editing into itself :) – Web Dev Guy Apr 01 '20 at 01:37
  • @NickG you could use `ToArray()` or you could make a copy (explicitly) of the list before you start the loop and iterate over the copy. – orangecaterpillar Jul 26 '20 at 16:34
  • Also, look at @joe's answer below, using Immutable types. https://stackoverflow.com/a/57799537/10307728 – orangecaterpillar Jul 26 '20 at 16:45
  • In my case the entities were being updated and saved on the db but I was curious about the error/warning I was getting instead of a 200. Thanks, this worked just fine. – Spyros_Spy Oct 21 '20 at 08:20
  • Worked for me, although as others have mentioned: perhaps a little inefficient - for those wondering why this works: a local copy is made and then iterated on, stopped any other updates from interfering with it as it's accessed in local scope only. – Jamie Nicholl-Shelley Nov 16 '20 at 11:18
  • it's working for me – Raj K Mar 11 '21 at 03:15
118

When a subscriber unsubscribes you are changing contents of the collection of Subscribers during enumeration.

There are several ways to fix this, one being changing the for loop to use an explicit .ToList():

public void NotifySubscribers(DataRecord sr)  
{
    foreach(Subscriber s in subscribers.Values.ToList())
    {
                                              ^^^^^^^^^  
        ...
Mitch Wheat
  • 280,588
  • 41
  • 444
  • 526
70

A more efficient way, in my opinion, is to have another list that you declare that you put anything that is "to be removed" into. Then after you finish your main loop (without the .ToList()), you do another loop over the "to be removed" list, removing each entry as it happens. So in your class you add:

private List<Guid> toBeRemoved = new List<Guid>();

Then you change it to:

public void NotifySubscribers(DataRecord sr)
{
    toBeRemoved.Clear();

    ...your unchanged code skipped...

   foreach ( Guid clientId in toBeRemoved )
   {
        try
        {
            subscribers.Remove(clientId);
        }
        catch(Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Unsubscribe Error " + 
                e.Message);
        }
   }
}

...your unchanged code skipped...

public void UnsubscribeEvent(Guid clientId)
{
    toBeRemoved.Add( clientId );
}

This will not only solve your problem, it will prevent you from having to keep creating a list from your dictionary, which is expensive if there are a lot of subscribers in there. Assuming the list of subscribers to be removed on any given iteration is lower than the total number in the list, this should be faster. But of course feel free to profile it to be sure that's the case if there's any doubt in your specific usage situation.

Soner Gönül
  • 91,172
  • 101
  • 184
  • 324
x4000
  • 2,717
  • 3
  • 25
  • 24
  • 11
    I'm expecting this to be worth considering if you have a you are working with larger collections. If it small I'd probably just ToList and move on. – Karl Kieninger Mar 26 '15 at 21:34
45

You can also lock your subscribers dictionary to prevent it from being modified whenever its being looped:

 lock (subscribers)
 {
         foreach (var subscriber in subscribers)
         {
               //do something
         }
 }
Mohammad Sepahvand
  • 16,889
  • 20
  • 78
  • 119
  • 2
    Is that a complete example? I have a class (_dictionary obj below) that contains a generic Dictionary named MarkerFrequencies, but doing this didn't instantly solve the crash: lock (_dictionary.MarkerFrequencies) { foreach (KeyValuePair pair in _dictionary.MarkerFrequencies) {...} } – Jon Coombs Apr 21 '14 at 05:25
  • 4
    @JCoombs it's possible that you're modifying, possibly reassigning the `MarkerFrequencies` dictionary inside the lock itself, meaning that the original instance is no longer locked. Also try using a `for` instead of a `foreach`, See [this](http://stackoverflow.com/questions/9925083/collection-was-modified-enumeration-operation-may-not-execute) and [this](http://stackoverflow.com/questions/16759964/collection-was-modified-enumeration-operation-may-not-execute-lock-is-being-us). Let me know if that solves it. – Mohammad Sepahvand Apr 21 '14 at 06:09
  • 1
    Well, I finally got around to fixing this bug, and these tips were helpful--thanks! My dataset was small, and this operation was just a UI display update, so iterating over a copy seemed best. (I also experimented with using for instead of foreach after locking MarkerFrequencies in both threads. This prevented the crash but seemed to require further debugging work. And it introduced more complexity. My only reason for having two threads here is to enable the user to cancel an operation, by the way. The UI doesn't directly modify that data.) – Jon Coombs Jul 22 '14 at 19:45
  • 12
    Problem is, for large applications, locks can be a major performance hit -- better off to use a collection in the `System.Collections.Concurrent` namespace. – BrainSlugs83 Sep 08 '14 at 04:39
36

Why this error?

In general .Net collections do not support being enumerated and modified at the same time. If you try to modify the collection list during enumeration, it raises an exception. So the issue behind this error is, we can not modify the list/dictionary while we are looping through the same.

One of the solutions

If we iterate a dictionary using a list of its keys, in parallel we can modify the dictionary object, as we are iterating through the key-collection and not the dictionary(and iterating its key collection).

Example

//get key collection from dictionary into a list to loop through
List<int> keys = new List<int>(Dictionary.Keys);

// iterating key collection using a simple for-each loop
foreach (int key in keys)
{
  // Now we can perform any modification with values of the dictionary.
  Dictionary[key] = Dictionary[key] - 1;
}

Here is a blog post about this solution.

And for a deep dive in StackOverflow: Why this error occurs?

open and free
  • 2,097
  • 18
  • 22
  • Thank you! A clear explanation of why it happens and immediately gave me the way to solve this in my application. – Kim Crosser Sep 13 '19 at 23:29
  • As of .NET Core 3.0 with C# 8.0, a [dictionary may modified during enumeration (foreach) via .Remove and .Clear only](https://stackoverflow.com/a/62844729/5587356). This does not apply to other collections. – Super Jade Jul 11 '20 at 03:40
9

Okay so what helped me was iterating backwards. I was trying to remove an entry from a list but iterating upwards and it screwed up the loop because the entry didn't exist anymore:

for (int x = myList.Count - 1; x > -1; x--)
{
    myList.RemoveAt(x);
}
Neuron
  • 3,776
  • 3
  • 24
  • 44
Mark Aven
  • 145
  • 1
  • 6
5

Actually the problem seems to me that you are removing elements from the list and expecting to continue to read the list as if nothing had happened.

What you really need to do is to start from the end and back to the begining. Even if you remove elements from the list you will be able to continue reading it.

luc.rg.roy
  • 51
  • 1
  • 1
  • I dont see how that would make any difference? If you removing an element in the middle of the list it would still throw an error as the item it is trying to access cannot be found? – Zapnologica Jul 17 '14 at 05:48
  • 4
    @Zapnologica the difference is -- you wouldn't be *enumerating* the list -- instead of doing a for/each, you'd be doing a for/next and accessing it by integer -- you can definitely modify a list a in a for/next loop, but never in a for/each loop (because for/each *enumerates*) -- you can also do it going forwards in a for/next, provided you have extra logic to adjust your counters, etc. – BrainSlugs83 Sep 08 '14 at 04:37
4

InvalidOperationException- An InvalidOperationException has occurred. It reports a "collection was modified" in a foreach-loop

Use break statement, Once the object is removed.

ex:

ArrayList list = new ArrayList(); 

foreach (var item in list)
{
    if(condition)
    {
        list.remove(item);
        break;
    }
}
nich
  • 84
  • 3
  • 9
vivek
  • 41
  • 2
3

The accepted answer is imprecise and incorrect in the worst case . If changes are made during ToList(), you can still end up with an error. Besides lock, which performance and thread-safety needs to be taken into consideration if you have a public member, a proper solution can be using immutable types.

In general, an immutable type means that you can't change the state of it once created. So your code should look like:

public class SubscriptionServer : ISubscriptionServer
{
    private static ImmutableDictionary<Guid, Subscriber> subscribers = ImmutableDictionary<Guid, Subscriber>.Empty;
    public void SubscribeEvent(string id)
    {
        subscribers = subscribers.Add(Guid.NewGuid(), new Subscriber());
    }
    public void NotifyEvent()
    {
        foreach(var sub in subscribers.Values)
        {
            //.....This is always safe
        }
    }
    //.........
}

This can be especially useful if you have a public member. Other classes can always foreach on the immutable types without worrying about the collection being modified.

joe
  • 732
  • 6
  • 23
2

I had the same issue, and it was solved when I used a for loop instead of foreach.

// foreach (var item in itemsToBeLast)
for (int i = 0; i < itemsToBeLast.Count; i++)
{
    var matchingItem = itemsToBeLast.FirstOrDefault(item => item.Detach);

   if (matchingItem != null)
   {
      itemsToBeLast.Remove(matchingItem);
      continue;
   }
   allItems.Add(itemsToBeLast[i]);// (attachDetachItem);
}
Nisarg
  • 13,121
  • 5
  • 31
  • 48
  • 11
    This code is wrong and will skip some items in the collection if any element will be removed. For example: you have var arr = ["a", "b", "c"] and in first iteration (i = 0) you remove element at position 0 (element "a"). After this all the array elements will move one position up and array will be ["b", "c"]. So, in the next iteration (i=1) you will check element at position 1 which will be "c" not "b". This is wrong. To fix that, you have to move from bottom to the top – Kaspars Ozols Apr 21 '15 at 15:59
2

I've seen many options for this but to me this one was the best.

ListItemCollection collection = new ListItemCollection();
        foreach (ListItem item in ListBox1.Items)
        {
            if (item.Selected)
                collection.Add(item);
        }

Then simply loop through the collection.

Be aware that a ListItemCollection can contain duplicates. By default there is nothing preventing duplicates being added to the collection. To avoid duplicates you can do this:

ListItemCollection collection = new ListItemCollection();
            foreach (ListItem item in ListBox1.Items)
            {
                if (item.Selected && !collection.Contains(item))
                    collection.Add(item);
            }
Mike
  • 281
  • 1
  • 4
  • 21
  • How would this code prevent duplicates from being entered into the database. I have written something similar and when I add new users from the listbox, if I accidentally keep one selected that is already in the list, it will create a duplicate entry. Do you have any suggestions @Mike ? – Jamie Mar 15 '18 at 16:37
2

I want to point out other case not reflected in any of the answers. I have a Dictionary<Tkey,TValue> shared in a multi threaded app, which uses a ReaderWriterLockSlim to protect the read and write operations. This is a reading method that throws the exception:

public IEnumerable<Data> GetInfo()
{
    List<Data> info = null;
    _cacheLock.EnterReadLock();
    try
    {
        info = _cache.Values.SelectMany(ce => ce.Data); // Ad .Tolist() to avoid exc.
    }
    finally
    {
        _cacheLock.ExitReadLock();
    }
    return info;
}

In general, it works fine, but from time to time I get the exception. The problem is a subtlety of LINQ: this code returns an IEnumerable<Info>, which is still not enumerated after leaving the section protected by the lock. So, it can be changed by other threads before being enumerated, leading to the exception. The solution is to force the enumeration, for example with .ToList() as shown in the comment. In this way, the enumerable is already enumerated before leaving the protected section.

So, if using LINQ in a multi-threaded application, be aware to always materialize the queries before leaving the protected regions.

JotaBe
  • 34,736
  • 7
  • 85
  • 109
1

There is one link where it elaborated very well & solution is also given. Try it if you got proper solution please post here so other can understand. Given solution is ok then like the post so other can try these solution.

for you reference original link :- https://bensonxion.wordpress.com/2012/05/07/serializing-an-ienumerable-produces-collection-was-modified-enumeration-operation-may-not-execute/

When we use .Net Serialization classes to serialize an object where its definition contains an Enumerable type, i.e. collection, you will be easily getting InvalidOperationException saying "Collection was modified; enumeration operation may not execute" where your coding is under multi-thread scenarios. The bottom cause is that serialization classes will iterate through collection via enumerator, as such, problem goes to trying to iterate through a collection while modifying it.

First solution, we can simply use lock as a synchronization solution to ensure that the operation to the List object can only be executed from one thread at a time. Obviously, you will get performance penalty that if you want to serialize a collection of that object, then for each of them, the lock will be applied.

Well, .Net 4.0 which makes dealing with multi-threading scenarios handy. for this serializing Collection field problem, I found we can just take benefit from ConcurrentQueue(Check MSDN)class, which is a thread-safe and FIFO collection and makes code lock-free.

Using this class, in its simplicity, the stuff you need to modify for your code are replacing Collection type with it, use Enqueue to add an element to the end of ConcurrentQueue, remove those lock code. Or, if the scenario you are working on do require collection stuff like List, you will need a few more code to adapt ConcurrentQueue into your fields.

BTW, ConcurrentQueue doesnât have a Clear method due to underlying algorithm which doesnât permit atomically clearing of the collection. so you have to do it yourself, the fastest way is to re-create a new empty ConcurrentQueue for a replacement.

1

Here is a specific scenario that warrants a specialized approach:

  1. The Dictionary is enumerated frequently.
  2. The Dictionary is modified infrequently.

In this scenario creating a copy of the Dictionary (or the Dictionary.Values) before every enumeration can be quite costly. My idea about solving this problem is to reuse the same cached copy in multiple enumerations, and watch an IEnumerator of the original Dictionary for exceptions. The enumerator will be cached along with the copied data, and interrogated before starting a new enumeration. In case of an exception the cached copy will be discarded, and a new one will be created. Here is my implementation of this idea:

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

public class EnumerableSnapshot<T> : IEnumerable<T>, IDisposable
{
    private IEnumerable<T> _source;
    private IEnumerator<T> _enumerator;
    private ReadOnlyCollection<T> _cached;

    public EnumerableSnapshot(IEnumerable<T> source)
    {
        _source = source ?? throw new ArgumentNullException(nameof(source));
    }

    public IEnumerator<T> GetEnumerator()
    {
        if (_source == null) throw new ObjectDisposedException(this.GetType().Name);
        if (_enumerator == null)
        {
            _enumerator = _source.GetEnumerator();
            _cached = new ReadOnlyCollection<T>(_source.ToArray());
        }
        else
        {
            var modified = false;
            if (_source is ICollection collection) // C# 7 syntax
            {
                modified = _cached.Count != collection.Count;
            }
            if (!modified)
            {
                try
                {
                    _enumerator.MoveNext();
                }
                catch (InvalidOperationException)
                {
                    modified = true;
                }
            }
            if (modified)
            {
                _enumerator.Dispose();
                _enumerator = _source.GetEnumerator();
                _cached = new ReadOnlyCollection<T>(_source.ToArray());
            }
        }
        return _cached.GetEnumerator();
    }

    public void Dispose()
    {
        _enumerator?.Dispose();
        _enumerator = null;
        _cached = null;
        _source = null;
    }

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

public static class EnumerableSnapshotExtensions
{
    public static EnumerableSnapshot<T> ToEnumerableSnapshot<T>(
        this IEnumerable<T> source) => new EnumerableSnapshot<T>(source);
}

Usage example:

private static IDictionary<Guid, Subscriber> _subscribers;
private static EnumerableSnapshot<Subscriber> _subscribersSnapshot;

//...(in the constructor)
_subscribers = new Dictionary<Guid, Subscriber>();
_subscribersSnapshot = _subscribers.Values.ToEnumerableSnapshot();

// ...(elsewere)
foreach (var subscriber in _subscribersSnapshot)
{
    //...
}

Unfortunately this idea cannot be used currently with the class Dictionary in .NET Core 3.0, because this class does not throw a Collection was modified exception when enumerated and the methods Remove and Clear are invoked. All other containers I checked are behaving consistently. I checked systematically these classes: List<T>, Collection<T>, ObservableCollection<T>, HashSet<T>, SortedSet<T>, Dictionary<T,V> and SortedDictionary<T,V>. Only the two aforementioned methods of the Dictionary class in .NET Core are not invalidating the enumeration.


Update: I fixed the above problem by comparing also the lengths of the cached and the original collection. This fix assumes that the dictionary will be passed directly as an argument to the EnumerableSnapshot's constructor, and its identity will not be hidden by (for example) a projection like: dictionary.Select(e => e).ΤοEnumerableSnapshot().


Important: The above class is not thread safe. It is intended to be used from code running exclusively in a single thread.

Theodor Zoulias
  • 15,834
  • 3
  • 19
  • 54
1

This way should cover a situation of concurrency when the function is called again while is still executing (and items need used only once):

 while (list.Count > 0)
 {
    string Item = list[0];
    list.RemoveAt(0);
 
    // do here what you need to do with item
 
 } 
 

If the function get called while is still executing items will not reiterate from the first again as they get deleted as soon as they get used. Should not affect performance much for small lists.

qfactor77
  • 660
  • 5
  • 7
0

You can copy subscribers dictionary object to a same type of temporary dictionary object and then iterate the temporary dictionary object using foreach loop.

Rezoan
  • 1,598
  • 21
  • 49
  • (This post does not seem to provide a [quality answer](https://stackoverflow.com/help/how-to-answer) to the question. Please either edit your answer and, or just post it as a comment to the question). – sɐunıɔןɐqɐp Jun 24 '18 at 19:09
0

So a different way to solve this problem would be instead of removing the elements create a new dictionary and only add the elements you didnt want to remove then replace the original dictionary with the new one. I don't think this is too much of an efficiency problem because it does not increase the number of times you iterate over the structure.

ford prefect
  • 6,008
  • 10
  • 51
  • 78