Skip to content

Make config file writes durable and reads safe - #161

Merged
RikusVanRensburg merged 1 commit into
mainfrom
fix/RVR/config-durability
Nov 12, 2026
Merged

RikusVanRensburg merged 1 commit into
mainfrom
fix/RVR/config-durability

Conversation

@RikusVanRensburg

Copy link
Copy Markdown
Contributor

Closes #143

What changed and why

All in koppel-config (plus docs/config.md).

  • Durability: the directory is flushed after the replace (Unix; Windows has no call and NTFS journals metadata), so an acknowledged change cannot roll back in a power cut. Temp files are created 0600. A directory-flush error that only means "this file system cannot" (InvalidInput, Unsupported) is logged at debug and ignored.
  • Key file: still temp file plus hard link first. A file system that refuses the link (network, FUSE, Docker bind mounts) falls back to create_new in place, flushed, with the directory flushed after. A start that finds a short key file waits up to 3 seconds for it. A crash mid-write in the fallback leaves a short file that the next start refuses with the existing message; nothing was sealed with it.
  • Own-write detection: the stamp (size, mtime) now comes from the temp file's handle before the rename, so nothing can slip into a gap. A SHA-256 of the bytes written or read is kept; a changed stamp is followed by a hash compare, and bytes with the known hash are not decoded again. A file written in the last 3 seconds is hashed every poll, which catches a same-length edit on a coarse-mtime file system. The 250 ms poll is unchanged. Reads go through one handle and retry when the file changed while being read.
  • No plaintext copy: State.current_json is gone. "Unchanged" is a typed PartialEq compare (ConfigFile<T> now needs T: PartialEq; every file type already has it). This also covers the first bullet of Secrets are not copied, printed or left beside the key #156.
  • Error kinds: ConfigFileError gets kind: ConfigErrorKind (Io, Decrypt, Parse, Invalid, Newer, non-exhaustive); path and reason stay. The advice is composed from the kind: only Decrypt says "restore the keys directory". The secret container's own "newer format" refusal still arrives as Decrypt, because CodecError(pub String) is used by koppel-usb tests and I did not change its shape.
  • Size cap: 8 MiB, checked before and while reading.
  • Linux directory checks: harden_directory refuses a symlink and a directory owned by anyone but the effective user (libc::geteuid; libc was already a workspace dependency for koppel-usb).
  • Delivery: update still returns after delivery, and the nested shortcut stays, but the wait now times out after Options::delivery_timeout (default 15 s). A handler that spawns an update and awaits it used to deadlock; it now costs one timeout and the change was on disk already. I kept the deliver-then-return ordering because the confirm rule needs it.
  • shareOnStartup: one default, true. A hand-edited settings.json without the key used to read as false.
  • Version and unknown names: every file is written with "version": 1 after its fields (done at the file layer, so the config structs did not change). A newer version is refused (Newer). Unknown top-level names in settings.json, devices.json and attached.json are logged.

Tests

  • Fault injection: write_atomic_with takes a hook called after each step; a failure at each of six steps leaves the old file or the whole new one and no temp file. Plus a failing codec through ConfigFile::update (nothing kept, nothing delivered).
  • Two-process race (tests/key_file_race.rs): 6 real processes open a new key file together, 5 rounds; one key, nothing left over.
  • Hard-link fallback, key file waiting for a mid-write peer, same-size same-mtime edit seen by the watcher and by an update, version/newer/unknown names, error kinds, size cap, spawned-handler timeout, symlink and owner refusal on Linux.
  • Run (Linux container rust:1.99.1-trixie): cargo test --workspace --locked all green; cargo clippy --workspace --all-targets --locked -- -D warnings clean. Native Windows: cargo build --workspace --locked, cargo clippy ... -D warnings, cargo fmt --all --check clean.
  • NOT run: tests natively on Windows (project rule), koppel-contract against a fresh build, the UI tests, cargo deny. The directory fsync itself cannot be observed; the test checks it is the step after the replace. The owner test needs root and is skipped otherwise (it ran as root in the container).
  • The seed test's expected bytes changed ("version": 1 at the end). That test was right for the old format and is updated for the new one.

Docs

docs/config.md updated.

Changelog notes:

  • On disk: every config file written by this version ends with "version": 1; 0.1.x and older 0.2 builds ignore it. A file without it is read as version 1. The key file and its format are unchanged. Temp and replaced files are 0600 on Linux.
  • Behaviour: a settings.json without shareOnStartup now means true (was false). Linux: a symlinked or foreign-owned config or keys directory is refused at start.
  • Wire: none. Public API of koppel-config: ConfigFileError.kind, ConfigErrorKind, Options.warn_unknown_keys, Options.delivery_timeout added.

🤖 Generated with Claude Code

- Flush the directory after replacing a file, and make temp files 0600.
- Make the key file without relying on hard links, and wait for one that is mid-write.
- Tell our own writes from edits by a SHA-256 of the bytes, with the size and time taken
  from the temp file before it moves. No plaintext copy is kept for comparison.
- ConfigFileError gets kinds (Io, Decrypt, Parse, Invalid, Newer); advice follows the kind.
- Cap config files at 8 MiB.
- Refuse a symlinked config directory and one owned by another user on Linux.
- update gives up waiting for subscribers after 15 seconds instead of for ever.
- shareOnStartup has one default, true.
- Files carry "version": 1; a newer one is refused. Unknown names in hand-edited files are logged.
- Fault-injection tests for each write step, a two-process key file race, and tests for the rest.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@RikusVanRensburg
RikusVanRensburg merged commit 3bbe1b2 into main Nov 12, 2026
4 checks passed
@RikusVanRensburg
RikusVanRensburg deleted the fix/RVR/config-durability branch November 12, 2026 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Config files survive a power cut, a bind mount and a typo

1 participant