"Collection was modified" error in Commerce Engine log


Description

An issue might occur when the Components collection of any Catalog Entity (SellableItem, Category, or Catalog) is modified during serialization and writing of Catalog Entities into the response body.

When the enumeration of Catalog Entity happens in OData presentation, the Components collection is compared on each iteration. The current and the previous versions are not equal, so the Collection was modified exception is thrown. The stack trace in Commerce log files is as follows:

ERROR Connection id ""0HLM9N12Q3EI6"", Request id ""0HLM9N12Q3EI6:00000003"": An unhandled exception was thrown by the application.
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.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataFeedSerializer.WriteFeed(IEnumerable enumerable, IEdmTypeReference feedType, ODataWriter writer, ODataSerializerContext writeContext)
   at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteExpandedNavigationProperty(KeyValuePair`2 navigationPropertyToExpand, EntityInstanceContext entityInstanceContext, ODataWriter writer)
   at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteExpandedNavigationProperties(IDictionary`2 navigationPropertiesToExpand, EntityInstanceContext entityInstanceContext, ODataWriter writer)
   at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteEntry(Object graph, ODataWriter writer, ODataSerializerContext writeContext)
   at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataFeedSerializer.WriteFeed(IEnumerable enumerable, IEdmTypeReference feedType, ODataWriter writer, ODataSerializerContext writeContext)
   at Microsoft.AspNetCore.OData.Formatter.ODataOutputFormatter.WriteResponseBody(OutputFormatterWriteContext context)
   at System.Threading.Tasks.Task.Execute()

Solution

To resolve this issue, download and install the following patch:

For Sitecore XC 9.0.3: https://github.com/sitecoresupport/Sitecore.Support.389445/releases

Follow the instructions in the readme file.