mbdavid / LiteDB

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

[BUG] Invalid Data on xxx. Full zero: False. Page Type: Empty #1872

Open gasparecoolshop opened 4 years ago

gasparecoolshop commented 4 years ago

Version LiteDB 5.0.9 WPF with NetCore3.1 Xamarin with Mono 6.12

Describe the bug When trying to execute a query after the app started, a LiteDB.LiteException is thrown: Invalid Data on 263. Full zero: False. Page Type: Empty. Prev/Next: 4294967295/262. UniqueID: 94. ShareCounter: 1.

Code to Reproduce Couldn't reproduce systematically, it happens rarely. The first query executed by the app is always:

collection.Query()
                .Where(Query.GT("$.seq", new BsonValue("")))
                .ToArray()
                .Select(d => int.Parse((string) d["seq"]))
                .DefaultIfEmpty(0)
                .Max();

Some EnsureIndex are executed before this query, including the one for "$.seq"

Screenshots/Stacktrace

LiteDB.Engine.DataPage in .ctor at line 21:49

LiteDB.Engine.BasePage in ReadPage at line 736:48

LiteDB.Engine.Snapshot in ReadPage at line 232:17

LiteDB.Engine.Snapshot in GetPage at line 175:13

LiteDB.Engine.DataService in Read at line 164:17

LiteDB.Engine.BufferReader in MoveFordward at line 74:17

LiteDB.Engine.BufferReader in Read at line 117:17

LiteDB.Engine.BufferReader in ReadNumber at line 271:17

LiteDB.Engine.BufferReader in ReadInt32 at line 279:37

LiteDB.Engine.BufferReader in ReadDocument at line 441:13

LiteDB.Engine.BufferReader in ReadElement at line 529:17

LiteDB.Engine.BufferReader in ReadDocument at line 451:17

LiteDB.Engine.BufferReader in ReadElement at line 529:17

LiteDB.Engine.BufferReader in ReadDocument at line 451:17

LiteDB.Engine.DatafileLookup in Load at line 33:17

LiteDB.Engine.DatafileLookup in Load at line 26:13

LiteDB.Engine.BasePipe in LoadDocument at line 36:17

LiteDB.Engine.QueryPipe in Select at line 85:30

LiteDB.Engine.QueryExecutor+<>c__DisplayClass10_0+<<ExecuteQuery>g__RunQuery|0>d in MoveNext at line 119:38

LiteDB.BsonDataReader in Read at line 87:21

LiteDB.LiteQueryable`1 in ToDocuments at line 248:17

Called from: System.Linq.Enumerable+SelectEnumerableIterator`2 in ToArray

LiteDB.LiteQueryable`1 in ToArray at line 286:13
gasparecoolshop commented 4 years ago

@lbnascimento if you need a corrupted database where this problem occurred, I can provide you one in private

curtisshipley commented 3 years ago

@gasparecoolshop Out of curiosity, are you using the repository pattern (https://github.com/mbdavid/LiteDB/wiki/Repository-Pattern) or are you using the database as a singleton?

We're using the repository pattern but on multiple threads. It seems the likely culprit would be some kind of concurrency issue. Makes me wonder if using a concurrency protected singleton might solve the issues in the short term. Any thoughts?

workflowdigital-george commented 3 years ago

I have the same problem. Using LiteDB in conjuction with the singleton pattern. Problem occurs very rarely but enough for it to be annoying.

Is there a way to recover from this? I.E being able to identify what collection is causing the problem and dropping/recreating?