Skip to content

Commit

Permalink
Fix #2488 LockRecursionException when doing FindById
Browse files Browse the repository at this point in the history
  • Loading branch information
JKamsker committed Jun 1, 2024
1 parent 5a46966 commit 3609f6e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 21 deletions.
36 changes: 36 additions & 0 deletions LiteDB.Tests/Issues/Issue2471_Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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().Throw<LockRecursionException>();
act.Should().NotThrow();
}
}
53 changes: 32 additions & 21 deletions LiteDB/Engine/Query/QueryExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using static LiteDB.Constants;
Expand Down Expand Up @@ -130,28 +130,9 @@ IEnumerable<BsonDocument> RunQuery()

using (var enumerator = pipe.Pipe(nodes, queryPlan).GetEnumerator())
{
var read = false;

try
{
read = enumerator.MoveNext();
}
catch (Exception ex)
{
_state.Handle(ex);
throw ex;
}

while (read)
{
_cursor.Fetched++;
_cursor.Elapsed.Stop();

yield return enumerator.Current;

if (transaction.State != TransactionState.Active) throw new LiteException(0, $"There is no more active transaction for this cursor: {_cursor.Query.ToSQL(_cursor.Collection)}");

_cursor.Elapsed.Start();
var read = false;

try
{
Expand All @@ -162,6 +143,36 @@ IEnumerable<BsonDocument> RunQuery()
_state.Handle(ex);
throw ex;
}

while (read)
{
_cursor.Fetched++;
_cursor.Elapsed.Stop();

yield return enumerator.Current;

if (transaction.State != TransactionState.Active) throw new LiteException(0, $"There is no more active transaction for this cursor: {_cursor.Query.ToSQL(_cursor.Collection)}");

_cursor.Elapsed.Start();

try
{
read = enumerator.MoveNext();
}
catch (Exception ex)
{
_state.Handle(ex);
throw ex;
}
}
}
finally
{
if (isNew)
{
_monitor.ReleaseTransaction(transaction);
isNew = false;
}
}
}

Expand Down

0 comments on commit 3609f6e

Please # to comment.