System.InvalidOperationException: Collection was modified; enumeration operation may not execute for ImmutableDictionary

I have a function that is being repeatedly called via a thread. Randomly some times it raises this exception.

 System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   at System.String.Join[T](String separator, IEnumerable`1 values)
   at FormatLoDestStatus(ImmutableDictionary`2 loDestStatus) 
   in C:Formatter.cs:line 117

The function that is being called is :

  public string FormatLoDestStatus(ImmutableDictionary<string, List<int>> loDestStatus)
  {
      List<string> result = new List<string>();
      foreach (var status in loDestStatus)
      {
          string value = string.Join(",", status.Value).TrimEnd();
          var formatted = $"{status.Key}S{value}";  // This is place where it gives invalid operation
          if (Regex.IsMatch(formatted, _patternCollection.LoDestStatusPattern)
              &&
              Regex.IsMatch(value, _patternCollection.UnAvailableLevelsPattern))
          {
              result.Add(formatted);
          }
      }

      return string.Join(":", result);
  }

I converted loDestStatus from Dictionary<string, List<int>> to ImmutableDictionary<string, List<int>> but still it randomly gives exception at the line i mentioned in code where i apply string.Join(",", status.Value).TrimEnd();

What is actually going wrong and how i can solve it ? (My assumption here is if i have ImmutableDictionary, it is thread safe and the original immutable Dictionary loDestStatus shouldn’t be changed).

New contributor

user27330175 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

You can see from your stack trace that the problem happens when you iterate the List<T> during the string.Join call:

at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at System.String.Join[T](String separator, IEnumerable`1 values)

List<T> is not thread-safe. From the documentation:

It is safe to perform multiple read operations on a List<T>, but issues can occur if the collection is modified while it’s being read. To ensure thread safety, lock the collection during a read or write operation. To enable a collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization. For collections with built-in synchronization, see the classes in the System.Collections.Concurrent namespace. For an inherently thread-safe alternative, see the ImmutableList<T> class.

So, how you want to solve this depends on how exactly you’re using that list.


One solution is to keep using a List<T>, but make sure that you acquire a lock before reading or writing it. You could use the List<T> itself as the lock object, or have a dedicated lock:

public class ListAndLock<T>
{
    public List<T> List { get; } = new();
    public object LockObject { get; } = new();
}

Then your dictionary becomes:

ImmutableDictionary<string, ListAndLock<int>>

To add an item to the list, you need to acquire the lock first:

ListAndLock<int> listAndLock = ...;
lock (listAndLock.LockObject)
{
    listAndLock.List.Add(...);
}

And iterating the list becomes:

string value;
lock (status.Value.LockObject)
{
    value = string.Join(",", status.Value.List).TrimEnd();
}

Another option is to reach for a suitable thread-safe collection.

For example, if the order of items in your list doesn’t actually matter, you could use a ConcurrentBag<T>:

ImmutableDictionary<string, ConcurrentBag<int>>

It is safe to read from a ConcurrentBag<T> while also adding elements to it.

If order does matter, you could make use of ConcurrentStack<T> or ConcurrentQueue<T>.


Another option is to go down the immutable collections route, and use an ImmutableList<T>.

This does make adding elements slightly harder, as you need to modify the ImmutableDictionary<T>:

ImmutableDictionary<string, ImmutableList<int>> dict = ...;
var newList = dict[key].Add(...);
dict = dict.SetItem(key, newList);

… and if multiple threads are doing this to the dictionary at the same time, you need to make sure that modifications made by one of the threads aren’t thrown away by the other thread.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

System.InvalidOperationException: Collection was modified; enumeration operation may not execute for ImmutableDictionary

I have a function that is being repeatedly called via a thread. Randomly some times it raises this exception.

 System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   at System.String.Join[T](String separator, IEnumerable`1 values)
   at FormatLoDestStatus(ImmutableDictionary`2 loDestStatus) 
   in C:Formatter.cs:line 117

The function that is being called is :

  public string FormatLoDestStatus(ImmutableDictionary<string, List<int>> loDestStatus)
  {
      List<string> result = new List<string>();
      foreach (var status in loDestStatus)
      {
          string value = string.Join(",", status.Value).TrimEnd();
          var formatted = $"{status.Key}S{value}";  // This is place where it gives invalid operation
          if (Regex.IsMatch(formatted, _patternCollection.LoDestStatusPattern)
              &&
              Regex.IsMatch(value, _patternCollection.UnAvailableLevelsPattern))
          {
              result.Add(formatted);
          }
      }

      return string.Join(":", result);
  }

I converted loDestStatus from Dictionary<string, List<int>> to ImmutableDictionary<string, List<int>> but still it randomly gives exception at the line i mentioned in code where i apply string.Join(",", status.Value).TrimEnd();

What is actually going wrong and how i can solve it ? (My assumption here is if i have ImmutableDictionary, it is thread safe and the original immutable Dictionary loDestStatus shouldn’t be changed).

New contributor

user27330175 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

You can see from your stack trace that the problem happens when you iterate the List<T> during the string.Join call:

at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at System.String.Join[T](String separator, IEnumerable`1 values)

List<T> is not thread-safe. From the documentation:

It is safe to perform multiple read operations on a List<T>, but issues can occur if the collection is modified while it’s being read. To ensure thread safety, lock the collection during a read or write operation. To enable a collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization. For collections with built-in synchronization, see the classes in the System.Collections.Concurrent namespace. For an inherently thread-safe alternative, see the ImmutableList<T> class.

So, how you want to solve this depends on how exactly you’re using that list.


One solution is to keep using a List<T>, but make sure that you acquire a lock before reading or writing it. You could use the List<T> itself as the lock object, or have a dedicated lock:

public class ListAndLock<T>
{
    public List<T> List { get; } = new();
    public object LockObject { get; } = new();
}

Then your dictionary becomes:

ImmutableDictionary<string, ListAndLock<int>>

To add an item to the list, you need to acquire the lock first:

ListAndLock<int> listAndLock = ...;
lock (listAndLock.LockObject)
{
    listAndLock.List.Add(...);
}

And iterating the list becomes:

string value;
lock (status.Value.LockObject)
{
    value = string.Join(",", status.Value.List).TrimEnd();
}

Another option is to reach for a suitable thread-safe collection.

For example, if the order of items in your list doesn’t actually matter, you could use a ConcurrentBag<T>:

ImmutableDictionary<string, ConcurrentBag<int>>

It is safe to read from a ConcurrentBag<T> while also adding elements to it.

If order does matter, you could make use of ConcurrentStack<T> or ConcurrentQueue<T>.


Another option is to go down the immutable collections route, and use an ImmutableList<T>.

This does make adding elements slightly harder, as you need to modify the ImmutableDictionary<T>:

ImmutableDictionary<string, ImmutableList<int>> dict = ...;
var newList = dict[key].Add(...);
dict = dict.SetItem(key, newList);

… and if multiple threads are doing this to the dictionary at the same time, you need to make sure that modifications made by one of the threads aren’t thrown away by the other thread.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật