mbdavid / LiteDB

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

[BUG] System.InvalidOperationException on insert/query #1472

Open Lupinho opened 4 years ago

Lupinho commented 4 years ago

LiteDB 5.0.2

Now I get this error:

System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Collections.Generic.Dictionary2.Enumerator.MoveNext() at LiteDB.BsonDocument.CopyTo(BsonDocument other) at LiteDB.Engine.IndexCost..ctor(CollectionIndex index, BsonExpression expr, BsonExpression value, Collation collation) at LiteDB.Engine.QueryOptimization.ChooseIndex(HashSet1 fields) at LiteDB.Engine.QueryOptimization.DefineIndex() at LiteDB.Engine.QueryOptimization.ProcessQuery() at LiteDB.Engine.QueryExecutor.<>c__DisplayClass10_0.<g__RunQuery

This must be multithreading issue. I bulk-insert many documents while querying the same collection.

mbdavid commented 4 years ago

Hi @Lupinho, can you paste your unit test code to check this?

Lupinho commented 4 years ago

At this time I have no unit test for this error. Anyway I can't get around this error in my application and so LiteDB is not useable for me in this state. The situation is the same as in the other error I reported, which was fixed: I bulk-insert many documents while querying at the same collection. I have only two collections, where one is DbRef-ed by the other. For me the error looks like a Bson-Document is read out (BsonDocument.CopyTo), while another thread modified the same Bson-Document (in the update thread).

Lupinho commented 4 years ago

Test-wise I build in a retry to BsonDocument.CopyTo; this seemed to work. Synchronizing the access to the Dictionary did nit work on first try and may slow down normal access. But I dont know why there is concurrency on these documents and what is the concept in LiteDB to deal with concurrency.

Lupinho commented 4 years ago

I could not reproduce the original exception with the current release (5.0.5). But: now I get the same error on disposing: System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Collections.Generic.Dictionary```2.ValueCollection.Enumerator.MoveNext() at System.Linq.Enumerable.WhereEnumerableIterator```1.MoveNext() at LiteDB.Engine.Snapshot.<GetWritablePages>d__22.MoveNext() at System.Linq.Enumerable.WhereSelectEnumerableIterator```2.MoveNext() at LiteDB.Engine.DiskService.DiscardCleanPages(IEnumerable```1 pages) at LiteDB.Engine.TransactionService.Dispose() at LiteDB.Engine.TransactionMonitor.Dispose() at LiteDB.Engine.LiteEngine.Dispose(Boolean disposing) at LiteDB.Engine.LiteEngine.Dispose() at LiteDB.LiteDatabase.Dispose(Boolean disposing) at LiteDB.LiteDatabase.Dispose()

lbnascimento commented 4 years ago

@Lupinho v5.0.7 was released earlier this week, could you test with it?

Lupinho commented 4 years ago

If tested with 5.0.7 also. The problem seems to be provocated on my side: I think I did queries in parallel when disposing the Database. Decide, if the exception above is fine in this case; I would rather have seen something like a message "rejected query on a data base while disposing" or similar...

leandromolina commented 3 years ago

LiteDB 5.0.9

hi, i am facing the exact same problem when i try to bulk insert documents into a collection, both from different threads and from the same thread. It's honestly a shame because this bug renders LiteDB unusable in my project. No solution yet?