LiteEngine.Dispose silently discards close failures, including a failed close checkpoint
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 throughTryCatch.Catchand returnstc.Exceptions.LiteEngine.Dispose(bool)callsthis.Close()and ignores the returned list. No other caller reads it either:SharedEngine(engine.Close(final: true),engine.Close()) andCoordinatorHostdrop it too.DiskService.Dispose()deliberately collects its failures (data pool, log pool, log delete, cache) and throws anAggregateException. 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
.dbalone. If the close checkpoint failed, the commits still in-log.dbare 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:
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 rowOn 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)
- Throw from
Dispose: anAggregateException(or the single exception) after every close step has run.FileStream.Disposealso throws when its final flush fails. The cost is thatDisposecan throw from insideusing, and an exception thrown there can hide one already propagating from the block. - Keep
Disposequiet and report elsewhere: log through the engine's diagnostics or an event, and document that an application that needs a single checkpointed file must callCheckpoint()(which throws) before disposing. - 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.
Source: litedb-org/LiteDB