InvalidCastException during FindAll().ToList()
=========================
Exception Information
=========================
Message: Unable to cast object of type 'LiteDB.EmptyPage' to type 'LiteDB.HeaderPage'.
Source: LiteDB
TargetSite: Void Recovery()
HelpLink:
Type: System.InvalidCastException
Place: 1
Stack:
at LiteDB.TransactionService.Recovery()
at LiteDB.LiteEngine..ctor(IDiskService disk, String password, Nullable`1 timeout, Int32 cacheSize, Logger log)
at LiteDB.LiteDatabase.<>c__DisplayClass11_0.<.ctor>b__0()
at LiteDB.LazyLoad`1.get_Value()
at LiteDB.LiteCollection`1.<Find>d__17.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
This exception are recurrent. Was fixed long time ago and now seams back. It's about concurrency. I will search about this again
What's about this issue? Can we help somehow with the fix? We receive more and more complains from our users.
Hi @podkolzzzin, no time for that yet. I'm in a big project now and with no time for litedb yet, sorry
Here is code that reproduces error really often.
(Also another exception can be raised(NullReferenceException), but also because of unsuccessful cast to Header page, but in that case as operator is used in your code)
using LiteDB;
using System;
using System.IO;
using System.Linq;
using System.Threading;
namespace LiteDBAssault
{
class Session
{
public string Name { get; set; }
public int Number { get; set; }
public int Id { get; set; }
}
class Program
{
static void Main(string[] args) {
File.Delete("db");
Thread[] threads = Enumerable.Repeat(0, 100).Select(f => new Thread(ThreadProc)).ToArray();
foreach (var t in threads)
t.Start();
}
private static void ThreadProc() {
Random r = new Random();
int val = r.Next();
if (val % 2 == 0)
Read();
else
Write();
}
private static void Write() {
Thread.CurrentThread.Name = "Write Thread";
Random rand = new Random();
Console.WriteLine("Thread writes");
try {
while (true) {
using (LiteDatabase db = new LiteDatabase("db")) {
var sessions = db.GetCollection<Session>("Sessions");
foreach (var s in sessions.FindAll().ToList().Where(f => f.Number % 4 == 0))
sessions.Delete(s.Id);
}
using (LiteDatabase db = new LiteDatabase("db")) {
db.GetCollection<Session>("Sessions").Insert(new Session() {
Name = Guid.NewGuid().ToString(),
Number = rand.Next(0, int.MaxValue)
});
}
}
}
catch (Exception ex) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.ToString());
Environment.Exit(-1);
}
}
private static void Read() {
Thread.CurrentThread.Name = "Read Thread";
Console.WriteLine("Thread reads");
try {
while (true) {
using (LiteDatabase db = new LiteDatabase("db")) {
var data = db.GetCollection<Session>("Sessions").FindAll().ToList();
Console.WriteLine(data.Count);
}
}
}
catch (Exception ex) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.ToString());
Environment.Exit(-1);
}
}
}
}
Same issue here, has anyone found a solution?
@NourShalabi, are you in v3.x?
I was able to get around this by using Find instead of FindAll. By using Find with an obvious predicate, it was able to return an empty collection without throwing the exception. For example:
var results = new List<Article>();
using (var db = new LiteDatabase(Path))
{
var articles = db.GetCollection<Article>("articles");
var foundItems = articles.Find(a => a.Id > 0);
results.AddRange(foundItems);
return results;
}
In case this helps anyone.
Hi! With the objective of organizing our issues, we are closing old unsolved issues. Please check the latest version of LiteDB and open a new issue if your problem/question/suggestion still applies. Thanks!