Skip to content

Commit

Permalink
Fix race condition in mbdavid#2536
Browse files Browse the repository at this point in the history
This fixes a race condition in BsonMapper, caused by a fix to a
different issue in mbdavid#2493.

It seems like the current approach of checking the dictionary twice is
deliberate. That said, I don't believe reading from a dictionary
that may be in the process of being updated is actually safe to begin
with.
  • Loading branch information
einarmo committed Sep 3, 2024
1 parent 391cc93 commit 5aef78c
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions LiteDB/Client/Mapper/BsonMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,14 @@ internal EntityMapper GetEntityMapper(Type type)
{
//TODO: needs check if Type if BsonDocument? Returns empty EntityMapper?

if (!_entities.TryGetValue(type, out EntityMapper mapper))
lock (_entities)
{
lock (_entities)
if (!_entities.TryGetValue(type, out EntityMapper mapper))
{
if (!_entities.TryGetValue(type, out mapper))
return this.BuildAddEntityMapper(type);
return this.BuildAddEntityMapper(type);
}
return mapper;
}

return mapper;
}

/// <summary>
Expand Down

0 comments on commit 5aef78c

Please sign in to comment.