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

Locking in ProviderGraph to prevent duplicate code generation #3109

Merged

Conversation

felixkmh
Copy link
Contributor

@felixkmh felixkmh commented Apr 2, 2024

  • Implemented fix as discussed in Duplicate Type Generation #3083
    • Added single lock object to synchronize code generation
  • Added unit test to cover this fix
  • Confirm that the project builds
  • All automated tests succeed

@felixkmh felixkmh changed the title 3083 duplicate code generation Locking in ProviderGraph to prevent duplicate code generation Apr 2, 2024
@felixkmh felixkmh marked this pull request as ready for review April 2, 2024 18:05
@@ -23,7 +24,10 @@ public ProviderGraph(StoreOptions options)

public void Append<T>(DocumentProvider<T> provider) where T : notnull
{
_storage = _storage.AddOrUpdate(typeof(T), provider);
lock (_storageLock)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't do the lock here, it's unnecessary w/ the immutable hash map

Copy link
Contributor Author

@felixkmh felixkmh Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how ImHashMaps work exactly, but the reason I also locked it is because I thought that if two threads enter this method concurrently, then they call AddOrUpdate on the same instance. Then they each would create a new map based on that same map instance, resulting in two separate instances that do not contain the item the other thread tried to add. The assignment only happens after AddOrUpdate returns, so whoever assigns to _storage last "wins".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'll just add that as another test case and see if that can happen. Although tests of that nature aren't the most reliable.

Copy link
Contributor Author

@felixkmh felixkmh Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems that it is at least possible:
image
I can't speak to how likely that is in practice, but I'd argue that the impact of keeping the lock would be negligible.

@jeremydmiller
Copy link
Member

@felixkmh Thanks for taking this on!

@jeremydmiller jeremydmiller merged commit 1c094ea into JasperFx:master Apr 3, 2024
11 checks passed
@felixkmh felixkmh deleted the 3083_duplicate_code_generation branch April 5, 2024 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants