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

#2536 Fix race condition in bsonmapper #2538

Merged

Conversation

JKamsker
Copy link
Collaborator

@JKamsker JKamsker commented Sep 6, 2024

Fixes #2536

Copy link

@einarmo einarmo left a 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>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about using ConcurrentDictionary?

Copy link
Collaborator Author

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)
Copy link
Collaborator

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?

Copy link
Collaborator Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Race condition in BsonMapper
5 participants