-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5633: BsonSerializer.IsTypeDiscriminated is not thread-safe #1722
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
Open
papafe
wants to merge
2
commits into
mongodb:main
Choose a base branch
from
papafe:csharp5633
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ public static class BsonSerializer | |
private static Dictionary<Type, IIdGenerator> __idGenerators = new Dictionary<Type, IIdGenerator>(); | ||
private static Dictionary<Type, IDiscriminatorConvention> __discriminatorConventions = new Dictionary<Type, IDiscriminatorConvention>(); | ||
private static Dictionary<BsonValue, HashSet<Type>> __discriminators = new Dictionary<BsonValue, HashSet<Type>>(); | ||
private static HashSet<Type> __discriminatedTypes = new HashSet<Type>(); | ||
private static ConcurrentDictionary<Type, bool> __discriminatedTypes = new (); | ||
private static BsonSerializerRegistry __serializerRegistry; | ||
private static TypeMappingSerializationProvider __typeMappingSerializationProvider; | ||
// ConcurrentDictionary<Type, object> is being used as a concurrent set of Type. The values will always be null. | ||
|
@@ -321,8 +321,7 @@ internal static bool IsDiscriminatorConventionRegisteredAtThisLevel(Type type) | |
/// <returns>True if the type is discriminated.</returns> | ||
public static bool IsTypeDiscriminated(Type type) | ||
{ | ||
var typeInfo = type.GetTypeInfo(); | ||
return typeInfo.IsInterface || __discriminatedTypes.Contains(type); | ||
return type.GetTypeInfo().IsInterface || __discriminatedTypes.ContainsKey(type); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -587,7 +586,7 @@ public static void RegisterDiscriminator(Type type, BsonValue discriminator) | |
// mark all base types as discriminated (so we know that it's worth reading a discriminator) | ||
for (var baseType = typeInfo.BaseType; baseType != null; baseType = baseType.GetTypeInfo().BaseType) | ||
{ | ||
__discriminatedTypes.Add(baseType); | ||
__discriminatedTypes.TryAdd(baseType, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it OK that we are ignoring the return value of I understand that we expect it to always return true given the check on line 582. Should we document that we are ignoring the return value on purpose by coding like this:
|
||
} | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetTypeInfo
is left over from when we used to target a very old .NET Core framework.I think this line can be just:
return type.IsInterface || __discriminatedTypes.ContainsKey(type);