#3047·LiteDB

LiteEngine.Dispose silently discards close failures, including a failed close checkpoint

Author: JKamskerCreated Sep 28, 2026Updated Sep 28, 2026
Labelsbugarea: wal/recovery

Summary

LiteEngine.Dispose() (and so LiteDatabase.Dispose()) returns normally when the close fails. The close checkpoint, the disk service and the lock service can all throw during close, and every one of those exceptions is discarded:

  • LiteEngine.Close(bool, bool) runs each step through TryCatch.Catch and returns tc.Exceptions.
  • LiteEngine.Dispose(bool) calls this.Close() and ignores the returned list. No other caller reads it either: SharedEngine (engine.Close(final: true), engine.Close()) and CoordinatorHost drop it too.
  • DiskService.Dispose() deliberately collects its failures (data pool, log pool, log delete, cache) and throws an AggregateException. The engine's close catches that exception as well, so the lower layer's attempt to surface the error never reaches the application.

Found while wiring SimulateDataWriteFail for #3034 (#3045).

What is and is not at risk

Committed data is not lost by this. A failed close checkpoint leaves the WAL in place, and the next open replays it. #3045 adds CheckpointDataWriteFailure_Tests.FailedCloseCheckpoint_LeavesTheWalToRecoverEveryCommit, which shows both halves: the close checkpoint's data write throws IOException, the using block around the engine exits normally, the -log file is still there, and a fresh open recovers every committed row.

The harm is that the application cannot tell. After a Dispose that returned normally:

  • The application may reasonably assume the database is one checkpointed file and copy, move, upload or back up the .db alone. If the close checkpoint failed, the commits still in -log.db are missing from that copy. Nothing warns about this until someone opens the copy.
  • A device error at close (disk full, I/O error, a failed flush while disposing the streams) is hidden until the next open, which may happen on another machine or much later.
  • In shared mode the discarded list also hides failures of the connection's final close (SharedEngine.Readers.cs, engine.Close(final: true)).

Reproduce

Condensed from the test above. It needs #3045's branch, which wires SimulateDataWriteFail into the checkpoint's data-page writes:

csharp
using (var engine = new LiteEngine(settings))
using (var db = new LiteDatabase(engine, disposeOnClose: false))
{
    db.GetCollection("c").Insert(new BsonDocument { ["x"] = 1 });
    db.CheckpointSize = 10000; // the close checkpoints the WAL
    engine.SimulateDataWriteFail = page => throw new IOException("injected");
} // no exception here
// -log.db is still present; the .db file alone does not contain the row

On current dev the same code path runs for any real I/O failure during the close checkpoint or while disposing the disk service.

Options (maintainer decision)

  1. Throw from Dispose: an AggregateException (or the single exception) after every close step has run. FileStream.Dispose also throws when its final flush fails. The cost is that Dispose can throw from inside using, and an exception thrown there can hide one already propagating from the block.
  2. Keep Dispose quiet and report elsewhere: log through the engine's diagnostics or an event, and document that an application that needs a single checkpointed file must call Checkpoint() (which throws) before disposing.
  3. Both: throw only for failures that leave the files different from what a successful close promises (checkpoint or flush failures), and log the rest.

Whichever is chosen, a regression test should close an engine whose close checkpoint fails and assert the application observes it. FailedCloseCheckpoint_LeavesTheWalToRecoverEveryCommit currently depends on the silence and would have to change with the fix.