LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

System.Threading.SynchronizationLockException during LiteCollection.EnsureIndex

Open mattiamerzi opened this issue 5 years ago • 3 comments

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 avatar May 21 '21 23:05 mattiamerzi

@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.

batmanonline avatar May 22 '21 07:05 batmanonline

It looks like the code has InsertBulk as Obsolete. It looks like you would want to use Insert(IEnumerable entities) , however, I don't see a code difference between the two off hand. I did test both methods with 1,500,000 record inserts (3,000 at a time) and couldn't reproduce.

Here is what I did to test:

namespace TestLiteDB_Net5 { class Program { static void Main(string[] args) { int i = 0; int insBatch = 0; List test1 = new List(); using (var db = new LiteDatabase("test.db")) { var col = db.GetCollection();

            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;
                }
                
            }
        }
    }
}

}

ghost avatar May 26 '21 21:05 ghost

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.

JKamsker avatar Sep 12 '26 18:09 JKamsker