Skip to content

Commit 61ddcf3

Browse files
committed
Fix potential concurrency issue in GetEntityMapper caused by #2566
1 parent caf6158 commit 61ddcf3

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.IO;
2+
using Xunit;
3+
4+
namespace LiteDB.Tests.Database;
5+
6+
public class Writing_While_Reading_Test
7+
{
8+
[Fact]
9+
public void Test()
10+
{
11+
using var f = new TempFile();
12+
using (var db = new LiteDatabase(f.Filename))
13+
{
14+
var col = db.GetCollection<MyClass>("col");
15+
col.Insert(new MyClass { Name = "John", Description = "Doe" });
16+
col.Insert(new MyClass { Name = "Joana", Description = "Doe" });
17+
col.Insert(new MyClass { Name = "Doe", Description = "Doe" });
18+
}
19+
20+
21+
using (var db = new LiteDatabase(f.Filename))
22+
{
23+
var col = db.GetCollection<MyClass>("col");
24+
foreach (var item in col.FindAll())
25+
{
26+
item.Description += " Changed";
27+
col.Update(item);
28+
}
29+
30+
db.Commit();
31+
}
32+
33+
34+
using (var db = new LiteDatabase(f.Filename))
35+
{
36+
var col = db.GetCollection<MyClass>("col");
37+
foreach (var item in col.FindAll())
38+
{
39+
Assert.EndsWith("Changed", item.Description);
40+
}
41+
}
42+
}
43+
44+
class MyClass
45+
{
46+
public int Id { get; set; }
47+
48+
public string Name { get; set; }
49+
50+
public string Description { get; set; }
51+
}
52+
}

LiteDB/Client/Mapper/BsonMapper.GetEntityMapper.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ internal EntityMapper GetEntityMapper(Type type)
3030
{
3131
this.BuildEntityMapper(mapper);
3232
}
33-
cts.Cancel();
34-
cts.Dispose();
33+
else
34+
{
35+
if (!_entities.TryGetValue(type, out mapper))
36+
{
37+
throw new LiteException(LiteException.MAPPER_NOT_FOUND, $"EntityMapper for type {type} was not found.");
38+
}
39+
}
3540

3641
return mapper;
3742
}

LiteDB/Utils/LiteException.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class LiteException : Exception
5858
public const int INVALID_PASSWORD = 217;
5959
public const int ILLEGAL_DESERIALIZATION_TYPE = 218;
6060
public const int ENTITY_INITIALIZATION_FAILED = 219;
61+
public const int MAPPER_NOT_FOUND = 220;
6162

6263
public const int INVALID_DATAFILE_STATE = 999;
6364

0 commit comments

Comments
 (0)