LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

Unable to cast object of type 'LiteDB.EmptyPage' to type 'LiteDB.HeaderPage'

Open ArefGihuni opened this issue 9 years ago • 55 comments

Closed the app and now it's happening.

   at LiteDB.PageService.GetPage[T](UInt32 pageID)
   at LiteDB.PageService.NewPage[T](BasePage prevPage)
   at LiteDB.IndexService.AddNode(CollectionIndex index, BsonValue key, Byte level, IndexNode last)
   at LiteDB.LiteEngine.UpdateDocument(CollectionPage col, BsonDocument doc)
   at LiteDB.LiteEngine.<>c__DisplayClass29_0.<Update>b__0(CollectionPage col)
   at LiteDB.LiteEngine.Transaction[T](String collection, Boolean addIfNotExists, Func`2 action)
   at LiteDB.LiteCollection`1.Update(T document)
   at MyAppAsync()
   at System.Threading.Tasks.Task.Execute()

ArefGihuni avatar Jul 02 '17 16:07 ArefGihuni

Are you using master branch version or 3.1.1 from Nuget?

mbdavid avatar Jul 02 '17 17:07 mbdavid

Yes, version 3.1.0 from Nuget .. 3.1.1 was too slow so I downgraded ..

it's a UWP windows 10 app

ArefGihuni avatar Jul 02 '17 17:07 ArefGihuni

I started getting InvalidCastException
Is there any way to fix it?

LiteDB.PageService.GetPage<System.__Canon>(Void* InstParam, UInt32 pageID) in at 0:0 
LiteDB.TransactionService.Commit() in at 0:0 
LiteDB.LiteEngine.Commit() in at 0:0 
LiteDB.LiteEngine.Transaction<System.Int32>$catch$0() in at 0:0 
LiteDB.LiteEngine.Insert(String collection, IEnumerable$1<$BsonDocument> docs) in at 0:0 
LiteDB.LiteEngine.Insert(String collection, $BsonDocument doc) in at 0:0 
LiteDB.LiteCollection$1<System.__Canon>.Insert(__Canon document) in at 0:0 

ArefGihuni avatar Jul 03 '17 17:07 ArefGihuni

this error occurs in some concurrency scenarios. I'm working on this to fix

mbdavid avatar Jul 03 '17 21:07 mbdavid

I have added the option FileOptions.WriteThrough when opening the data file at 2017.6.13. The program runs very well utill the same problem appeared 3 days ago again.It's strange, isn't?

The version of LiteDB is 3.1.1.0 with a modify of adding the option FileOptions.WriteThrough. The threading model is single instance.

The first exception is

System.Threading.SynchronizationLockException: The write lock is being released without being held. at System.Threading.ReaderWriterLockSlim.ExitWriteLock() at LiteDB.LockService.b__17_2() at LiteDB.LockService.<>c__DisplayClass11_0.b__0() at LiteDB.LockControl.Dispose() at LiteDB.LiteCollection`1.Insert(T document)

Then, It came after the program being restarted

System.InvalidCastException: Unable to cast object of type 'LiteDB.EmptyPage' to type 'LiteDB.HeaderPage'. at LiteDB.PageService.GetPage[T](UInt32 pageID) at LiteDB.PageService.NewPage[T](BasePage prevPage) at LiteDB.PageService.GetFreePage[T](UInt32 startPageID, Int32 size) at LiteDB.IndexService.AddNode(CollectionIndex index, BsonValue key, Byte level, IndexNode last) at LiteDB.LiteEngine.InsertDocument(CollectionPage col, BsonDocument doc) at LiteDB.LiteEngine.<>c__DisplayClass61_0.b__0(CollectionPage col) at LiteDB.LiteEngine.Transaction[T](String collection, Boolean addIfNotExists, Func2 action) at LiteDB.LiteEngine.Insert(String collection, IEnumerable1 docs) at LiteDB.LiteCollection1.Insert(IEnumerable1 docs)

@mbdavid

lidanger avatar Jul 10 '17 11:07 lidanger

Hi @lidanger, I'm working on it in "nojournal" branch. It's not finish, but you can check.

mbdavid avatar Jul 10 '17 11:07 mbdavid

@mbdavid, that's good.I'll try.

lidanger avatar Jul 10 '17 11:07 lidanger

Hi,

I have a similar issue, here is my stack trace:

System.InvalidCastException: 'Unable to cast object of type 'LiteDB.EmptyPage' to type 'LiteDB.IndexPage'.'

at LiteDB.PageService.GetPage[T](UInt32 pageID) at LiteDB.PageService.GetFreePage[T](UInt32 startPageID, Int32 size) at LiteDB.IndexService.AddNode(CollectionIndex index, BsonValue key, Byte level, IndexNode last) at LiteDB.LiteEngine.InsertDocument(CollectionPage col, BsonDocument doc) at LiteDB.LiteEngine.<>c__DisplayClass33_0.b__0(CollectionPage col) at LiteDB.LiteEngine.Transaction[T](String collection, Boolean addIfNotExists, Func2 action) at LiteDB.LiteEngine.Upsert(String collection, IEnumerable1 docs) at LiteDB.LiteEngine.Upsert(String collection, BsonDocument doc) at LiteDB.LiteCollection`1.Upsert(T document)

Thanks for investigating

trichling avatar Aug 08 '17 05:08 trichling

Hi, I have a similar issue too , here is my stack trace: version is 4.1.1

System.InvalidCastException: Unable to cast object of type 'LiteDB.EmptyPage' to type 'LiteDB.DataPage'. at LiteDB.PageService.GetPage[T](UInt32 pageID) at LiteDB.QueryCursor.Fetch(TransactionService trans, DataService data, BsonReader bsonReader) at LiteDB.LiteEngine.d__9.MoveNext() at LiteDB.LiteEngine.d__13.MoveNext() at LiteDB.LiteCollection1.<Find>d__17.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at xxxCache.<>c__DisplayClass29_0.b__0(LiteDatabase db) at xxxCache.Execute[TReturn](Func`2 execute)

JasonGrass avatar Feb 01 '18 09:02 JasonGrass

Hi @JasonGrass, I never got this error anymore (on v4.x). Can you use LiteDB.Demo project (inside LiteDB source) to make a replication of this error?

mbdavid avatar Feb 01 '18 09:02 mbdavid

Here is the broken db file , But it can be read by using shell command.
I do not know how it been broken.

CoursewareCache.zip

snipaste_2018-02-02_09-11-15

snipaste_2018-02-02_09-14-10

JasonGrass avatar Feb 02 '18 01:02 JasonGrass

Same thing. Got this exception recently. Version is 4.1.1 (from Nuget)

System.InvalidCastException: Unable to cast object of type 'LiteDB.EmptyPage' to type 'LiteDB.IndexPage'.
   at LiteDB.PageService.GetPage[T](UInt32 pageID)
   at LiteDB.IndexService.<FindAll>d__13.MoveNext()
   at LiteDB.QueryCursor.Fetch(TransactionService trans, DataService data, BsonReader bsonReader)
   at LiteDB.LiteEngine.<Find>d__9.MoveNext()
   at LiteDB.LiteEngine.<Find>d__13.MoveNext()
   at LiteDB.LiteCollection`1.<Find>d__17.MoveNext()
   at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
   at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)

And here's the code producing it: var chunk = liteCollection.Find(x => x.DocumentId == documentId, skip: allItems.Count, limit: chunkSize);

os-alan avatar Feb 02 '18 08:02 os-alan

Hi @guys, this code/database already has broken and only with this I can replicate this broken problem. I never got this problem in v4.x. How are using LiteDB over concurrency? Are web/mobile or desktop app? How write operations are made? If possible, create a sample example using LiteDB.Demo project. I made this project just to catch this kind concurrency of problem.

mbdavid avatar Feb 04 '18 03:02 mbdavid

Hi @mbdavid! Frankly speaking I'm not getting that issue since that moment and cannot reproduce it, though I tried. It is a desktop app (WPF) and I use write/update/delete records from different threads. Will keep you posted, if I face it again.

os-alan avatar Feb 07 '18 07:02 os-alan

Hi @os-alan, if you could reproduce this error in v4 please send to me. After corrupt database I can test anymore.

mbdavid avatar Feb 07 '18 11:02 mbdavid

Hey there - My team's product has been using LiteDB for a little while without seeing this problem. But just recently we updated from v3 something to v4.1.1.0, and also we switched over to using LiteDB's concurrency features (keeping a single instance of the database open and accessing it on multiple threads). Just after doing this, we started getting error reports from our users, with both this error message (https://github.com/StrangeLoopGames/EcoIssues/issues/5548) and a variety of similar messages (https://github.com/StrangeLoopGames/EcoIssues/issues/5610, https://github.com/StrangeLoopGames/EcoIssues/issues/5707).

It's possible the people who are seeing this had an existing .db/.journal file and it was upgraded to v4. I haven't been sure what to ask them. Would it be helpful for me to get their .db files and share them here?

MarjieAVolk avatar Mar 09 '18 23:03 MarjieAVolk

Hi @MarjieAVolk, will be very helpfull if you can send me a copy of this corruped database to check. Did you know if this datafile was updated from v3? In v3 you don't have this problem?

mbdavid avatar Mar 10 '18 00:03 mbdavid

Yes, in v3 I did not have the problem. But I also was not using concurrency in the same way. I will ask these people for their db files. Thanks.

MarjieAVolk avatar Mar 10 '18 00:03 MarjieAVolk

Okay, I got one of the .db files that is seeing the error: Game.zip

This is the exact stack trace they reported:

Unable to cast object of type 'LiteDB.EmptyPage' to type 'LiteDB.HeaderPage'.

Stack:
at LiteDB.PageService.GetPage[T](UInt32 pageID)
at LiteDB.TransactionService.PersistDirtyPages()
at LiteDB.LiteEngine.Transaction[T](String collection, Boolean addIfNotExists, Func2 action) 
at LiteDB.LiteEngine.Insert(String collection, BsonDocument doc, BsonType autoId) 
at LiteDB.LiteCollection1.Insert(T document)
at Eco.Core.Utils.AtomicAction.SimpleAtomicAction.TryApply()
at Eco.Core.Utils.AtomicAction.MultiAtomicAction.TryApply()
at Eco.Core.Utils.AtomicAction.MultiAtomicAction.TryApply()
at Eco.Gameplay.Components.CraftingComponent.CreateWorkOrder(Player player, Recipe recipe, Int32 quantity)

MarjieAVolk avatar Mar 13 '18 23:03 MarjieAVolk

Any updates on this? Did that file have the same issue?

MarjieAVolk avatar Mar 19 '18 19:03 MarjieAVolk