- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.2k
Closed
Description
Lines 85 to 97 in 93eacbd
| 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:
- Thread 1 sees _importsCache == nullandvalue == nulland enters the first conditional
- Thread 2 sees _importsCache == nullandvalue == nulland enters the first conditional too
- Thread 1 takes the lock, sees value == nulland initializes_importsCacheto a new dictionary
- Thread 2 takes the lock, see the local variable valueis still null, and then re-initializes_importsCacheto another value.
Is this correct/possible?