From 5aef78c89fbb54a5531834cdc8a4e60a8255f7e2 Mon Sep 17 00:00:00 2001 From: Einar Omang Date: Tue, 3 Sep 2024 09:29:14 +0200 Subject: [PATCH] Fix race condition in #2536 This fixes a race condition in BsonMapper, caused by a fix to a different issue in #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. --- LiteDB/Client/Mapper/BsonMapper.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/LiteDB/Client/Mapper/BsonMapper.cs b/LiteDB/Client/Mapper/BsonMapper.cs index c613e7bc3..d4fe83777 100644 --- a/LiteDB/Client/Mapper/BsonMapper.cs +++ b/LiteDB/Client/Mapper/BsonMapper.cs @@ -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; } ///