Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to safely use eventBus. AsynRegister #6737

Closed
bxjg1987 opened this issue Jul 1, 2023 · 4 comments · Fixed by #6805
Closed

How to safely use eventBus. AsynRegister #6737

bxjg1987 opened this issue Jul 1, 2023 · 4 comments · Fixed by #6805
Assignees
Milestone

Comments

@bxjg1987
Copy link

bxjg1987 commented Jul 1, 2023

abp 8.2

I am using the blazer server and want to notify the UI when entities change, so I need to dynamically register events.

Thread A is triggering an event, while thread B is registering or unregistering the event,

The foreach loop in thread A will prompt the error that the collection has been modified.

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at Abp.Events.Bus.EventBus.Trigger(Type eventType, Object eventSource, IEventData eventData)
   at Abp.Domain.Uow.UnitOfWorkBase.CompleteAsync()
   at ZLJ.App.Admin.Equipments.EquipmentInstances.BXJGEquipmentInstanceAppService.ChangeLifeCycleStatus(BatchChangeLifetimeStatusInput input) in D:\fyj\back\src\ZLJ.Application\Equipments\EquipmentInstances\BXJGEquipmentInstanceAppService.cs:line 315

There are two ways I envision to handle it:

  1. Lock, not advisable, performance is too low in high concurrency scenarios.
  2. When traversing, the original collection calls ToList()
@ismcagdas
Copy link
Member

Could you provide sample code to reproduce this problem ?

@netty2019
Copy link
Contributor

netty2019 commented Jul 3, 2023

Could you provide sample code to reproduce this problem ?
@ismcagdas
I have encountered the same problem, attached is the console demo
EventBusConsoleDemo.zip
bd2d4a49b71fd6a4cadcbf0591a2255

@netty2019
Copy link
Contributor

@ismcagdas
image

@ismcagdas ismcagdas added this to the v8.3 milestone Jul 3, 2023
@ismcagdas ismcagdas modified the milestones: v8.3, v8.4 Jul 27, 2023
@HenriqueHilleshein
Copy link

HenriqueHilleshein commented Jul 28, 2023

I think this issue might be tightly related to the issue that I opened. #6726
I think if the collection of the event handlers is changed while triggering an event, this exception will be thrown.
Maybe this issue is then a duplicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants