LiteDB: [QUESTION] Object synchronization method was called from an unsynchronized block of code
I’m getting this error intermittently while running with ConnectionType.Shared and don’t with the default.
System.ApplicationException: Object synchronization method was called from an unsynchronized block of code.
at System.Threading.Mutex.ReleaseMutex()
at LiteDB.SharedEngine.CloseDatabase()
at LiteDB.SharedEngine.Upsert(String collection, IEnumerable`1 docs, BsonAutoId autoId)
System.ApplicationException: Object synchronization method was called from an unsynchronized block of code.
at System.Threading.Mutex.ReleaseMutex()
at LiteDB.SharedEngine.CloseDatabase()
at LiteDB.SharedDataReader.Dispose()
at LiteDB.LiteQueryable`1.ToDocuments()+MoveNext()
at System.Linq.Enumerable.SelectEnumerableIterator`2.ToList()
I tried researching this but haven’t come up with any ideas. Does anyone have any suggestions on why I’m getting this?
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 9
- Comments: 42 (2 by maintainers)
Commits related to this issue
- Used direct connection due to LiteDB issue https://github.com/mbdavid/LiteDB/issues/1546 — committed to zbalkan/IntegrityService by zbalkan 2 years ago
@a44281071 no this isn’t fixed in 5.0.12. Obviously multi-threading is complicated to get right. Unfortunately the readme file claims LiteDb is thread-safe, when it really isn’t fully (yet…).
same problem
Okay, I’ve found why it’s giving me thread-lock issues intermittently. To my understanding this is causing because of the proper wentout of dispose from multiple threads. I’m using repository pattern and unit of work together in code.
Here’s how I fixed (atleast for me)
Since I made repositories Scoped, The dependent service/business layer also need to be changed to scoped. Anyway this fixed my issue for now.
This problem still happen on version 5.0.11.
@nightroman Yes, I was able to reproduce the problem.
Hello, is there any progress in resolving this issue? I can see some commits in the “single_file” branch two months ago that might be fixes for this issue, a I right?
@MidasLamb
Mutex
is only used in theSharedEngine
, for synchronization between processes. TheLiteEngine
uses lock statements for thread synchronization.The
SharedEngine
will be reworked for future versions and will no longer useMutex
.I’ve the same problem with 5.0.5 on .Net core 3.1 when multiple API calls are trying to connect to the same database.
Update 1: In my application I use Autofac and configured it to create only a single instance of the LiteDB database (wrapped in a provider, so actually the provider was created once and the internal LiteDB as well). The connectionstring contains ‘Connection=shared’. In this situation I get the same error as described above.
I’ve changed my Autofac configuration so it will always create a new provider (with internally a new LiteDb instance). This solved this specific problem, but now I’m running into file in use problems?
Update 2: Well I now reverted the my Autofac configuration into what I initially had. So a single instance of the provider, with internally a single instance of LiteDB. Changed the connectionstring from ‘Connection=shared’ to ‘Connection=direct’ and this seems to be stable after a couple of tests.
I got the same error, too.