genaray/Arch

Concurrency issues with component type registration result in duplicate key exception

Opened this issue · 1 comments

This is in 1.3.0-alpha. Situation is running unit tests, so likely a concurrency issue where multiple threads are (indirectly) registering component types at the some time. I would suggest ComponentRegistry.Add should lock, double check the component type hasn't already been registered, and then proceed to register - returning the existing entry if it has been registered prior.

Otherwise the solution is for Arch users to preregister components in any situation where they may be utilizing multiple threads - running server and client threads, running multiple worlds on different threads, etc.

An exception of type 'System.ArgumentException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'An item with the same key has already been added. Key: TestStringComponent'
   at System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException[T](T key)
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at Arch.Core.Utils.ComponentRegistry.Add(Type type, Int32 typeSize)
   at Arch.Core.Utils.ComponentRegistry.Add(Type type)
   at Arch.Core.Utils.Component.GetComponentType(Type type)
   at Arch.Core.Utils.ComponentType.op_Implicit(Type value)

Adding a simple lock like this seems to fix it. (or at least my issue, which should be from the same root cause)
image