#3052·LiteDB

Close sync-helper failure publication races in the extracted durability protocol

Author: JKamskerCreated Sep 29, 2026Updated Sep 29, 2026

Follow-up to #3051

This tracks one concrete concurrency/safety gap found while reviewing #3027 at 3d136a46f.

It belongs primarily to #3051 S09 — coherent WAL durability and recovery protocol. It should be fixed and proven on the extracted S09 candidate rather than expanding the original monolithic #3027 indefinitely.

Problem

The new failure policy says that after a real storage write/sync failure is recorded, no later write, sync, checkpoint, or promotion may continue using the failed engine/handles as if they were still safe.

Several writer paths already enforce this correctly by publishing/rechecking failure state while holding the WAL writer lock.

Examples:

  • WAL batch failure handling stops the engine before releasing the WAL writer.
  • File-format promotion rechecks ThrowIfStopped() and RequireNoWriteFailure() after acquiring the writer lock.

However, some sync helpers do not participate in the same boundary.

Current examples from #3027:

  • DiskService.OverwriteBarrier.cs → LogSyncs()
  • DiskService.DurableFlush.cs → DataFileSyncs()
  • DiskService.WriteFailures.cs → WalKeptReport
  • WAL-limit/checking paths that call these helpers and only record/stop after the helper unwinds

The problematic shape is:

check failure state
    ↓
wait for I/O / WAL-writer lock
    ↓
perform sync
    ↓
publish failure later in outer caller

instead of making the failure boundary atomic with respect to the synchronization protecting that I/O.

Concrete interleaving 1 — waiting sync runs after another failure was published

  1. Operation A (for example a $database diagnostic) passes its initial failure-state check.
  2. A then waits for the WAL writer lock in LogSyncs().
  3. Operation B currently holding that lock hits a real WAL failure.
  4. B records the failure and begins stopping the engine before releasing the lock.
  5. B releases the lock.
  6. A acquires it and calls SyncRawLog() anyway because LogSyncs() does not recheck stopped/recorded-failure state under the lock.

This violates the intended invariant that nothing else performs write/sync work through handles once their failure has been published.

Concrete interleaving 2 — helper failure is published too late

The reverse window exists when the diagnostic/helper sync itself fails:

  1. Operation A performs a sync inside LogSyncs() / DataFileSyncs().
  2. That sync fails.
  3. The helper unwinds and releases its synchronization.
  4. Only the outer caller records the failure / schedules the stop.
  5. A waiting writer may acquire the relevant lock in between and pass its normal admission check before the new failure is visible.

The exact set of affected callers should be audited rather than assuming only WalKeptReport is involved.

Required invariant

For a real I/O failure:

Once the operation that observed it releases the synchronization protecting that storage transition, every waiter must already be able to observe a state that prevents further write/sync/checkpoint/promotion work on the failed engine.

Teardown may still happen outside unrelated locks. The important part is publish-before-unlock and recheck-after-lock-acquisition.

Do not turn unsupported-sync/degraded-storage cases into fatal failures accidentally. Preserve the existing distinction between:

  • storage that answers “cannot sync” / an explicitly supported degraded path; and
  • a real write/sync I/O failure.

Proposed implementation direction

Audit every helper that can issue persistent I/O after an earlier admission check.

At minimum:

LogSyncs()

After acquiring the WAL writer lock, recheck the relevant engine/failure state before issuing SyncRawLog().

If the sync itself fails with a real I/O error, publish/record the failure before releasing the WAL writer lock. Complete teardown afterwards to avoid lock-order problems.

DataFileSyncs()

Apply the equivalent rule around the synchronization protecting the data writer, while preserving the existing lock order (log → data where both are required).

Be careful not to create the WAL merely to acquire its lock on paths where it does not yet exist, especially encrypted WALs whose creation itself has persistent side effects.

Outer diagnostic / WAL-limit callers

Simplify them so failure publication is not delayed until after the helper released the synchronization that ordered the I/O.

Do not duplicate competing failure records; the first causal failure should remain authoritative.

Tests

Add deterministic forced-interleaving tests. Sleeps alone are not sufficient.

1. Waiting helper does no I/O after published failure

  • Hold the WAL writer with operation B.
  • Let operation A pass its outer admission check and block waiting for the writer.
  • Inject a real storage failure into B.
  • Ensure B publishes the failure before releasing the writer.
  • Pause teardown if needed.
  • Release the writer.
  • Assert A performs no additional native/raw sync or persistent write.
  • Assert A observes the recorded failure/read-only continuation contract instead.

Positive control: without the injected failure, A performs the expected sync.

2. Helper failure blocks a waiting writer before it can proceed

  • Operation A owns the relevant synchronization and its diagnostic/WAL-limit sync fails.
  • Operation B is already waiting to write.
  • Assert the failure is published before A releases the synchronization.
  • Assert B writes no WAL frame/confirmation and cannot acknowledge a commit.
  • Assert the engine subsequently follows the documented read-only/sticky-failure behavior.

Positive control: with the sync succeeding, B proceeds normally.

3. Data-file equivalent

Repeat the same publication/recheck assertion for the data-sync path where its locking differs from the WAL path.

4. Degraded/unsupported-sync control

Exercise the #2242-style “cannot sync” case to prove the fix did not accidentally convert an intentionally supported degraded/refusal path into the fatal-I/O path.

Acceptance criteria

  • Every persistent-I/O helper reachable after an earlier admission check is audited.
  • Relevant helpers recheck stopped/recorded-failure state after acquiring their protecting synchronization.
  • A real sync/write failure becomes visible to waiters before that synchronization is released.
  • No later write, sync, checkpoint or promotion runs on the failed engine/handles in the forced interleavings.
  • Unsupported-sync/degraded-storage behavior remains distinct and covered.
  • The first causal storage failure remains the reported failure.
  • Deterministic failing-before / passing-after interleaving tests exist.
  • #3051 S09 safety evidence explicitly includes these tests on the actual extracted candidate tree.

Scope

This issue is not:

  • #3048 (unproven WAL prefix after a later reopen),
  • the rebuild namespace/directory durability work,
  • the commit/rebuild race,
  • a redesign of the sticky read-only continuation layer,
  • or a requirement to solve every durability residual before splitting #3027.

It is specifically the synchronization boundary around publishing and observing storage failures while persistent I/O helpers are queued/racing.

Refs: #3051, #3027, #3034.