mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.52k stars 1.24k forks source link

[BUG] ReaderWriterLockSlim error on release if not held #2197

Closed dethknite closed 2 years ago

dethknite commented 2 years ago

LiteDB v5.0.11 / Win 11 / .NET 4.8

Ran into this during a system shutdown.

The read lock is being released without being held.
   at System.Threading.ReaderWriterLockSlim.ExitReadLock()
   at LiteDB.Engine.TransactionMonitor.ReleaseTransaction(TransactionService transaction)
   at LiteDB.Engine.TransactionService.Dispose(Boolean dispose)
   at LiteDB.Engine.TransactionService.Finalize()
dethknite commented 2 years ago

I believe the issue is here: https://github.com/mbdavid/LiteDB/blob/1e014473976c779e94dccad7664266c748b4e945/LiteDB/Engine/Services/LockService.cs#L57

If this is called and no read lock is held.. it shouldn't be trying to release it.

Fix:

public void ExitTransaction()
{
    // if current thread are in reserved mode, do not exit transaction (will be exit from ExitExclusive)
    if (_transaction.IsWriteLockHeld) return;

    if(_transaction.IsReadLockHeld)
        _transaction.ExitReadLock();
}
dethknite commented 2 years ago

Submitted a pull request with fix. https://github.com/mbdavid/LiteDB/pull/2198