Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Intermittent 'enumeration modified' exception in SendToTargetOrType() #109

Open
NickHarmer opened this issue Aug 19, 2021 · 1 comment
Open

Comments

@NickHarmer
Copy link

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?

@michael-hawker
Copy link

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

image

@lbugnion did you plan to archive the repo too?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants