From ce46ced0f52614bada35ecd5e3473d244642eaaf Mon Sep 17 00:00:00 2001 From: Tiago Rodrigues <64850634+1Krypt0@users.noreply.github.com> Date: Tue, 27 Oct 2026 15:12:58 +0000 Subject: [PATCH] Fix "Collection was modified" in LocalEventBus when handlers subscribe during a publish Loop over a copy of each handler list taken under its lock, as EventBusBase.TriggerHandlersAsync does since #21786. Resolves #26342 --- .../Volo/Abp/EventBus/Local/LocalEventBus.cs | 6 +- .../Local/EventBus_Concurrency_Test.cs | 83 +++++++++++++++++++ 2 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_Concurrency_Test.cs diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs index 729afa2915..8411a50a0d 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs @@ -217,7 +217,7 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency foreach (var handlerFactory in HandlerFactories.Where(hf => ShouldTriggerEventForHandler(eventType, hf.Key))) { - foreach (var factory in handlerFactory.Value) + foreach (var factory in handlerFactory.Value.Locking(factories => factories.ToArray())) { handlerFactoryList.Add(new Tuple( factory, @@ -228,7 +228,7 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency foreach (var handlerFactory in DynamicEventHandlerFactories.Where(aehf => eventNames.Contains(aehf.Key))) { - foreach (var factory in handlerFactory.Value) + foreach (var factory in handlerFactory.Value.Locking(factories => factories.ToArray())) { handlerFactoryList.Add(new Tuple( factory, @@ -252,7 +252,7 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency foreach (var handlerFactory in DynamicEventHandlerFactories.Where(aehf => aehf.Key == eventName)) { - foreach (var factory in handlerFactory.Value) + foreach (var factory in handlerFactory.Value.Locking(factories => factories.ToArray())) { using var handler = factory.GetHandler(); var handlerType = handler.EventHandler.GetType(); diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_Concurrency_Test.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_Concurrency_Test.cs new file mode 100644 index 0000000000..62688199b5 --- /dev/null +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_Concurrency_Test.cs @@ -0,0 +1,83 @@ +using System.Threading; +using System.Threading.Tasks; +using Xunit; + +namespace Volo.Abp.EventBus.Local; + +public class EventBus_Concurrency_Test : EventBusTestBase +{ + private const int PublishCount = 10_000; + + [Fact] + public async Task Should_Publish_While_Other_Handlers_Subscribe_And_Unsubscribe() + { + var handledCount = 0; + using var subscription = LocalEventBus.Subscribe(_ => + { + Interlocked.Increment(ref handledCount); + return Task.CompletedTask; + }); + + using var cts = new CancellationTokenSource(); + var churn = Task.Run(() => + { + while (!cts.IsCancellationRequested) + { + LocalEventBus.Subscribe(_ => Task.CompletedTask).Dispose(); + } + }); + + try + { + for (var i = 0; i < PublishCount; i++) + { + await LocalEventBus.PublishAsync(new MySimpleEventData(1), onUnitOfWorkComplete: false); + } + } + finally + { + cts.Cancel(); + await churn; + } + + Assert.Equal(PublishCount, handledCount); + } + + [Fact] + public async Task Should_Publish_Dynamic_Event_While_Other_Handlers_Subscribe_And_Unsubscribe() + { + const string eventName = "MyConcurrencyTestDynamicEvent"; + var handledCount = 0; + using var subscription = LocalEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory( + new ActionEventHandler(_ => + { + Interlocked.Increment(ref handledCount); + return Task.CompletedTask; + }))); + + using var cts = new CancellationTokenSource(); + var churn = Task.Run(() => + { + while (!cts.IsCancellationRequested) + { + LocalEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory( + new ActionEventHandler(_ => Task.CompletedTask))).Dispose(); + } + }); + + try + { + for (var i = 0; i < PublishCount; i++) + { + await LocalEventBus.PublishAsync(eventName, new { Value = 1 }, onUnitOfWorkComplete: false); + } + } + finally + { + cts.Cancel(); + await churn; + } + + Assert.Equal(PublishCount, handledCount); + } +}