System.Threading.SynchronizationLockException during LiteCollection.EnsureIndex
I've written a simple application that
- creates a new db
- creates a new collection
- bulk-writes 1.500.000 objects, 3.000 objects at a time in a for loop with InsertBulk
- calls EnsureIndex(x => x.TimeStamp) , where "TimeStamp" is a DateTime prop
When I run it I receive this exception:
System.Threading.SynchronizationLockException
HResult=0x80131518
Message=Object synchronization method was called from an unsynchronized block of code.
Source=System.Private.CoreLib
StackTrace:
at System.Threading.Monitor.Exit(Object obj)
at LiteDB.Engine.LockService.ExitLock(String collectionName)
at LiteDB.Engine.TransactionService.Rollback()
at LiteDB.Engine.LiteEngine.AutoTransaction[T](Func`2 fn)
at LiteDB.Engine.LiteEngine.EnsureIndex(String collection, String name, BsonExpression expression, Boolean unique)
at LiteDB.LiteCollection`1.EnsureIndex(String name, BsonExpression expression, Boolean unique)
at LiteDB.LiteCollection`1.EnsureIndex(BsonExpression expression, Boolean unique)
at LiteDB.LiteCollection`1.EnsureIndex[K](Expression`1 keySelector, Boolean unique)
at ConsoleApp8.Program.Main(String[] args) in [...]ConsoleApp8\Program.cs:line 72
If you need, I can attach the source of the app (single file) or the solution, let me know.
Greetings and thanks! :)
@mattiamerzi if the code is not private and it can reproduce the error/crash do post a minimal version of it. LiteDB needs more developers, trying to fix bugs with a code that can replicate the crash is a good way to attract more devs.
It looks like the code has InsertBulk as Obsolete. It looks like you would want to use Insert(IEnumerable
Here is what I did to test:
namespace TestLiteDB_Net5
{
class Program
{
static void Main(string[] args)
{
int i = 0;
int insBatch = 0;
List
while (i < 1500000)
{
i++;
insBatch++;
var test = new TestClass
{
Name = "John Doe John Doe John Doe John Doe John Doe John Doe John Doe",
Age = new Random().Next()
};
test1.Add(test);
if (insBatch == 3000)
{
//col.Insert(test1);
col.InsertBulk(test1, insBatch);
test1.Clear();
insBatch = 0;
}
}
}
}
}
}
Consolidated into #2803, which combines this report with the related ones and tracks the fix. Closing this as a duplicate — please follow #2803, and comment there if your case is different.