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 the locking in ReflectionComposablePart correct? #103650

Closed
omajid opened this issue Jun 18, 2024 · 4 comments · Fixed by #103660
Closed

Is the locking in ReflectionComposablePart correct? #103650

omajid opened this issue Jun 18, 2024 · 4 comments · Fixed by #103660

Comments

@omajid
Copy link
Member

omajid commented Jun 18, 2024

var value = _importsCache;
if (value == null)
{
lock (_lock)
{
if (value == null)
{
value = new Dictionary<ImportDefinition, ImportingItem>();
_importsCache = value;
}
}
}
return value;

https://github.com/dotnet/runtime/blob/main/docs/design/specs/Memory-model.md describes how Object assignment to a location potentially accessible by other threads is a release with respect to accesses to the instance’s fields/elements and metadata. But this method only uses the local variable value for locking. Any updates to value will not be seen by other threads. It seems like this might be possible:

  1. Thread 1 sees _importsCache == null and value == null and enters the first conditional
  2. Thread 2 sees _importsCache == null and value == null and enters the first conditional too
  3. Thread 1 takes the lock, sees value == null and initializes _importsCache to a new dictionary
  4. Thread 2 takes the lock, see the local variable value is still null, and then re-initializes _importsCache to another value.

Is this correct/possible?

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jun 18, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jun 18, 2024
@MihaZupan MihaZupan added area-System.ComponentModel.Composition and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jun 18, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-componentmodel-composition
See info in area-owners.md if you want to be subscribed.

@jkotas jkotas added the bug label Jun 18, 2024
@stephentoub
Copy link
Member

This is broken.

should be checking the field not the local.

@omajid
Copy link
Member Author

omajid commented Jun 18, 2024

Same issue in

var value = _importValues;
if (value == null)
{
lock (_lock)
{
value = _importValues;
if (value == null)
{
value = new Dictionary<ImportDefinition, object?>();
_importValues = value;
}
}
}
return value;

@stephentoub
Copy link
Member

Same issue in

That one's ok. Note L69.

omajid added a commit to omajid/dotnet-runtime that referenced this issue Jun 18, 2024
The second check needs to use the value from the field (to see updates
made by other threads), not the local variable.

Fixes: dotnet#103650
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants