Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ead lock when done reading
  • Loading branch information
JKamsker committed Jun 1, 2024
1 parent 5a46966 commit ece59bb
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 17 deletions.
49 changes: 49 additions & 0 deletions LiteDB.Tests/Issues/Issue2471_Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using FluentAssertions;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Xunit;

namespace LiteDB.Tests.Issues;

public class Issue2471_Test
{
[Fact]
public void TestFragmentDB_FindByIDException()
{
using var db = new LiteDatabase(":memory:");
var collection = db.GetCollection<object>("fragtest");

var fragment = new object { };
var id = collection.Insert(fragment);

id.Should().BeGreaterThan(0);

var frag2 = collection.FindById(id);
frag2.Should().NotBeNull();

Action act = () => db.Checkpoint();

act.Should().NotThrow();
}

[Fact]
public void MultipleReadCleansUpTransaction()
{
using var database = new LiteDatabase(":memory:");

var collection = database.GetCollection("test");
collection.Insert(new BsonDocument { ["_id"] = 1 });

for (int i = 0; i < 500; i++)
{
collection.FindById(1);
}
}
}
28 changes: 11 additions & 17 deletions LiteDB/Engine/Query/QueryExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using LiteDB.Utils.Extensions;

using System;
using System.Collections.Generic;
using System.Linq;

using static LiteDB.Constants;

namespace LiteDB.Engine
Expand Down Expand Up @@ -71,8 +74,14 @@ internal BsonDataReader ExecuteQuery(bool executionPlan)

transaction.OpenCursors.Add(_cursor);

var enumerable = RunQuery();
if (isNew)
{
enumerable = enumerable.OnDispose(() => _monitor.ReleaseTransaction(transaction));
}

// return new BsonDataReader with IEnumerable source
return new BsonDataReader(RunQuery(), _collection, _state);
return new BsonDataReader(enumerable, _collection, _state);

IEnumerable<BsonDocument> RunQuery()
{
Expand All @@ -89,11 +98,6 @@ IEnumerable<BsonDocument> RunQuery()

transaction.OpenCursors.Remove(_cursor);

if (isNew)
{
_monitor.ReleaseTransaction(transaction);
}

yield break;
}

Expand All @@ -111,11 +115,6 @@ IEnumerable<BsonDocument> RunQuery()

transaction.OpenCursors.Remove(_cursor);

if (isNew)
{
_monitor.ReleaseTransaction(transaction);
}

yield break;
}

Expand Down Expand Up @@ -169,11 +168,6 @@ IEnumerable<BsonDocument> RunQuery()
_cursor.Elapsed.Stop();

transaction.OpenCursors.Remove(_cursor);

if (isNew)
{
_monitor.ReleaseTransaction(transaction);
}
};
}

Expand Down
27 changes: 27 additions & 0 deletions LiteDB/Utils/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiteDB.Utils.Extensions
{
internal static class EnumerableExtensions
{
// calls method on dispose
public static IEnumerable<T> OnDispose<T>(this IEnumerable<T> source, Action onDispose)
{
try
{
foreach (var item in source)
{
yield return item;
}
}
finally
{
onDispose();
}
}
}
}

0 comments on commit ece59bb

Please sign in to comment.