LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

[BUG] Cannot dispose DB after (expected) read only errors

Open nightroman opened this issue 3 years ago • 2 comments

Tested version: LiteDB 5.0.12

Steps to reproduce

  1. Open a database in read only mode.
  2. Invoke some Insert. It fails as expected.
  3. Dispose the database instance. This throws an unexpected exception.
    • and the database file remains in use, e.g. cannot be deleted

Use the attached XUnit test project, ready to test. See code comments for the details.

_220616_u5.zip

nightroman avatar Jun 17 '22 03:06 nightroman

From the attached test

// create the database, add a document
{
    using (var db = new LiteDatabase(dbPath))
    {
        var c1 = db.GetCollection<Data>("c1");
        c1.Insert(new Data { Id = 1, Name = "n1" });
    }
}

// open the database read only and try to insert
{
    var db = new LiteDatabase($"Filename={dbPath}; ReadOnly=true");
    var c1 = db.GetCollection<Data>("c1");

    // this exception is expected due to ReadOnly=true
    // but the message is somewhat not clear
    var ex1 = Assert.Throws<Exception>(() =>
    {
        c1.Insert(new Data { Id = 2, Name = "n2" });
    });
    Assert.Equal("LiteDB ENSURE: discarded page must be writable", ex1.Message);

    // this exception is NOT EXPECTED, we are just closing the database
    var ex2 = Assert.Throws<Exception>(() =>
    {
        db.Dispose();
    });
    Assert.Equal("LiteDB ENSURE: discarded page must be writable", ex2.Message);

    // this exception is NOT EXPECTED, we should have closed the database and can delete the file
    var ex3 = Assert.Throws<IOException>(() =>
    {
        File.Delete(dbPath);
    });
    Assert.StartsWith("The process cannot access the file", ex3.Message);
}

nightroman avatar Jun 17 '22 03:06 nightroman

was this bug fixed?

mirror222 avatar Sep 15 '23 01:09 mirror222