LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

The write lock is being released without being held

Open janstafa opened this issue 9 years ago • 7 comments

Hi David, I am getting the same error as here, I know the issue was closed but how do I avoid this? I use lock as you see below: https://github.com/mbdavid/LiteDB/issues/545

System.Threading.SynchronizationLockException: The write lock is being released without being held. at System.Threading.ReaderWriterLockSlim.ExitWriteLock () [0x0002a] in <63992662b765477a898ef49cdcc99ee2>:0 at LiteDB.LockService.b__17_2 () [0x00000] in :0 at LiteDB.LockService+<>c__DisplayClass11_0.b__0 () [0x0000b] in :0 at LiteDB.LockControl.Dispose () [0x00008] in :0 at LiteDB.LiteCollection`1[T].Insert (T document) [0x00075] in :0 at CacheProvider.InsertToCache (Data data) [0x0002f] in <0b0c1287d0f3439c815d09e4c543b346>:0

I am using lock but still getting the error when using it from multithread: How should I use it then? Thanks Jan

public bool InsertToCache(Data data) { try { lock (Lock) { var col = _db?.GetCollection(_collectionName); col?.Insert(data); return true; } } catch (Exception exception) { Log.Error(exception, "Error inserting entity to cache."); }

    return false;
}

janstafa avatar May 16 '17 07:05 janstafa

I have had success in using LiteDB from a background thread, but not from multiple threads. Bear in mind I also believe this to be an issue relating to unity's mono implementation of ReadWriterLockSlim, you are probably not using unity?

mcintyrg avatar May 18 '17 09:05 mcintyrg

I am not using unity. The issue shows in linux mono 5.0.0 and windows .net 4.6.2. I am using lock so I think that even when I run it on multiple threads the lock should help or not? How else should I use litedb if my app is multihreading?

janstafa avatar May 18 '17 09:05 janstafa

In my case I just made sure I created the db on main thread, then always used it on a single background thread. I didn't need extra locking as the collection utilises locks itself. while I had the same error, I haven't yet seen it again so far...

mcintyrg avatar May 18 '17 09:05 mcintyrg

Will try it. Thanks

janstafa avatar May 18 '17 10:05 janstafa

Any plans to solve this problem? I'm using it in multiple Tasks.

Error: System.Threading.SynchronizationLockException:
   in System.Threading.ReaderWriterLockSlim.ExitWriteLock()
   in LiteDB.LockControl.Dispose()
   in LiteDB.LiteEngine.Rollback()
   in LiteDB.LiteEngine.Transaction[T](String collection, Boolean addIfNotExists, Func`2 action)

And I also had this error (in another execution):

Error: System.InvalidCastException: Unable to cast object of type 'LiteDB.CollectionPage' to type 'LiteDB.IndexPage'.
   em LiteDB.PageService.GetPage[T](UInt32 pageID)
   em LiteDB.IndexService.Delete(CollectionIndex index, PageAddress nodeAddress)
   em LiteDB.LiteEngine.<>c__DisplayClass51_0.<Delete>b__0(CollectionPage col)
   em LiteDB.LiteEngine.Transaction[T](String collection, Boolean addIfNotExists, Func`2 action)

LiteDB 3.1 and Net Framework 4.5.2.

Thanks

CharlesToniolo avatar Aug 11 '17 01:08 CharlesToniolo

@CharlesToniolo, yes, try v3.5 dev version (in dev branch). It's on tests about this.

mbdavid avatar Aug 11 '17 12:08 mbdavid

Hi! With the objective of organizing our issues, we are closing old unsolved issues. Please check the latest version of LiteDB and open a new issue if your problem/question/suggestion still applies. Thanks!

lbnascimento avatar Feb 05 '20 17:02 lbnascimento