diff --git a/src/MudBlazor.UnitTests/Utilities/ObserverManager/ObserverManagerTests.cs b/src/MudBlazor.UnitTests/Utilities/ObserverManager/ObserverManagerTests.cs index d5b31ed56635..22fb547df449 100644 --- a/src/MudBlazor.UnitTests/Utilities/ObserverManager/ObserverManagerTests.cs +++ b/src/MudBlazor.UnitTests/Utilities/ObserverManager/ObserverManagerTests.cs @@ -275,4 +275,34 @@ async Task NotificationAsync(string observer) .VerifyLogging($"Adding entry for {DefunctObserverId}/{DefunctObserver}. 1 total observers after add.") .VerifyLogging($"Removing defunct entry for {DefunctObserverId}. 0 total observers after remove."); } + + [Test] + public void CollectionModified() + { + // Arrange + var observerManager = new ObserverManager(NullLogger.Instance); + + for (var i = 0; i < 1000; i++) + { + observerManager.Subscribe(i, i); + } + + bool Predicate(int id, int observer) + { + if (id == 500) + { + observerManager.Subscribe(1001, 1001); + } + + return true; + } + + Task NotificationAsync(int observer) + { + return Task.CompletedTask; + } + + // Act & Assert + Assert.DoesNotThrowAsync(() => observerManager.NotifyAsync(NotificationAsync, Predicate)); + } } diff --git a/src/MudBlazor/Utilities/ObserverManager/ObserverManager.cs b/src/MudBlazor/Utilities/ObserverManager/ObserverManager.cs index 52c38173b276..11460127c23e 100644 --- a/src/MudBlazor/Utilities/ObserverManager/ObserverManager.cs +++ b/src/MudBlazor/Utilities/ObserverManager/ObserverManager.cs @@ -114,7 +114,7 @@ public void Unsubscribe(TIdentity id) public async Task NotifyAsync(Func notification, Func? predicate = null) { var defunct = default(List); - foreach (var observer in _observers) + foreach (var observer in _observers.ToArray()) { // Skip observers which don't match the provided predicate. if (predicate != null && !predicate(observer.Key, observer.Value.Observer))