Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[BUG] System.InvalidOperationException: Collection was modified; enumeration operation may not execute. on multi thread inserts #2565

Open
megedsh opened this issue Nov 19, 2024 · 0 comments
Labels

Comments

@megedsh
Copy link

megedsh commented Nov 19, 2024

Version
LiteDB 5.0.21 / .NET 472

Describe the bug
InvalidOperationException Collection was modified when inserting from multiple threads
Bug could already be solved by 2536

Code to Reproduce
Sorry - the code is on a machine with no internet access
to reproduce do multiple serializations of the same type, from multiple threads using the same BsonMapper instance

Thread A enters GetEntityMapper and does not find the mapper.
Thread A calls BuildAddEntityMapper, and goes past, adding an entity mapper to the _entities dictionary but not yet updating the List Members of the mapper

thread B enters GetEntityMapper.
Thread B finds a mapper and returns, but this mapper is still being populated by thread A
Thread B Iterates over the List Members - while thread A adds to the same list

Thread B throws the exception below

Screenshots/Stacktrace

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.List1.Enumerator.MoveNextRare() at System.Linq.Enumerable.WhereListIterator1.MoveNext()
at LiteDB.BsonMapper.SerializeObject(Type type, Object obj, Int32 depth)
at LiteDB.BsonMapper.Serialize(Type type, Object obj, Int32 depth)
at LiteDB.BsonMapper.ToDocument(Type type, Object entity)
at LiteDB.BsonMapper.ToDocument[T](T entity)
at LiteDB.LiteCollection1.<GetBsonDocs>d__52.MoveNext() at LiteDB.Engine.LiteEngine.<>c__DisplayClass7_0.<Insert>b__0(TransactionService transaction) at LiteDB.Engine.LiteEngine.AutoTransaction[T](Func2 fn)

Additional context
this issue was probably introduced by this fix : 2493
so it might be short lived - I did not see any other similar issues reported - but you guys probably know better.

I am still waiting for the release of the fix 2536 to validate if it is fixed this current issue.

@megedsh megedsh added the bug label Nov 19, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant