#3050·LiteDB

Sort spill temp-file I/O failure should fail only the query, not the engine

Author: JKamskerCreated Sep 29, 2026Updated Sep 29, 2026
Labelsbugseverity: highpriority: P2

Summary

A failed I/O operation on the private sort spill temp file currently poisons the whole engine.

This is pre-existing on dev and is already called out as an out-of-scope residual in #3027, but it does not have its own tracker.

A large unindexed ORDER BY can spill through LiteDB/Engine/Sort/SortDisk.cs. If that temp-file write/read fails (for example disk full), the resulting IOException reaches EngineState.Handle, which treats every IOException as an engine-fatal storage failure. The query fails as expected, but every later operation on the same LiteDatabase also fails.

That is too broad: the sort spill file is scratch state, not database/WAL state. Its failure should abort that query/transaction and clean up the sort, while the database remains usable.

Related but not a duplicate of #2821: that issue is about database/WAL I/O failure semantics and post-commit checkpoint behavior. This issue is specifically about private sort scratch I/O.

Reproduction / failing test

Add an IoSafety regression test that:

  1. Creates a collection large enough to force a disk spill, e.g. ~6000 documents with a ~200-character string field.
  2. Runs an unindexed query such as: rows.Query().OrderBy("k").ToList().
  3. Injects an IOException("No space left on device") from the sort spill path.
    • Today EngineState.ObserveSortSpill can identify the spill point under DEBUG || TESTING.
    • If it is used to throw, reclassify/add a proper fault injector rather than silently treating an observer as an injector.
  4. Assert the sort query throws the injected IOException.
  5. On the same LiteDatabase, assert:
    • rows.FindById(1) still succeeds;
    • a subsequent insert succeeds.

Also cover a later large sort after the injected failure and verify its result is complete and correctly ordered.

Current cause

On current dev, EngineState.Handle stops the engine for any IOException.

SortDisk uses the engine's scratch stream for external merge-sort containers, but its exceptions carry no indication that they came from disposable temp storage. The engine therefore cannot distinguish:

  • database/WAL I/O, which can affect integrity/durability; from
  • sort scratch I/O, which should affect only the current operation.

#3027 changes the database-file failure policy to sticky read-only behavior, but the distinction is still required: a sort-temp failure should not create a database write failure or degrade the engine.

Desired behavior

  • Sort temp write/read/extend/seek I/O failures fail the current query.
  • The query transaction unwinds/rolls back normally.
  • The engine remains usable for reads and writes.
  • No database/WAL write-failure state is recorded for a private temp-file failure.
  • Cleanup must not leak readers, temp-file handles, or container positions.

Implementation options include marking/wrapping exceptions at the SortDisk boundary (for example an internal exception-data key or a dedicated internal exception type) and making EngineState.Handle treat that classification as non-fatal.

Do not whitelist arbitrary IOException from the query stack: only failures proven to originate from disposable sort scratch storage should get this treatment.

Container reuse safety

A failed spill can occur after a container position has already been allocated. Verify that cleanup returns the position exactly once and that reuse cannot expose partially written/stale bytes.

Required regression:

  1. force a spill write to fail after a position was allocated;
  2. let the failed query unwind;
  3. run another large spilling sort;
  4. verify exact count, contents and ordering.

Safety / CI

  • Keep the normal database/WAL I/O-failure tests unchanged: real storage I/O must still follow the durability policy.
  • Register the fault point/evidence in .github/safety/fault-points.json if the hook becomes an injector.
  • Run the normal partitioned LiteDB.Tests suite and the safety scripts, including check_coverage_regression.py and check_fault_points.py.
  • Add a short entry to docs/release-notes.md.

Done when

  • the injected spill failure fails only that query;
  • the same database instance can read and write afterwards;
  • a subsequent spill succeeds with exact results;
  • sort resources/positions are safely reclaimed;
  • existing database/WAL I/O safety behavior is unchanged.

Related: #3027, #2821, #2960, #3034.