Skip to content

Commit ca7037f

Browse files
committed
Updated the transaction finalizer to not try to release a lock that can never be successful
1 parent e55199d commit ca7037f

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

LiteDB/Engine/Services/TransactionMonitor.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
2-
using System.Collections.Concurrent;
32
using System.Collections.Generic;
43
using System.Linq;
5-
using System.Runtime.InteropServices;
64
using System.Threading;
75
using static LiteDB.Constants;
86

@@ -105,9 +103,10 @@ public TransactionService GetTransaction(bool create, bool queryOnly, out bool i
105103
}
106104

107105
/// <summary>
108-
/// Release current thread transaction
106+
/// Dispose and remove transaction from monitor
107+
/// without releasing thread lock
109108
/// </summary>
110-
public void ReleaseTransaction(TransactionService transaction)
109+
public bool RemoveTransaction(TransactionService transaction)
111110
{
112111
// dispose current transaction
113112
transaction.Dispose();
@@ -123,8 +122,16 @@ public void ReleaseTransaction(TransactionService transaction)
123122
_freePages += transaction.MaxTransactionSize;
124123

125124
// check if current thread contains more query transactions
126-
keepLocked = _transactions.Values.Any(x => x.ThreadID == Environment.CurrentManagedThreadId);
125+
return keepLocked = _transactions.Values.Any(x => x.ThreadID == Environment.CurrentManagedThreadId);
127126
}
127+
}
128+
129+
/// <summary>
130+
/// Release current thread transaction
131+
/// </summary>
132+
public void ReleaseTransaction(TransactionService transaction)
133+
{
134+
var keepLocked = RemoveTransaction(transaction);
128135

129136
// unlock thread-transaction only if there is no more transactions
130137
if (keepLocked == false)
@@ -150,7 +157,7 @@ public TransactionService GetThreadTransaction()
150157
{
151158
lock (_transactions)
152159
{
153-
return
160+
return
154161
_slot.Value ??
155162
_transactions.Values.FirstOrDefault(x => x.ThreadID == Environment.CurrentManagedThreadId);
156163
}
@@ -191,7 +198,7 @@ private int GetInitialSize()
191198
/// </summary>
192199
private bool TryExtend(TransactionService trans)
193200
{
194-
lock(_transactions)
201+
lock (_transactions)
195202
{
196203
if (_freePages >= _initialSize)
197204
{
@@ -211,7 +218,7 @@ private bool TryExtend(TransactionService trans)
211218
/// </summary>
212219
public bool CheckSafepoint(TransactionService trans)
213220
{
214-
return
221+
return
215222
trans.Pages.TransactionSize >= trans.MaxTransactionSize &&
216223
this.TryExtend(trans) == false;
217224
}
@@ -232,4 +239,4 @@ public void Dispose()
232239
}
233240
}
234241
}
235-
}
242+
}

LiteDB/Engine/Services/TransactionService.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using System;
2-
using System.Collections.Concurrent;
32
using System.Collections.Generic;
43
using System.Linq;
5-
using System.Runtime.InteropServices;
6-
using System.Threading;
74
using static LiteDB.Constants;
85

96
namespace LiteDB.Engine
@@ -444,7 +441,7 @@ protected virtual void Dispose(bool dispose)
444441
if (!dispose)
445442
{
446443
// Remove transaction monitor's dictionary
447-
_monitor.ReleaseTransaction(this);
444+
_monitor.RemoveTransaction(this);
448445
}
449446
}
450447
}

0 commit comments

Comments
 (0)