-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
#2536 Fix race condition in bsonmapper #2538
#2536 Fix race condition in bsonmapper #2538
Conversation
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.
I think this makes sense, though the whole thing is IMO a little unpleasant.
Probably better than my solution in terms of performance.
One thing I'm still unsure of is whether it is actually safe to concurrently insert into a dictionary while reading from it on another thread, I think there is a chance it fails, if the underlying storage needs to grow, but I'm not familiar enough with the details of Dictionary to say for sure.
/// <summary> | ||
/// Unfinished mapping cache between Class/BsonDocument | ||
/// </summary> | ||
private readonly Dictionary<Type, EntityMapper> _buildEntities = new Dictionary<Type, EntityMapper>(); |
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.
how about using ConcurrentDictionary?
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.
We don't need concurrent access to this because it is only ever used in building the type mapping, which is always happening in the same thread.
return mapper; | ||
} | ||
|
||
lock (_entities) |
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.
You mentioned that is not needed to use ConcurrentDictionary
, but we need a lock here in order to access and update its values (for both), are you sure isn't better to use that collection and avoid the lock
here?
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.
In the best case (which happens 99% of the time), we don`t lock. Only initially, when first creating the mapping, we need the lock.
In addidtion to that, this behaviour is the same as the original code before the bugfix that caused this bug
Fixes #2536