mvvmlight icon indicating copy to clipboard operation
mvvmlight copied to clipboard

Intermittent 'enumeration modified' exception in SendToTargetOrType()

Open NickHarmer opened this issue 2 years ago • 1 comments

I am seeing very occasional 'enumeration is modified' exceptions when calling Messenger.Send(), as follows:

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.KeyCollection.Enumerator.MoveNext()
   at System.Linq.Enumerable.<TakeIterator>d__25`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at GalaSoft.MvvmLight.Messaging.Messenger.SendToTargetOrType[TMessage](TMessage message, Type messageTargetType, Object token) in C:\Users\lbugn\Documents\MVVMLight\GalaSoft.MvvmLight\GalaSoft.MvvmLight (PCL)\Messaging\Messenger.cs:line 671
   at GalaSoft.MvvmLight.Messaging.Messenger.Send[TMessage](TMessage message) in C:\Users\lbugn\Documents\MVVMLight\GalaSoft.MvvmLight\GalaSoft.MvvmLight (PCL)\Messaging\Messenger.cs:line 296

Looking at the source code I note that there is a lock around this use of the _recipientsOfSubclassesAction dictionary:

lock (_recipientsOfSubclassesAction)
{
    list = _recipientsOfSubclassesAction[type].Take(_recipientsOfSubclassesAction[type].Count()).ToList();
}

but not around this one:

// Clone to protect from people registering in a "receive message" method
// Correction Messaging BL0008.002
var listClone =
                    _recipientsOfSubclassesAction.Keys.Take(_recipientsOfSubclassesAction.Count()).ToList();

which seems to be the source of the error (line 671)

Should this be updated?

NickHarmer avatar Aug 19 '21 08:08 NickHarmer

@NickHarmer this library is no longer maintained as per the readme:

image

@lbugnion did you plan to archive the repo too?

michael-hawker avatar Aug 19 '21 09:08 michael-hawker