From 82e850673b9c0a8a848a9a37dcfa2377817b6362 Mon Sep 17 00:00:00 2001 From: Fred Silberberg Date: Wed, 2 Mar 2022 09:58:18 -0800 Subject: [PATCH] Account for possible cache invalidation on a different thread in assert. (#59842) Attempt at fixing https://github.com/dotnet/roslyn/issues/59395. Reviewing the codepath I don't see any other obvious points that could cause the assert to fail, so if it continues to reproduce after this change we can look at further logging to determine the root cause. --- src/Compilers/Core/Portable/Compilation/Compilation.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Compilers/Core/Portable/Compilation/Compilation.cs b/src/Compilers/Core/Portable/Compilation/Compilation.cs index 98a6a4ba981c1..c8eb1c9461bb4 100644 --- a/src/Compilers/Core/Portable/Compilation/Compilation.cs +++ b/src/Compilers/Core/Portable/Compilation/Compilation.cs @@ -1169,7 +1169,9 @@ public INamedTypeSymbol CreateNativeIntegerTypeSymbol(bool signed) { val = CommonGetTypeByMetadataName(fullyQualifiedMetadataName); var result = _getTypeCache.TryAdd(fullyQualifiedMetadataName, val); - Debug.Assert(result || (_getTypeCache.TryGetValue(fullyQualifiedMetadataName, out var addedType) && ReferenceEquals(addedType, val))); + Debug.Assert(result + || !_getTypeCache.TryGetValue(fullyQualifiedMetadataName, out var addedType) // Could fail if the type was already evicted from the cache + || ReferenceEquals(addedType, val)); } return val; } @@ -1196,8 +1198,8 @@ public ImmutableArray GetTypesByMetadataName(string fullyQuali val = getTypesByMetadataNameImpl(); var result = _getTypesCache.TryAdd(fullyQualifiedMetadataName, val); Debug.Assert(result - || (_getTypesCache.TryGetValue(fullyQualifiedMetadataName, out var addedArray) - && Enumerable.SequenceEqual(addedArray, val, ReferenceEqualityComparer.Instance))); + || !_getTypesCache.TryGetValue(fullyQualifiedMetadataName, out var addedArray) // Could fail if the type was already evicted from the cache + || Enumerable.SequenceEqual(addedArray, val, ReferenceEqualityComparer.Instance)); } return val;