LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

Passing the wrong field to a query during a delete operation causes unhandled exception and process termination

Open ccicchitelli opened this issue 8 years ago • 7 comments

Hello! One of the four daily DB cleanup tasks we were running were causing the app to crash. Turns out we were passing in the wrong field, one that didn't exist on that collection. Here is the result:

Description: The process was terminated due to an unhandled exception.
Exception Info: System.InvalidCastException
   at LiteDB.PageService.GetPage[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](UInt32)
   at LiteDB.LiteEngine+<>c__DisplayClass8_0.<Delete>b__0(LiteDB.CollectionPage)
   at LiteDB.LiteEngine.Transaction[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.String, Boolean, System.Func2<LiteDB.CollectionPage,Int32>)

This is the (sanitized) code:

    public static void CleanLog(int days)
    {
        var col = xApp.Database.GetCollection<DbLogItem>("EventLog");
        col.Delete(LiteDB.Query.LT("EventTime", DateTime.Now.AddDays(-days)));
    }

Obvious solution on our end is to add a try/catch, but wanted to make you aware.

Thank you!

ccicchitelli avatar May 05 '18 22:05 ccicchitelli

Hi @ccicchitelli, are you using LiteDB v4? This error occurs in some cases in v3 on concurrent environment... but I didn't get in v4 yet.

mbdavid avatar May 05 '18 23:05 mbdavid

4.1.2, I'm pulling it from NuGet.

ccicchitelli avatar May 05 '18 23:05 ccicchitelli

It seems the same method call with the wrong field name will also generate this error, or least has in the past:

Exception Info: LiteDB.LiteException at LiteDB.LockService.Write() at LiteDB.LiteEngine.Transaction[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.String, Boolean, System.Func2<LiteDB.CollectionPage,Int32>)`

ccicchitelli avatar May 05 '18 23:05 ccicchitelli

Hi @ccicchitelli, creating a simple example I didn't get this error (with or without index):

var initial = new DateTime(2000, 01, 01);

using (var db = new LiteEngine(file.Filename))
{
    for(var i = 0; i < 5000; i++)
    {
        db.Insert("col", new BsonDocument { { "dt", initial.AddDays(i) } });
    }

    db.EnsureIndex("col", "dt");

    Assert.AreEqual(5000, db.Count("col"));

    Assert.AreEqual(4999, db.Count("col", Query.GT("dt", initial)));

    var del = db.Delete("col", Query.GT("dt", initial));

    Assert.AreEqual(4999, del);

    Assert.AreEqual(0, db.Count("col", Query.GT("dt", initial)));
}

This error always occurs? There is another variable that can be analyzed to detect when this happening? Are running, in another thread, any different command? It's possible write snippet code here to reproduce?

mbdavid avatar May 06 '18 12:05 mbdavid

I can confirm that passing in the correct field name (as opposed to one that doesn't exist) results in the issue going away. I'd be happy to pass along the exact code via email or slack etc if you'd like.

In your example, you are passing in "dt" as the field name, which exists. What happens if you pass in say "dd", which does not?

ccicchitelli avatar May 07 '18 01:05 ccicchitelli

If you use an non existing field, the result of condition are always false: there no error, but no document will be deleted.

Assert.AreEqual(0, db.Count("col", Query.GT("dd", initial)));

In LiteDB there is no fixed document schema, so any field can be used (returning null if not exists). If you want pass me your code, send to my email so I can take a look better to understand.

mbdavid avatar May 07 '18 11:05 mbdavid

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!

lbnascimento avatar Feb 28 '20 13:02 lbnascimento