Skip to content

Commit

Permalink
Fix race condition in litedb-org#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 litedb-org#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 28fbe0e
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 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 Expand Up @@ -396,7 +394,7 @@ protected virtual CreateObject GetTypeCtor(EntityMapper mapper)
for (i = 0; i < pars.Length; i++)
{
ParameterInfo par = pars[i];
MemberMapper mi = null;
MemberMapper mi = null;
foreach (MemberMapper member in mapper.Members)
{
if (member.MemberName.ToLower() == par.Name.ToLower() && member.DataType == par.ParameterType)
Expand All @@ -405,10 +403,10 @@ protected virtual CreateObject GetTypeCtor(EntityMapper mapper)
break;
}
}
if (mi == null) {break;}
if (mi == null) { break; }
paramMap[i] = new KeyValuePair<string, Type>(mi.FieldName, mi.DataType);
}
if (i < pars.Length) { continue;}
if (i < pars.Length) { continue; }
CreateObject toAdd = (BsonDocument value) =>
Activator.CreateInstance(type, paramMap.Select(x =>
this.Deserialize(x.Value, value[x.Key])).ToArray());
Expand Down

0 comments on commit 28fbe0e

Please # to comment.