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

Concurrency issues with component type registration result in duplicate key exception #239

Open
immortius opened this issue Oct 14, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@immortius
Copy link

immortius commented Oct 14, 2024

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)
@emelrad12
Copy link
Contributor

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

@genaray genaray added the bug Something isn't working label Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Todo
Development

No branches or pull requests

3 participants