docir — design documents Graph

Documents / Issues / issue-d75df898a99a

One add after the index is deleted under a running daemon writes two documents

The daemon kept serving a deleted index, the failed add left its file behind, and the client resent the crashed request, so a fresh daemon indexed the orphan and wrote the document again.

issue-d75df898a99aissueresolved#daemon#integrity#persistence
View as Markdown◉ View in graph

What is wrong

With the daemon running, deleting the index (rm .docir/index.db*, or git clean -fdX, which the README's "derived and gitignored" invites) and then running one docir add left two files with identical content under two ids, half a second apart. The command exited 0 naming the second id, and docir check reported nothing. Three faults lined up:

  • the daemon's pooled SQLite connections still held the deleted file, so it answered reads from an index nobody could see and refused every write: attempt to write a readonly database;
  • add writes its file before the commit, and the failed commit left the file behind;
  • the crash closed the socket without a reply, which the client read as "the request never landed", the one failure it resends. A fresh daemon rebuilt the index from the orphaned file and wrote the document again under a new id.

The docs watcher died on the same error: it guarded against DocirError only, and sqlite3.OperationalError is not one.

How it showed

Acceptance testing of 0.30.4, reproduced three times out of three on a docir init store: add, delete the index, add again, and ls .docir/docs/decisions lists two …-request-log-rotation.md. It needs a store minting random ids, the init default. A numbered store bumps id_sequences before the file is written, so its add failed before writing anything and the resend wrote one file. Deleting daemon.pid along with the index hid it too, because no daemon was reused. The daemon log held the watcher's traceback but never the add's: the client's respawn sent SIGTERM while the old daemon was still unwinding.

Resolution

Each fault is closed on its own:

  • The daemon checks its index before every request. IndexIdentityGuard records the device and inode of the file the container opened. If the file at the path has a different identity or is gone, the request is refused before it runs (DaemonIndexReplacedError). That refusal is the one daemon answer SocketExecutor meets by replacing the daemon and resending, and the new daemon rebuilds the index from the files.
  • A request the daemon took is never resent. The server sends a keepalive the moment it has read a request, before running it. A connection closed after that frame raises DaemonCrashError, which names the daemon log and is not retried. A close before any frame is still the resendable DaemonError.
  • A failed create takes its file back, so a refused add leaves nothing a later rebuild can index.
  • The watcher survives any exception, logging its type.

Verification

The acceptance repro, as a slow test through a real spawned daemon on an initialised store, failed before the fix with two files, and now passes with one file and an index holding both documents. Seven bugs were re-injected, one per guard, and each was caught by its own test. The CLI repro now leaves one file and exits 0.

To amend: Re-verify: