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

Is this locking correct in ModuleDefinition? #951

Open
omajid opened this issue Jun 19, 2024 · 1 comment
Open

Is this locking correct in ModuleDefinition? #951

omajid opened this issue Jun 19, 2024 · 1 comment

Comments

@omajid
Copy link

omajid commented Jun 19, 2024

if (assembly_resolver.value == null) {
lock (module_lock) {
assembly_resolver = Disposable.Owned (new DefaultAssemblyResolver () as IAssemblyResolver);
}
}
return assembly_resolver.value;

It seems like this sequence of events might be possible, resulting in creation of an DefaultAssemblyResolver instance that's never disposed:

  1. Thread 1 sees assembly_resolver.value == null
  2. Thread 2 sees assembly_resolver.value == null
  3. Thread 1 enters the lock block and sets assembly_resolver to a new instance
  4. Thread 2 enters the lock block and re-sets assembly_resolver to a new instance

The instance in step 3 is now lost and was never disposed.

@ltrzesniewski
Copy link
Contributor

I agree, using if/lock/if would be appropriate as it's a standard pattern, or just changing = to ??= in this case.

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

No branches or pull requests

2 participants