diff --git a/.gitignore b/.gitignore index cc6ff119f1..e38b5879c5 100644 --- a/.gitignore +++ b/.gitignore @@ -4,12 +4,14 @@ .idea .worktrees/ +.superpowers/ .codex .opencode/ .claude/ .mcp.json skills/ memory.graph +memory.graph.fts.sqlite3* # heaptrack profiling artifacts heaptrack.*.zst diff --git a/CHANGELOG.md b/CHANGELOG.md index e0149c8d49..b01e9e958d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,91 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Breaking-ish changes + +- **Adding `OpenOptions::allow_unlocked` breaks struct-literal construction.** + `OpenOptions` has all-public fields and is not `#[non_exhaustive]`, so under + Rust's semver rules any downstream code building it as + `OpenOptions { wal_checkpoint_threshold: .., page_cache_size: .., .. }` + without `..Default::default()` no longer compiles. Callers who spread + `..Default::default()`, or who use the chainable builder methods, are + unaffected. This branch had to update two such literals in its own tree — + the `Default` impl itself and one test — and only the latter is a + "downstream literal" in the sense that matters to a consumer. + + Recorded here because the release decision is deliberately deferred, and + whoever makes it needs the facts: this is the **first post-1.0 field + addition** to `OpenOptions`. The earlier `max_derived_facts` and + `max_results` fields landed in v0.19.0, before 1.0, so they set no + precedent under the stability guarantee in PHILOSOPHY.md §7. Marking + `OpenOptions` `#[non_exhaustive]` would immunise every future field + addition, but is itself a breaking change, so it is a decision to make at + the same time as the version number rather than after it. +- **Minimum supported Rust version is now 1.89** (was effectively 1.85, implied + by `edition = "2024"`). Required for `std::fs::File::try_lock`, stabilised in + 1.89. Declared as `rust-version` in `Cargo.toml`, so cargo reports a clear + error rather than a confusing one. +- **On Windows, an open database can no longer be read through any other file + handle** — including another handle in the same process. `LockFileEx` locks + are mandatory rather than advisory and exclude every handle but the one + holding them, so copying or backing up an open `.graph` file now fails on + Windows where it previously succeeded, and so does opening it yourself for a + raw read while the database is live. The error is os error 33, "another + process has locked a portion of the file", even when the other process is + you. Unix locks remain advisory and are unaffected. Close the database + before reading the file directly — which also avoids a torn read. +- **`open()` now fails on filesystems that cannot lock at all** rather than + proceeding unprotected. Set `OpenOptions::allow_unlocked(true)` to accept the + risk on single-writer deployments. +- **A database genuinely locked by another process now reports its error + later than before** — about 375ms later. `FileBackend::open_with` retries a + cross-process `WouldBlock` up to 10 times with backoff doubling from 5ms and + capped at 50ms before concluding another process holds the lock. This exists + because `fork` transiently duplicates the lock-holding descriptor into a + child until `execve` closes its close-on-exec descriptors, so any process + that spawns subprocesses — the normal pattern for the Python and Node + bindings — could see a spurious `WouldBlock` from its own child. SQLite + solves the same problem with `busy_timeout`; this is the same idea, bounded + so a real conflict still fails, just not instantly. A same-process conflict + (this process already holds the file open) is unaffected: it fails + immediately, since waiting could never help. +- **A refused open on a filesystem that cannot lock may leave a 0-byte + `.graph` file behind.** The file must exist before it can be locked, so the + create-then-lock ordering now creates before it can know the lock will be + refused. Harmless — the next open sees `is_new` and initialises normally — + but worth knowing if you inspect the filesystem after a failed open. + ### Bug fixes +- **A `.graph` file is no longer bricked when its holder dies in a container** + (#317): `FileLock` recorded the holder's PID in a `.graph.lock` sidecar and + refused to open when that PID equalled our own. Every container's main process + is PID 1 in its own namespace, so a container killed with SIGKILL left the + sidecar holding `1`, and the replacement container — also PID 1 — read its own + PID back and refused. The refusal was permanent; no code path could clear it, + and an operator had to delete the file by hand. The `pid == our_pid` refusal + was itself the fix for #304, so the two defects were entangled. + + A PID cannot serve as a liveness token: it is not unique across PID namespaces + and is recycled within one. The sidecar is gone, and the lock now lives in the + kernel via `std::fs::File::try_lock` on the `.graph` file itself. The kernel + releases it whenever the process exits, however it exits, so a crashed holder + leaves nothing behind. Because `flock` and OFD locks attach to the open file + description rather than the process, a second open within one process is also + denied, so #304 stays fixed with no PID bookkeeping at all. + + A leftover `.graph.lock` from a version before 1.3 is ignored and never + deleted — it no longer has any effect, and a still-running old process may + still depend on it. Running mixed versions against one file is not + supported; upgrade all writers together. + + Reported and diagnosed by [@ocasazza](https://github.com/ocasazza) in #317, + who supplied the reproduction, identified the PID-namespace mechanism, and + correctly traced the refusal back to the #304 fix that caused it. That + analysis is what made this straightforward to verify. One of the tests from + their proposed patch is the ancestor of the regression test that now covers + this. + - **Keywords may contain `?`**: `:person/alive?` now lexes as a single keyword. Previously `?` terminated the keyword, so `[:e :alive? true]` lexed as `:alive` followed by a stray `?` symbol and was rejected as a four-element fact — reporting `Optional 4th element of a fact must be a map`, which named the value rather than the keyword. `?` is a constituent character in EDN keywords and predicate-style names (`:artist/dead?`) are idiomatic. Query variables are unaffected: a `?` not preceded by `:` still begins a symbol. `tests/grammar/grammar.pest` updated to match. ## v1.2.3 — 2026-08-10 diff --git a/CLAUDE.md b/CLAUDE.md index 84fd0b016c..72d356769b 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -167,14 +167,14 @@ Auto-migrates v1/v2/v3/v4/v5/v6 → v7 on open/checkpoint. ## Test Coverage -**998 tests passing** (990 passing, 8 ignored; unit + integration + doc). +**1023 tests passing** (1015 passing, 8 ignored; unit + integration + doc). See `docs/TEST_COVERAGE.md` for the full per-file breakdown. **Testing conventions** — see the Testing Conventions section below before writing any tests. ## Key Files for the Next Phase -Phase 8 is complete — v1.0.0 released. Wave 3 Reliability is complete. #231 Repo Split is complete — Python, Node, WASM, Java, Android, Swift, and C bindings are all in separate repos under the project-minigraf org. Wave 8 Documentation is complete (#190, #191, #192). v1.2.0 released — magic sets rewriting (#289) and per-query limits (#288). Wave 5 (Query Profiler) is next. +Phase 8 is complete — v1.0.0 released. Wave 3 Reliability is complete. #231 Repo Split is complete — Python, Node, WASM, Java, Android, Swift, and C bindings are all in separate repos under the project-minigraf org. Wave 8 Documentation is complete (#190, #191, #192). v1.2.0 released — magic sets rewriting (#289) and per-query limits (#288). Kernel file locking (#317, #304) is merged but unreleased: `std::fs::File::try_lock` on the `.graph` file replaced the `.graph.lock` PID sidecar, which bricked databases after any container restart. MSRV is now 1.89. The release version is deliberately undecided — see the CHANGELOG's breaking-changes section. Wave 5 (Query Profiler) is next. Wave 5 relevant areas (see `ROADMAP.md` for full spec): - `src/query/datalog/` — query executor where profiling hooks will go (#185) diff --git a/Cargo.toml b/Cargo.toml index 35026b2e99..5fc4184d99 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ resolver = "2" name = "minigraf" version = "1.2.3" edition = "2024" +rust-version = "1.89" description = "Zero-config, single-file, embedded graph database with bi-temporal Datalog queries" license = "MIT OR Apache-2.0" authors = ["Aditya Mukhopadhyay"] diff --git a/README.md b/README.md index 006c930af2..c567379225 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ let r2 = pq.execute(&[("tx", BindValue::TxCount(2)), ("entity", BindValue::Entit ```bash cargo run # interactive Datalog REPL -cargo test # run 998 tests +cargo test # run 1023 tests cargo run < demos/demo_recursive.txt # recursive rules demo ``` diff --git a/ROADMAP.md b/ROADMAP.md index c5c75ce282..517aecd27d 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -1707,6 +1707,7 @@ When evaluating features, ask: - ✅ v1.2.1 — Magic sets `fb` adornment fix: sentinel entity + 2-arg guard preserves keyword type through value-position binding (#297, #298) - ✅ v1.2.3 — Core unchanged; cut to carry `minigraf-node`'s new `close()` (project-minigraf/minigraf-node#1), needed because v1.2.2's handle exclusion left GC'd hosts with no way to release a handle - ✅ v1.2.2 — `FileLock::acquire` no longer treats a lock held by our own PID as stale, which had allowed two handles on one file per process with divergent page tables — the source of the intermittent `Page N out of bounds` (#304); ordering predicates compare strings lexicographically (#312) +- 🚧 Unreleased — Kernel file locking replaces the PID sidecar (#317, #304). A PID is not a liveness token: it is not unique across PID namespaces and is recycled within one, so a container killed with SIGKILL left `.graph.lock` holding `1` and the replacement container — also PID 1 — read its own PID back and refused to open, permanently. `std::fs::File::try_lock` moves liveness to the kernel, which releases on process death however it occurs; because `flock`/OFD locks attach to the open file description, a second open within one process is refused too, so v1.2.2's fix for #304 is preserved without any PID bookkeeping. The sidecar is gone. MSRV moves 1.85 → 1.89. Nightly NFS and Docker lock suites deferred to #324 **Transferred to other repos**: - Developer tools: `minigraf-inspector` (#184), `minigraf-visualizer` (#186); #185 (query profiler) deferred to 2.0 @@ -1729,4 +1730,4 @@ See [GitHub Issues](https://github.com/project-minigraf/minigraf/issues) for spe --- -**Last Updated**: August 2026 — v1.2.3 released (core unchanged; carries the minigraf-node `close()` fix). Next up is v1.3.0 (performance + structured errors: #274, #275, #248, #250, #277) +**Last Updated**: August 2026 — v1.2.3 released (core unchanged; carries the minigraf-node `close()` fix). Kernel file locking (#317, #304) is merged but unreleased; its release version is deliberately undecided, see the CHANGELOG's breaking-changes section. Next up is v1.3.0 (performance + structured errors: #274, #275, #248, #250, #277) diff --git a/docs/ERROR_REFERENCE.md b/docs/ERROR_REFERENCE.md index 6237436165..f17466aea9 100644 --- a/docs/ERROR_REFERENCE.md +++ b/docs/ERROR_REFERENCE.md @@ -124,6 +124,9 @@ appear in runtime output today — runtime codes are tracked in | STG-022 | Page id overflow writing fact pages | Storage | | STG-023 | Page index exceeds u64::MAX | Storage | | STG-024 | Pending fact count exceeds u64::MAX | Storage | +| STG-025 | Database already open in this process | Storage | +| STG-026 | Database locked by another process | Storage | +| STG-027 | Filesystem does not support file locking | Storage | | WAL-001 | Invalid WAL magic number | WAL | | WAL-002 | Unsupported WAL version | WAL | | WAL-003 | Fact serialised size exceeds maximum | WAL | @@ -1791,6 +1794,50 @@ See the [file format section in README](../README.md#file-format) for version hi --- +### STG-025 Database already open in this process + +**Error text**: `Database is already open in this process ({path}). A second handle on one file would give each its own page table and corrupt both — reuse the existing handle instead. \`Minigraf\` is cheap to clone and all clones share the same database.` + +**Cause**: This process already holds an open handle on this file. Two `FileBackend` instances on one file each cache their own `header.page_count`, allocate new pages from that count, and bounds-check reads against it, so the two page tables diverge and produce intermittent `Page N out of bounds` errors. + +**Resolution**: +- Reuse the handle you already have. `Minigraf` is cheap to clone and all clones share one database. +- If you cannot find the other handle, it is usually held by a longer-lived object than you expect — a cache, a registry, or a background task. + +**Scenario**: A request handler calls `Minigraf::open` per request while a connection pool already holds the same path open. + +--- + +### STG-026 Database locked by another process + +**Error text**: `Database is locked by another process ({path}). The lock is held on the file itself and is released automatically when the holding process exits, so there is no lock file to clean up.` + +**Cause**: Another process holds the kernel file lock on this `.graph` file. Minigraf is single-writer: one process at a time. This is reported only after a bounded retry (up to ~375ms) rules out a transient `fork`-related false positive — see the Unreleased CHANGELOG entry on the bounded retry. + +**Resolution**: +- Wait for the other process to exit; the kernel releases the lock automatically, including when the process is killed. +- There is no lock file to delete. If you find a stale `.graph.lock`, it is a leftover from a version before 1.3 and has no effect — it is not read or deleted, and should not be removed by hand in case an old process still depends on it. +- If two services genuinely need the same file, put one in front of the other. Minigraf is embedded, not a server. + +**Scenario**: A rolling deployment starts the new pod before the old one has exited, and both mount the same volume. + +--- + +### STG-027 Filesystem does not support file locking + +**Error text**: `Failed to lock database at {path}: {e}. This filesystem does not support file locking (common on NFSv3 without lockd, and on some FUSE mounts). Set \`allow_unlocked\` in \`OpenOptions\` to open anyway — that accepts the risk that concurrent writers corrupt the file.` + +**Cause**: The underlying filesystem rejected the lock outright. Common on NFSv3 mounts with no `lockd` running, and on some FUSE filesystems. Because the file must exist before it can be locked, this refusal can leave a 0-byte `.graph` file behind; the next open sees `is_new` and initialises normally. + +**Resolution**: +- Prefer moving the database to a filesystem that supports locking — this is the safe fix. +- On NFS, ensure NFSv4, or start `lockd` for NFSv3. +- If you are certain there is exactly one writer, set `OpenOptions::allow_unlocked(true)`. This accepts the risk that concurrent writers corrupt the file; Minigraf cannot detect them without a working lock. + +**Scenario**: A `.graph` file placed on an NFSv3 export whose `lockd` is not running. + +--- + ## WAL — Write-Ahead Log Errors WAL errors relate to the sidecar `.wal` file written alongside the `.graph` file. diff --git a/docs/TEST_COVERAGE.md b/docs/TEST_COVERAGE.md index 42b4681ec9..2cb83332b4 100644 --- a/docs/TEST_COVERAGE.md +++ b/docs/TEST_COVERAGE.md @@ -1,16 +1,17 @@ # Minigraf Test Coverage Report -**Last Updated**: v1.2.3 (August 2026), 998 tests ✅ +**Last Updated**: v1.2.3 (August 2026) + Unreleased kernel file locking work, 1023 tests ✅ ## Test Summary -**Total Tests**: 998 ✅ (990 passing, 8 ignored) -- ✅ 663 unit tests (lib — includes Wave 1 hash-join and selective-lookup test modules, Wave 3 fault-injection unit tests, per-query limits #288, magic sets #289, FileLock same-process exclusion #304) +**Total Tests**: 1023 ✅ (1015 passing, 8 ignored) +- ✅ 678 unit tests (lib — includes Wave 1 hash-join and selective-lookup test modules, Wave 3 fault-injection unit tests, per-query limits #288, magic sets #289, kernel file lock same-process exclusion and unsupported-filesystem classification #304 #317, parser recursion depth bound #326, non-UTF-8-boundary temporal input #325) - ✅ 12 bi-temporal tests (integration) - ✅ 11 complex query tests (integration) - ✅ 9 recursive rules tests (integration) - ✅ 12 concurrency tests (integration, 1 ignored: nightly stress) -- ✅ 21 WAL / crash recovery tests (integration) +- ✅ 22 WAL / crash recovery tests (integration) +- ✅ 2 PID namespace tests (integration, #317 — kernel lock survives a holder killed at PID 1 in another namespace; skips rather than fails where `unshare` is unavailable) - ✅ 2 cross-platform compat tests (integration, Phase 8.1) - ✅ 6 index tests (integration, Phase 6.1) - ✅ 7 performance tests (integration, Phase 6.2/6.4b) @@ -29,25 +30,25 @@ - ✅ 10 UDF tests (integration, Phase 7.7b — custom aggregates, custom predicates, UDF as window function, name collision guards, runtime errors, thread safety) - ✅ 17 prepared statement tests (integration, Phase 7.8 — entity/value/as-of/valid-at slots, combined temporal+entity, AnyValidTime, error paths, plan reuse) - ✅ 3 grammar conformance tests (integration, Phase 7.9 — pest shadow grammar + EDN corpus) -- ✅ 5 migration matrix tests (integration, Wave 3 #215 — v7 round-trip, v3 empty migrate, corrupt magic, unsupported version, WAL replay idempotent) +- ✅ 6 migration matrix tests (integration, Wave 3 #215 — v7 round-trip, v3 empty migrate, corrupt magic, unsupported version, WAL replay idempotent) - ✅ 5 index corruption tests (integration, Wave 3 #216 — checksum corruption, btree leaf/internal no-panic, root pointer mismatch, non-critical corruption query check) - ✅ 3 property-based tests (integration, Wave 3 #212/#213/#219 — proptest Datalog correctness vs naive reference evaluator) - ✅ 1 long-haul smoke test (integration, Wave 3 #220 — 500 entities × 10 attrs × 10 cycles; ignored: nightly) -- ✅ 10 XTDB compat tests (integration, Wave 3 #221 — Apache 2.0 semantic ports of XTDB concepts) -- ✅ 9 Datomic compat tests (integration, Wave 3 #221 — independently written semantic ports of Datomic concepts) +- ✅ 14 XTDB compat tests (integration, Wave 3 #221 — Apache 2.0 semantic ports of XTDB concepts) +- ✅ 11 Datomic compat tests (integration, Wave 3 #221 — independently written semantic ports of Datomic concepts) - ✅ 7 magic sets tests (integration, #289 — demand-driven recursive evaluation correctness: bound transitive closure, all-free closure, subset invariant, multi-hop, mutual recursion) - ✅ 15 doc tests (9 passing, 6 ignored: doc examples referencing internal types that cannot compile as standalone rustdoc tests) -**Status**: ✅ **All 990 tests passing** (8 ignored: 6 internal-type doc examples, 1 nightly concurrency stress, 1 nightly smoke) +**Status**: ✅ **All 1015 tests passing** (8 ignored: 6 internal-type doc examples, 1 nightly concurrency stress, 1 nightly smoke) ## v1.2.2 Completion Status: ✅ COMPLETE **v1.2.2 issues**: #312 (lexicographic string comparison in predicates), #304 (two handles on one file in the same process) **New tests added by v1.2.2** (+3 storage-backend, +4 predicate-expression, +2 magic-sets): -- ✅ `src/storage/backend/file.rs` — 3 FileLock unit tests: same-process second open refused, sequential reopen-after-drop still works, dead *other* process's lock still reclaimed (procfs-gated, since `is_process_alive` errs towards "alive" without `/proc`) +- ✅ `src/storage/backend/file.rs` — lock unit tests: same-process second open refused (#304), cross-process `WouldBlock` retried then failed within budget, sequential reopen-after-drop, leftover v1.2.x sidecar ignored and preserved, path registry not corrupted by a refused open, plus 4 `classify` tests covering the unsupported-filesystem branch in both `allow_unlocked` settings - ✅ `tests/predicate_expr_test.rs` — string ordering via `<`, `>`, `<=`, `>=`; mixed-type and NaN behaviour pinned -- ✅ Crash-simulation idiom corrected across `tests/wal_test.rs` (13 sites) and `tests/migration_matrix_test.rs`: `mem::forget` skips `FileLock::drop`, leaving the sidecar lock holding our *own* (live) pid, which is not what a crash leaves behind. Those sites now clear the lock so the reopen models a crashed holder rather than depending on minigraf treating a live lock as stale. +- ✅ Crash simulation uses a real child process that `abort()`s (`run_crashing_child`), replacing the former `mem::forget` idiom. `mem::forget` leaks the `File`, so the kernel lock stays held for the life of the test process; only real process death releases it, which is also what production does. --- @@ -502,14 +503,33 @@ All Phase 8 sub-phases complete. See per-phase sections below. - Rule Registry: ~95% (6 tests) - Recursive Evaluator: ~95% (10 tests) -### 12. Storage Backends (`src/storage/backend/`) - ✅ Good (11 tests) +### 12. Storage Backends (`src/storage/backend/`) - ✅ Good (18 tests) - ✅ FileBackend create/write/read, persistence across close/reopen - ✅ MemoryBackend write/read, error handling -- ✅ **FileLock same-process exclusion** (#304): a second open while a handle is live is refused with an error naming the same-process case — two `FileBackend`s on one file each cache their own `header.page_count` and corrupt each other -- ✅ FileLock refusal leaves the existing lock in place; dropping the only handle releases it +- ✅ **Same-process exclusion** (#304): a second open while a handle is live is refused, enforced by the kernel — `flock`/OFD locks attach to the open file description, not the process +- ✅ **Holder death across PID namespaces** (#317): `tests/pid_namespace_test.rs` runs two processes at PID 1 in separate namespaces via `unshare`; the survivor opens after the holder is killed, and two live holders are still refused - ✅ Sequential open → drop → reopen still works (the per-commit cycle downstream callers rely on) -- ✅ A lock held by a *different*, dead process is still reclaimed (procfs-gated) +- ✅ A cross-process `WouldBlock` is retried with bounded backoff before being treated as a real conflict, and still fails once the retry budget is spent +- ✅ A leftover v1.2.x `.graph.lock` sidecar is ignored and left untouched +- ✅ A refused open does not corrupt the in-process open-path registry +- ✅ `classify` (unsupported-filesystem branch) fails closed by default and proceeds when `allow_unlocked` is set, in both cases regardless of a live `WouldBlock` + +**Note on counting these tests**: `hold_lock_entrypoint` (`src/storage/backend/file.rs`) +and `crash_child_entrypoint` (`tests/wal_test.rs`, `tests/migration_matrix_test.rs`) are +re-exec entry points, not ordinary tests — the parent test spawns the current test binary +as a child process (`storage::backend::file::tests::hold_lock_entrypoint --exact +--nocapture`) so a genuinely external process holds the lock, since `#[cfg(test)]` items +are invisible across process boundaries. `crash_child_entrypoint` `abort()`s before the +harness can print a summary, so it leaves no trace in `cargo test` output. `hold_lock_entrypoint` +exits normally instead (it is simulating an ordinary second process, not a crash), so its +own child-process test harness prints a stray `test result: ok. 1 passed; 0 failed; +0 ignored; 0 measured; 675 filtered out` line, interleaved into the parent run's output. +That line is easy to mistake for a second, smaller test run of the whole `lib` binary — +it is not; it is one test's child process reporting on itself. Exclude it when totalling +`cargo test` output: `grep "^test result:" | grep -vE "[1-9][0-9]* filtered out"` +drops any line with a nonzero filtered-out count, keeping only the 34 real per-binary +summaries. **Coverage**: ~85% @@ -551,10 +571,10 @@ All Phase 8 sub-phases complete. See per-phase sections below. - ✅ Valid-at inside/outside/boundary, default filter, any-valid-time - ✅ Combined bi-temporal (both dimensions), multi-entity valid ranges -### WAL / Crash Recovery (`tests/wal_test.rs`) - ✅ 21 tests +### WAL / Crash Recovery (`tests/wal_test.rs`) - ✅ 22 tests - ✅ Basic persistence (file-backed transact and query) -- ✅ WAL replay after `mem::forget` crash simulation (the sidecar lock is cleared alongside, so the reopen models a crashed holder rather than a live one — #304) +- ✅ WAL replay after real subprocess crash (`run_crashing_child`) — the kernel releases the lock when the child aborts, so the reopen models a genuinely crashed holder (#304, #317) - ✅ Stale WAL dedup via `last_checkpointed_tx_count` - ✅ Corrupt/partial entry discard on recovery - ✅ Manual checkpoint clears WAL and updates header @@ -696,13 +716,13 @@ All Phase 8 sub-phases complete. See per-phase sections below. - ✅ `execute_with_extra_bindings` — extra `BindValue`s beyond declared slots are silently ignored - ✅ `multiple_slots_same_execute` — multiple distinct `$slot` names resolved in a single `execute()` call -### Migration Matrix (`tests/migration_matrix_test.rs`) - ✅ 5 tests (Wave 3 #215) +### Migration Matrix (`tests/migration_matrix_test.rs`) - ✅ 6 tests (Wave 3 #215) - ✅ v7 round-trip — facts written and read back correctly after save/load - ✅ v3 empty migrate — empty v3 database opens and migrates to v7 cleanly - ✅ corrupt magic — file with bad magic header returns `Err`, not panic - ✅ unsupported version — file with unrecognised format version returns `Err` -- ✅ WAL replay idempotent — replaying a WAL twice produces the same result as replaying once (crash simulated by `mem::forget` plus clearing the sidecar lock — #304) +- ✅ WAL replay idempotent — replaying a WAL twice produces the same result as replaying once (crash simulated by real subprocess crash via `run_crashing_child` — #304, #317) ### Index Corruption (`tests/index_corruption_test.rs`) - ✅ 5 tests (Wave 3 #216) @@ -722,20 +742,24 @@ All Phase 8 sub-phases complete. See per-phase sections below. - ✅ `smoke_large_graph_10_cycles` — 500 entities × 10 attributes × 10 update cycles; 7 invariants verified: active count (333), retracted count, fact count bounds, temporal snapshot integrity, prepared query consistency, rule transitive closure, WAL checkpoint round-trip -### XTDB Compatibility (`tests/xtdb_compat_test.rs`) - ✅ 10 tests (Wave 3 #221) - -- ✅ `xtdb_eav_triple_model` — entity attributes are independent queryable facts -- ✅ `xtdb_transaction_time_as_of` — `:as-of` by tx_count matches XTDB transaction-time semantics -- ✅ `xtdb_valid_time_travel` — `:valid-at` point-in-time filter matches XTDB valid-time semantics -- ✅ `xtdb_retraction_current_view` — retracted facts absent from current-time view -- ✅ `xtdb_retraction_historical_view` — retracted facts visible in pre-retraction snapshot -- ✅ `xtdb_datalog_join` — multi-pattern join matches XTDB Datalog query semantics -- ✅ `xtdb_datalog_negation` — `not` clause matches XTDB negation semantics -- ✅ `xtdb_recursive_rules` — recursive rule transitive closure matches XTDB rule semantics -- ✅ `xtdb_parameterized_query` — prepared-statement `$slot` bindings match XTDB `:in` semantics -- ✅ `xtdb_bitemporal_combined` — combined `:as-of` + `:valid-at` query matches XTDB bi-temporal semantics - -### Datomic Compatibility (`tests/datomic_compat_test.rs`) - ✅ 9 tests (Wave 3 #221) +### XTDB Compatibility (`tests/xtdb_compat_test.rs`) - ✅ 14 tests (Wave 3 #221) + +- ✅ `xtdb_basic_find_by_attribute_value` — finds all entities with a given attribute value (`:profession "painter"`) +- ✅ `xtdb_multi_attribute_join` — multi-pattern join returns only entities satisfying both patterns (`:role "admin"` and `:active true`) +- ✅ `xtdb_entity_reference_join` — entities sharing a reference-valued attribute (`:dept :dept-eng`) are found by pattern match +- ✅ `xtdb_retraction_removes_specific_fact` — retracting one fact leaves other facts on the same entity intact +- ✅ `xtdb_retracted_fact_not_visible` — a retracted fact is absent from the current-time query result +- ✅ `xtdb_as_of_returns_past_state` — `:as-of` by tx_count returns the value that held before a later retract+transact pair +- ✅ `xtdb_valid_at_query` — `:valid-at` finds a fact inside its `:valid-from`/`:valid-to` range and excludes it outside +- ✅ `xtdb_not_excludes_matching_entities` — `not` clause excludes entities that also match a second pattern (banned flag) +- ✅ `xtdb_count_aggregate` — `(count ?e)` aggregate returns the correct tally for a filtered pattern +- ✅ `xtdb_recursive_ancestor_rule` — recursive rule computes transitive ancestor closure over a parent chain +- ✅ `xtdb_attribute_keyword_may_end_in_question_mark` — predicate-style keyword attribute (`:artist/dead?`) is queryable and binds correctly by value (regression: `?` used to terminate the keyword token) +- ✅ `xtdb_keyword_value_may_end_in_question_mark` — a keyword value ending in `?` (`:ready?`) round-trips through storage and matches both by value and by binding +- ✅ `xtdb_retract_predicate_named_attribute` — retraction targets a predicate-named attribute correctly, leaving sibling facts on the same entity intact +- ✅ `xtdb_valid_time_query_over_predicate_named_attribute` — `:valid-at` filtering works over a predicate-named attribute the same as any other + +### Datomic Compatibility (`tests/datomic_compat_test.rs`) - ✅ 11 tests (Wave 3 #221) - ✅ `datomic_entity_attributes_are_independent_facts` — EAV datom model: each attribute independently queryable - ✅ `datomic_multiple_entities_same_attribute` — multiple entities share the same attribute; all (entity, value) pairs returned @@ -746,6 +770,8 @@ All Phase 8 sub-phases complete. See per-phase sections below. - ✅ `datomic_parameterized_query_prepared` — prepared `$slot` bindings match Datomic `:in` clause semantics - ✅ `datomic_named_rule_reuse` — named reusable rules match Datomic rule semantics - ✅ `datomic_predicate_expression_filter` — predicate expression `[(>= ?a 18)]` matches Datomic expression clause semantics +- ✅ `datomic_attribute_keyword_may_end_in_question_mark` — predicate-style keyword attribute (`:person/alive?`) is queryable and binds correctly by value (regression: `?` used to terminate the keyword token) +- ✅ `datomic_keyword_value_may_end_in_question_mark` — a keyword value ending in `?` (`:ready?`) matches by value and round-trips through storage --- @@ -870,7 +896,6 @@ covered above. ### Known Limitations - ⏳ Crash during checkpoint write (safe by construction — WAL not deleted until save succeeds; covered indirectly by the Wave 3 checkpoint-atomicity fault-injection tests) -- ⏳ Stale-lock reclamation is procfs-only: without `/proc`, `is_process_alive` cannot distinguish a dead pid from a live one and deliberately errs towards "alive", so a crashed holder's lock must be removed by hand on macOS and Windows (#304) - ⏳ Known `not-join` limitation: when a rule B positively invokes rule A and both are stratum 0, single-pass mixed-rule evaluation means B may not see A's derived facts unless rules are declared in dependency order - ⏳ `matches?` pattern compiled per-row (no caching); will be optimised in Phase 7.9b (`FunctionRegistry`) @@ -914,8 +939,8 @@ cargo test --test migration_matrix_test # migration matrix (5) cargo test --test index_corruption_test # index corruption (5) cargo test --test cross_platform_compat_test # cross-platform compat (2) cargo test --test property_test # property-based (3) -cargo test --test xtdb_compat_test # XTDB compat (10) -cargo test --test datomic_compat_test # Datomic compat (9) +cargo test --test xtdb_compat_test # XTDB compat (14) +cargo test --test datomic_compat_test # Datomic compat (11) cargo test --test smoke_test -- --include-ignored # long-haul smoke (1, nightly) # Run with output @@ -931,7 +956,7 @@ cargo test -- --nocapture **Test Quality**: ✅ **Excellent** — High confidence in all Phase 3-8.1 + Wave 3 reliability features **Strengths**: -- WAL crash safety verified with real `mem::forget` simulation +- WAL crash safety verified with a real subprocess crash (`run_crashing_child`) - Both implicit and explicit transaction write paths verified - Thread safety proven with Barrier-synchronized concurrent tests - Index persistence and CRC32 sync check verified @@ -955,10 +980,10 @@ cargo test -- --nocapture - Index corruption resilience verified: checksum corruption triggers rebuild, btree corruption returns Err not panic (Wave 3) - Property-based testing verified: EAV model, bi-temporal monotonicity, retract visibility (Wave 3) - Long-haul smoke verified: 500 entities × 10 attrs × 10 cycles, 7 invariants, nightly CI (Wave 3) -- XTDB compatibility verified: 10 semantic ports covering EAV, time travel, negation, rules, prepared queries (Wave 3) -- Datomic compatibility verified: 9 independently written semantic ports covering datom model, tx-time, retraction, Datalog patterns (Wave 3) +- XTDB compatibility verified: 14 semantic ports covering EAV, time travel, negation, rules, prepared queries (Wave 3) +- Datomic compatibility verified: 11 independently written semantic ports covering datom model, tx-time, retraction, Datalog patterns (Wave 3) - Same-process handle exclusion verified: a second open on a live file is refused rather than silently granted, which is what produced the intermittent `Page N out of bounds` (v1.2.2, #304) -- 998 tests covering all Phase 3-8.1 features + Wave 3 reliability/compat (including browser WASM + WASI + cross-platform compat + fuzzing CI) +- 1023 tests covering all Phase 3-8.1 features + Wave 3 reliability/compat + kernel file locking (including browser WASM + WASI + cross-platform compat + fuzzing CI) **Confidence Level**: ✅ **Production-ready for Wave 3 scope** diff --git a/docs/superpowers/plans/2026-08-22-kernel-file-locking.md b/docs/superpowers/plans/2026-08-22-kernel-file-locking.md new file mode 100644 index 0000000000..e3087ef5ba --- /dev/null +++ b/docs/superpowers/plans/2026-08-22-kernel-file-locking.md @@ -0,0 +1,1196 @@ +# Kernel File Locking Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Replace the PID-sidecar `FileLock` with kernel file locking via `std::fs::File::try_lock`, closing #317 (a `.graph` file permanently unopenable after a container restart) and #304 (two handles on one file in one process). + +**Architecture:** `FileBackend` already holds an open `File` on the `.graph` file. The lock moves onto that file descriptor, so the kernel owns liveness: a dead holder's lock is released by the OS, and a second open in the same process is denied because `flock`/OFD locks attach to the open file description rather than the process. The `.graph.lock` sidecar, `is_process_alive`, and the recursion in `acquire` are all deleted. A process-global path set survives only to choose between two error messages. + +**Tech Stack:** Rust 2024 edition, `std::fs::File::try_lock` / `TryLockError` (stable 1.89), `std::sync::LazyLock`, `tempfile` for tests, `unshare(1)` for the PID-namespace test. + +**Spec:** `docs/superpowers/specs/2026-08-22-kernel-file-locking-design.md` + +## Global Constraints + +- **MSRV is Rust 1.89.** `Cargo.toml` must declare `rust-version = "1.89"`. `File::try_lock` is `#[stable(feature = "file_lock", since = "1.89.0")]`. +- **No new dependencies.** CI's binary-size gate is 1 MiB and the last measured build was 1006 KiB — 18 KiB of headroom. Do not add `fs4`, `rustix`, `libc`, or `windows-sys`. +- **Never use `{:?}` on `Result`, `Fact`, `Value`, `EdnValue`, or anything transitively containing a `Uuid` in assert messages.** CodeQL flags it as `rust/cleartext-logging` and it blocks CI. Use plain string messages, or `unwrap()`/`expect()`. See CLAUDE.md. +- **`allow_unlocked` never overrides a live lock.** It applies only when the filesystem cannot lock. Enforce in code, not by convention. +- **Do not delete a leftover `.graph.lock`.** A still-running v1.2.x process may depend on it. Ignore it. +- **Scope excludes the nightly NFS and Docker CI work**, tracked separately in issue #324. + +--- + +### Task 1: `classify` — the lock decision, as a pure function + +Isolating the decision from the I/O makes the unsupported-filesystem branch testable without an exotic filesystem, which is otherwise untestable in CI. + +**Files:** +- Modify: `Cargo.toml` (add `rust-version`) +- Modify: `src/storage/backend/file.rs` (add `LockOutcome`, `classify`, and unit tests) + +**Interfaces:** +- Consumes: nothing. +- Produces: `enum LockOutcome { Acquired, Held, Unsupported(std::io::Error), ProceedUnlocked }` and `fn classify(result: Result<(), std::fs::TryLockError>, allow_unlocked: bool) -> LockOutcome`, both private to the module. Task 2 calls `classify`. + +- [ ] **Step 1: Declare the MSRV** + +In `Cargo.toml`, in the `[package]` section, directly after the `edition = "2024"` line: + +```toml +rust-version = "1.89" +``` + +- [ ] **Step 2: Write the failing tests** + +Append inside the existing `mod tests` block in `src/storage/backend/file.rs` (the block opens at `#[cfg(all(test, not(target_arch = "wasm32")))]`): + +```rust + #[test] + fn test_classify_ok_is_acquired() { + assert!(matches!(classify(Ok(()), false), LockOutcome::Acquired)); + assert!(matches!(classify(Ok(()), true), LockOutcome::Acquired)); + } + + #[test] + fn test_classify_would_block_is_held_regardless_of_allow_unlocked() { + // allow_unlocked must NOT override a live lock: doing so reintroduces + // exactly the cross-process corruption the lock exists to prevent. + assert!(matches!( + classify(Err(std::fs::TryLockError::WouldBlock), false), + LockOutcome::Held + )); + assert!(matches!( + classify(Err(std::fs::TryLockError::WouldBlock), true), + LockOutcome::Held + )); + } + + #[test] + fn test_classify_unsupported_fails_closed_by_default() { + let e = std::io::Error::from_raw_os_error(95); // ENOTSUP + assert!(matches!( + classify(Err(std::fs::TryLockError::Error(e)), false), + LockOutcome::Unsupported(_) + )); + } + + #[test] + fn test_classify_unsupported_proceeds_when_allowed() { + let e = std::io::Error::from_raw_os_error(37); // ENOLCK + assert!(matches!( + classify(Err(std::fs::TryLockError::Error(e)), true), + LockOutcome::ProceedUnlocked + )); + } +``` + +- [ ] **Step 3: Run the tests to verify they fail** + +Run: `cargo test --lib classify` +Expected: FAIL to compile — `cannot find function 'classify' in this scope`. + +- [ ] **Step 4: Write the implementation** + +Add near the top of `src/storage/backend/file.rs`, after the `use` statements and before `pub struct FileBackend`: + +```rust +/// What `FileBackend::open` should do with the result of `File::try_lock`. +/// +/// Split out from the I/O so the unsupported-filesystem branch is testable +/// without an exotic filesystem: no CI runner can be relied on to provide a +/// mount whose locks fail. +enum LockOutcome { + /// We hold the lock. + Acquired, + /// Another open file description holds it. May be in this process. + Held, + /// The filesystem cannot lock, and the caller did not opt in to running + /// without one. + Unsupported(std::io::Error), + /// The filesystem cannot lock and the caller accepted the risk. + ProceedUnlocked, +} + +/// Decide what a `try_lock` result means. +/// +/// `allow_unlocked` is consulted ONLY for `TryLockError::Error`, never for +/// `WouldBlock`. Letting it bypass a live lock would reintroduce the +/// two-writers-one-file corruption the lock exists to prevent. +fn classify(result: Result<(), std::fs::TryLockError>, allow_unlocked: bool) -> LockOutcome { + match result { + Ok(()) => LockOutcome::Acquired, + Err(std::fs::TryLockError::WouldBlock) => LockOutcome::Held, + Err(std::fs::TryLockError::Error(_)) if allow_unlocked => LockOutcome::ProceedUnlocked, + Err(std::fs::TryLockError::Error(e)) => LockOutcome::Unsupported(e), + } +} +``` + +- [ ] **Step 5: Run the tests to verify they pass** + +Run: `cargo test --lib classify` +Expected: PASS, 4 tests. + +- [ ] **Step 6: Commit** + +```bash +git add Cargo.toml src/storage/backend/file.rs +git commit -m "feat(storage): add lock-decision classifier and declare MSRV 1.89 + +Isolates the try_lock decision from the I/O so the unsupported-filesystem +branch is testable without a filesystem that cannot lock. + +allow_unlocked deliberately does not apply to WouldBlock." +``` + +--- + +### Task 2: Replace `FileLock` with the kernel lock + +**Files:** +- Modify: `src/storage/backend/file.rs` — delete `FileLock` (lines 8-114 in the current file: the struct, `impl FileLock`, `is_process_alive`, and `impl Drop for FileLock`), rewrite `FileBackend::open`, replace the three existing lock unit tests +- Test: `src/storage/backend/file.rs` `mod tests` + +**Interfaces:** +- Consumes: `classify`, `LockOutcome` from Task 1. +- Produces: `FileBackend::open>(path: P) -> Result` (unchanged signature, `allow_unlocked = false`) and `FileBackend::open_with>(path: P, allow_unlocked: bool) -> Result`. Task 3 calls `open_with`. The `FileBackend` struct field `_lock: FileLock` becomes `_path_guard: PathGuard`. + +**Why `open_with` rather than adding a parameter:** `FileBackend::open` has roughly 35 call sites, nearly all in tests. Keeping `open` and adding `open_with` leaves them untouched. + +- [ ] **Step 1: Write the failing tests** + +In `src/storage/backend/file.rs`, replace the three existing tests — `test_second_open_in_same_process_is_refused`, `test_reopen_after_drop_still_works`, and `test_dead_other_process_lock_is_still_reclaimed` — with these. Note the third is deleted outright, not rewritten: reclaiming a dead process's lock is now the kernel's job and there is nothing left in our code to test. + +```rust + /// #304: a second handle on a file this process already has open must be + /// refused. `flock` and OFD locks attach to the open file description, not + /// the process, so the kernel denies this without any bookkeeping from us. + #[test] + fn test_second_open_in_same_process_is_refused() { + let dir = tempfile::tempdir().unwrap(); + let temp_path = dir.path().join("test_minigraf_self_steal.graph"); + + let first = FileBackend::open(&temp_path).unwrap(); + + // FileBackend is not Debug, so match on the Result rather than + // expect_err. + let msg = match FileBackend::open(&temp_path) { + Ok(_) => panic!("a second handle in the same process must be refused"), + Err(e) => e.to_string(), + }; + assert!( + msg.contains("already open in this process"), + "error should name the same-process case" + ); + + drop(first); + // Once the only handle is dropped, the kernel releases the lock. + FileBackend::open(&temp_path).unwrap(); + } + + /// The refusal must not regress the sequential open/drop/reopen cycle that + /// `temporal_reasoning` relies on between commits. + #[test] + fn test_reopen_after_drop_still_works() { + let dir = tempfile::tempdir().unwrap(); + let temp_path = dir.path().join("test_minigraf_reopen.graph"); + + for _ in 0..3 { + let backend = FileBackend::open(&temp_path).unwrap(); + drop(backend); + } + assert!(FileBackend::open(&temp_path).is_ok()); + } + + /// A leftover sidecar from v1.2.x must not block an open. We neither read + /// nor delete it: a still-running old process may rely on it. + #[test] + fn test_leftover_sidecar_is_ignored_and_preserved() { + let dir = tempfile::tempdir().unwrap(); + let temp_path = dir.path().join("test_minigraf_leftover.graph"); + let legacy = temp_path.with_extension("graph.lock"); + std::fs::write(&legacy, "1").unwrap(); + + let backend = FileBackend::open(&temp_path).unwrap(); + drop(backend); + + assert!( + legacy.exists(), + "a leftover sidecar must be left alone, not deleted" + ); + } + + /// The path registry must not leak entries: a refused open must leave the + /// set exactly as it found it, or a later legitimate open reports the + /// wrong error. + #[test] + fn test_refused_open_does_not_corrupt_path_registry() { + let dir = tempfile::tempdir().unwrap(); + let temp_path = dir.path().join("test_minigraf_registry.graph"); + + let first = FileBackend::open(&temp_path).unwrap(); + assert!(FileBackend::open(&temp_path).is_err()); + drop(first); + + // If the refused open had removed the path, or the guard had failed to, + // this would report the same-process case instead of succeeding. + FileBackend::open(&temp_path).unwrap(); + } +``` + +- [ ] **Step 2: Run the tests to verify they fail** + +Run: `cargo test --lib storage::backend::file` +Expected: `test_leftover_sidecar_is_ignored_and_preserved` FAILS — the current code reads the sidecar, sees PID 1 (or whatever was written), and either refuses or reclaims it. The other three may pass against the old code; that is expected and fine. + +- [ ] **Step 3: Delete `FileLock` entirely** + +Remove from `src/storage/backend/file.rs`: the `struct FileLock` declaration and its doc comment, the whole `impl FileLock` block (including `acquire` and `is_process_alive`), and `impl Drop for FileLock`. This is the contiguous region from the `/// Advisory file lock to prevent multi-process corruption.` comment through the closing brace of `impl Drop for FileLock`. + +- [ ] **Step 4: Add the path registry** + +Add to `src/storage/backend/file.rs`, immediately after the `classify` function from Task 1: + +```rust +/// Canonicalised paths this process currently has open. +/// +/// Purely diagnostic. Correctness comes from the kernel lock; this only picks +/// between two error messages. The distinction matters because #304 was +/// misdiagnosed for a long time when the generic "another process" wording +/// sent the investigation looking for a second process that did not exist. +static OPEN_PATHS: std::sync::LazyLock>> = + std::sync::LazyLock::new(|| std::sync::Mutex::new(std::collections::HashSet::new())); + +/// Removes this backend's path from `OPEN_PATHS` when the backend drops. +struct PathGuard(Option); + +impl Drop for PathGuard { + fn drop(&mut self) { + if let Some(path) = self.0.take() + && let Ok(mut open) = OPEN_PATHS.lock() + { + open.remove(&path); + } + } +} + +/// True if this process already has `path` open. Diagnostic only. +fn already_open_here(path: &Path) -> bool { + OPEN_PATHS + .lock() + .map(|open| open.contains(path)) + .unwrap_or(false) +} +``` + +- [ ] **Step 5: Rewrite `FileBackend::open`** + +Change the struct field in `pub struct FileBackend`, replacing `_lock: FileLock,` with: + +```rust + _path_guard: PathGuard, +``` + +Replace `FileBackend::open` and its doc comment with the following. Note the inverted order: the file is opened **first**, then locked, because the lock now lives on that file descriptor. `canonicalize` also requires the file to exist. + +```rust + /// Open or create a .graph file at the given path. + /// + /// If the file doesn't exist, creates it with an initial header. + /// If it exists, validates and loads the header. + /// + /// Takes a kernel file lock on the `.graph` file itself, which prevents + /// both multi-process corruption and a second handle within this process. + /// The kernel releases the lock when the process exits, however it exits, + /// so a crashed holder never leaves the database unopenable (#317). + pub fn open>(path: P) -> Result { + Self::open_with(path, false) + } + + /// As [`FileBackend::open`], but `allow_unlocked` permits opening on a + /// filesystem that cannot lock at all. + /// + /// `allow_unlocked` does NOT override a lock held by someone else. It + /// applies only when the filesystem rejects locking outright. + pub fn open_with>(path: P, allow_unlocked: bool) -> Result { + let path = path.as_ref().to_path_buf(); + + let mut file = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .truncate(false) + .open(&path)?; + + // Canonicalise only after the file exists. Used for the diagnostic + // registry; if it fails we fall back to the generic message. + let canonical = std::fs::canonicalize(&path).unwrap_or_else(|_| path.clone()); + + let path_guard = match classify(file.try_lock(), allow_unlocked) { + LockOutcome::Acquired => { + if let Ok(mut open) = OPEN_PATHS.lock() { + open.insert(canonical.clone()); + } + PathGuard(Some(canonical)) + } + LockOutcome::Held if already_open_here(&canonical) => { + anyhow::bail!( + "Database is already open in this process ({}). A second handle \ + on one file would give each its own page table and corrupt both \ + — reuse the existing handle instead. `Minigraf` is cheap to clone \ + and all clones share the same database.", + path.display() + ); + } + LockOutcome::Held => { + anyhow::bail!( + "Database is locked by another process ({}). The lock is held on \ + the file itself and is released automatically when the holding \ + process exits, so there is no lock file to clean up.", + path.display() + ); + } + LockOutcome::Unsupported(e) => { + anyhow::bail!( + "Failed to lock database at {}: {}. This filesystem does not \ + support file locking (common on NFSv3 without lockd, and on some \ + FUSE mounts). Set `allow_unlocked` in `OpenOptions` to open \ + anyway — that accepts the risk that concurrent writers corrupt \ + the file.", + path.display(), + e + ); + } + LockOutcome::ProceedUnlocked => PathGuard(None), + }; + + // Check file size using the open file handle's metadata. + // This is more reliable than checking path metadata separately, + // as it uses the same file descriptor we'll be reading from. + let file_len = file.metadata()?.len(); + + // Determine if this is an existing file with data or a new/empty one. + let is_new = file_len < PAGE_SIZE as u64; + let header = if file_len >= PAGE_SIZE as u64 { + // File has at least one page - try to read the header + match Self::read_header(&mut file) { + Ok(header) => header, + Err(e) => { + // File has content but header is invalid - this is a real error + anyhow::bail!( + "Failed to read header from existing file (size={}): {}", + file_len, + e + ); + } + } + } else { + // New file or empty file: write initial header + let header = FileHeader::new(); + Self::write_header(&mut file, &header)?; + header + }; + + Ok(FileBackend { + path, + file, + header, + is_new, + _path_guard: path_guard, + }) + } +``` + +- [ ] **Step 6: Run the full lib test suite** + +Run: `cargo test --lib` +Expected: PASS. If `use std::io::Write;` is now unused, remove it — `write!` was only used to write the PID into the sidecar. + +- [ ] **Step 7: Commit** + +```bash +git add src/storage/backend/file.rs +git commit -m "fix(storage): lock the .graph file in the kernel, not a PID sidecar + +A PID is not a liveness token. It is not unique across PID namespaces and +is recycled within one, so the sidecar could never distinguish a crashed +holder from a live one. A container killed with SIGKILL left the sidecar +holding pid 1, and the replacement container -- also pid 1 -- refused to +open the database forever (#317). + +File::try_lock puts the decision in the kernel, which releases the lock on +process death however it occurs. Because flock and OFD locks attach to the +open file description rather than the process, this also denies a second +open within one process, covering #304 with no bookkeeping. + +A leftover sidecar from v1.2.x is ignored, never deleted: a still-running +old process may depend on it." +``` + +--- + +### Task 3: `OpenOptions::allow_unlocked` + +**Files:** +- Modify: `src/db.rs` — `OpenOptions` struct (around line 63), its `Default` impl (around line 78), its builder impl (near the `page_cache_size` builder at line 98), the `open_with_options` call site (line 293), and the stale sidecar comment (line 610) +- Test: `src/db.rs` `mod tests` + +**Interfaces:** +- Consumes: `FileBackend::open_with` from Task 2. +- Produces: `OpenOptions { pub allow_unlocked: bool, .. }` and the builder `OpenOptions::allow_unlocked(self, allow: bool) -> Self`. + +`OpenOptions` exposes both public fields and chainable builder methods, so this needs both to match the existing pattern. + +- [ ] **Step 1: Write the failing test** + +Add to the `mod tests` block in `src/db.rs`: + +```rust + #[test] + fn test_allow_unlocked_defaults_to_false() { + assert!(!OpenOptions::default().allow_unlocked); + } + + #[test] + fn test_allow_unlocked_builder_sets_the_field() { + assert!(OpenOptions::default().allow_unlocked(true).allow_unlocked); + } + + /// `allow_unlocked` must not open a door past a live lock. It applies only + /// where the filesystem cannot lock at all. + #[test] + fn test_allow_unlocked_does_not_bypass_a_live_lock() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("allow_unlocked.graph"); + + let first = Minigraf::open(&path).unwrap(); + let opts = OpenOptions::default().allow_unlocked(true); + assert!( + Minigraf::open_with_options(&path, opts).is_err(), + "allow_unlocked must not bypass a lock that is actually held" + ); + drop(first); + } +``` + +- [ ] **Step 2: Run the tests to verify they fail** + +Run: `cargo test --lib allow_unlocked` +Expected: FAIL to compile — `no field 'allow_unlocked' on type 'OpenOptions'`. + +- [ ] **Step 3: Add the field** + +In `src/db.rs`, add to `pub struct OpenOptions`, after `pub max_results: usize,`: + +```rust + /// Open even when the filesystem cannot lock files at all. + /// + /// Defaults to `false`, which refuses to open rather than run without the + /// protection the lock provides. Set this only when you know the database + /// has a single writer by other means — for example an NFSv3 mount with no + /// `lockd`, where locking is unavailable rather than merely contended. + /// + /// This does **not** override a lock that another handle actually holds. + /// Opening a database twice is refused regardless of this setting. + pub allow_unlocked: bool, +``` + +In the `Default` impl, after `max_results: DEFAULT_MAX_RESULTS,`: + +```rust + allow_unlocked: false, +``` + +- [ ] **Step 4: Add the builder method** + +In `src/db.rs`, next to the `page_cache_size` builder method: + +```rust + /// Open even when the filesystem cannot lock files. See + /// [`OpenOptions::allow_unlocked`] for when this is appropriate. + #[must_use] + pub fn allow_unlocked(mut self, allow: bool) -> Self { + self.allow_unlocked = allow; + self + } +``` + +- [ ] **Step 5: Plumb it through** + +In `src/db.rs`, in `open_with_options`, replace: + +```rust + let backend = FileBackend::open(&db_path)?; +``` + +with: + +```rust + let backend = FileBackend::open_with(&db_path, opts.allow_unlocked)?; +``` + +Then fix the stale comment around line 610, replacing the sentence beginning `// File locking (\`.graph.lock\` sidecar, acquired by FileBackend::open)` and the lines through `// same-process double-opens (same PID bypasses the stale-lock check)` with: + +```rust + // The kernel file lock taken by FileBackend::open covers both a + // second process and a second handle in this one, since flock + // and OFD locks attach to the open file description. This guard + // remains for environments where the lock is unavailable and + // the caller set `allow_unlocked` (e.g. +``` + +Keep whatever text followed on the original line so the sentence still closes. + +- [ ] **Step 6: Run the tests to verify they pass** + +Run: `cargo test --lib allow_unlocked` +Expected: PASS, 3 tests. + +- [ ] **Step 7: Commit** + +```bash +git add src/db.rs +git commit -m "feat(db): add OpenOptions::allow_unlocked + +Fails closed by default when a filesystem cannot lock. The escape hatch is +for single-writer-by-convention deployments on mounts without working +locks; it never bypasses a lock another handle holds." +``` + +--- + +### Task 4: Real subprocess crash simulation + +`mem::forget` no longer models a crash. It leaks the `File`, so the fd stays open and the kernel lock is held for the rest of the test process — and no external cleanup can release it. A real child process that aborts is both the correct model and the only one that needs no test-only API in the shipping crate. + +**Files:** +- Modify: `tests/wal_test.rs` — add the helper and the child entry point, convert 13 sites (`std::mem::forget(` at lines 89, 132, 192, 280, 325, 374, 524, 559, 614, 641, 665, 684, 705), delete `simulate_crashed_holder` (line 840) and its 13 call sites +- Modify: `tests/migration_matrix_test.rs` — convert 1 site (line 101) and delete the manual `remove_file` of the sidecar that follows it + +**Interfaces:** +- Consumes: nothing from earlier tasks. +- Produces: `fn run_crashing_child(db_path: &std::path::Path, wal_checkpoint_threshold: usize, statements: &[&str])`, duplicated in both test files (integration test binaries do not share code without a `tests/common/` module; duplicating one small helper is simpler than adding one). + +- [ ] **Step 1: Write the helper and its child entry point** + +Add near the top of `tests/wal_test.rs`, after the existing `use` statements: + +```rust +/// Runs a child process that opens `db_path`, executes `statements`, then dies +/// by `abort()` without running any `Drop`. +/// +/// This is a real crash, not a simulated one: the child's file descriptors are +/// closed by the kernel, which releases the file lock exactly as it would for a +/// SIGKILLed container. `mem::forget` cannot model this — it leaks the `File`, +/// so the lock stays held for the life of the test process. +fn run_crashing_child( + db_path: &std::path::Path, + wal_checkpoint_threshold: usize, + statements: &[&str], +) { + let exe = std::env::current_exe().expect("test binary path"); + let status = std::process::Command::new(exe) + .args(["crash_child_entrypoint", "--exact", "--nocapture"]) + .env("MINIGRAF_CRASH_DB", db_path) + .env("MINIGRAF_CRASH_THRESHOLD", wal_checkpoint_threshold.to_string()) + .env("MINIGRAF_CRASH_STMTS", statements.join("\u{1e}")) + .status() + .expect("spawn crash child"); + + // On Unix `abort()` raises SIGABRT and `code()` is None. On Windows it + // terminates with a nonzero exit code instead. The property that matters + // on both is that the child did not exit cleanly. + assert!( + status.code().unwrap_or(1) != 0, + "crash child must not exit cleanly" + ); +} + +/// The child half of [`run_crashing_child`]. A no-op in ordinary test runs. +/// +/// This is a `#[test]` purely so the harness will dispatch to it by name; when +/// the marker environment variable is absent it returns immediately. +#[test] +fn crash_child_entrypoint() { + let Ok(db_path) = std::env::var("MINIGRAF_CRASH_DB") else { + return; + }; + let threshold: usize = std::env::var("MINIGRAF_CRASH_THRESHOLD") + .expect("threshold") + .parse() + .expect("threshold is a number"); + let stmts = std::env::var("MINIGRAF_CRASH_STMTS").expect("statements"); + + let db = Minigraf::open_with_options( + &db_path, + OpenOptions { + wal_checkpoint_threshold: threshold, + ..Default::default() + }, + ) + .expect("child opens db"); + + for stmt in stmts.split('\u{1e}').filter(|s| !s.is_empty()) { + db.execute(stmt).expect("child statement"); + } + + // Die without running Drop, so no checkpoint happens and the WAL is left + // for the next open to replay. + std::process::abort(); +} +``` + +- [ ] **Step 2: Convert the first site and verify the shape** + +In `tests/wal_test.rs`, `test_wal_recovery_after_simulated_crash`, replace the whole block that opens the db, executes, calls `std::mem::forget(db)` and then `simulate_crashed_holder(&db_path)` with: + +```rust + run_crashing_child( + &db_path, + 1_000_000, + &[r#"(transact [[:alice :name "Alice"]])"#], + ); +``` + +Keep the surrounding assertions unchanged. + +- [ ] **Step 3: Run that single test** + +Run: `cargo test --test wal_test test_wal_recovery_after_simulated_crash -- --nocapture` +Expected: PASS. The WAL exists after the child dies, and the reopen recovers the fact. + +- [ ] **Step 4: Convert the remaining 12 sites in `wal_test.rs`** + +Apply the same shape at each remaining `std::mem::forget(` site: lines 132, 192, 280, 325, 374, 524, 559, 614, 641, 665, 684, 705. For each, read the block's existing `OpenOptions` to get its `wal_checkpoint_threshold` and pass that value; collect the `db.execute(...)` calls in that block into the `statements` slice in order. Then delete the `fn simulate_crashed_holder` definition at line 840 and every remaining call to it. + +- [ ] **Step 5: Convert the site in `migration_matrix_test.rs`** + +Copy the `run_crashing_child` helper and `crash_child_entrypoint` from Step 1 into `tests/migration_matrix_test.rs`. Replace the block containing `std::mem::forget(db)` at line 101 with: + +```rust + run_crashing_child(&path, 1000, &[r#"(transact [[:e2 :color "blue"]])"#]); +``` + +Delete the comment block that follows about `mem::forget` skipping `FileLock::drop`, and the `std::fs::remove_file(path.with_extension("graph.lock")).unwrap();` line beneath it. There is no sidecar to remove, and the `unwrap()` would now panic. + +- [ ] **Step 6: Run both suites** + +Run: `cargo test --test wal_test --test migration_matrix_test` +Expected: PASS, with no reference to `simulate_crashed_holder` or `mem::forget` remaining. Verify with: + +```bash +grep -rn "simulate_crashed_holder\|mem::forget" tests/ +``` + +Expected: no output. + +- [ ] **Step 7: Commit** + +```bash +git add tests/wal_test.rs tests/migration_matrix_test.rs +git commit -m "test: simulate crashes with real subprocesses + +mem::forget no longer models a crash: it leaks the File, so the kernel +lock stays held for the life of the test process and no external cleanup +can release it. + +A child that aborts is both the accurate model -- the kernel closes its +descriptors exactly as it would for a SIGKILLed container -- and the one +that needs no test-only hook in the shipping crate. #[cfg(test)] cannot +reach tests/, and #[doc(hidden)] pub would be callable in production." +``` + +--- + +### Task 5: The #317 regression test + +**Files:** +- Create: `tests/pid_namespace_test.rs` +- Create: `examples/pid_ns_helper.rs` + +**Interfaces:** +- Consumes: nothing from earlier tasks. +- Produces: nothing consumed later. + +This is the test that reproduces the reported bug. Verified to fail against the pre-fix code: container A is SIGKILLed leaving the sidecar holding `1`, and container B — also PID 1 — is refused permanently. + +- [ ] **Step 1: Write the test file** + +Create `tests/pid_namespace_test.rs`: + +```rust +//! #317: a `.graph` file must stay openable after its holder dies in another +//! PID namespace. +//! +//! Kubernetes is where this was reported, but the bug has nothing to do with +//! Kubernetes: every container's main process is PID 1 in its own namespace, so +//! a lock keyed on a PID cannot tell a crashed holder from a live one. `unshare` +//! reproduces it in a few seconds with no container runtime. +#![cfg(target_os = "linux")] + +use std::path::Path; +use std::process::{Command, Stdio}; + +/// The `unshare` invocation that puts a command at PID 1 in a fresh namespace, +/// or `None` if this environment cannot create one. +/// +/// Ubuntu 24.04 restricts unprivileged user namespaces via +/// `kernel.apparmor_restrict_unprivileged_userns`, so fall back to `sudo` where +/// it is available passwordlessly (GitHub runners). If neither works the test +/// skips rather than fails: not every environment permits namespaces. +fn unshare_prefix() -> Option> { + let unprivileged: Vec = [ + "unshare", + "--user", + "--map-root-user", + "--pid", + "--fork", + "--mount-proc", + ] + .iter() + .map(|s| s.to_string()) + .collect(); + + let privileged: Vec = [ + "sudo", "-n", "unshare", "--pid", "--fork", "--mount-proc", + ] + .iter() + .map(|s| s.to_string()) + .collect(); + + for candidate in [unprivileged, privileged] { + let ok = Command::new(&candidate[0]) + .args(&candidate[1..]) + .args(["sh", "-c", "test \"$$\" = 1"]) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .map(|s| s.success()) + .unwrap_or(false); + if ok { + return Some(candidate); + } + } + None +} + +/// Runs the helper binary at PID 1 in a fresh PID namespace. +fn spawn_in_namespace(prefix: &[String], helper: &Path, db: &Path, mode: &str) -> std::process::Child { + Command::new(&prefix[0]) + .args(&prefix[1..]) + .arg(helper) + .arg(db) + .arg(mode) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("spawn in namespace") +} + +/// Path to the compiled helper binary (see `examples/pid_ns_helper.rs`). +fn helper_binary() -> std::path::PathBuf { + // target/debug/examples/pid_ns_helper, resolved relative to the test binary + // at target/debug/deps/pid_namespace_test-. + let mut dir = std::env::current_exe().expect("test binary path"); + dir.pop(); // deps/ + dir.pop(); // debug/ + dir.join("examples").join("pid_ns_helper") +} + +#[test] +fn test_lock_survives_holder_death_in_another_pid_namespace() { + let Some(prefix) = unshare_prefix() else { + eprintln!("SKIP: this environment cannot create PID namespaces"); + return; + }; + let helper = helper_binary(); + if !helper.exists() { + eprintln!("SKIP: helper not built; run `cargo build --examples` first"); + return; + } + + let dir = tempfile::tempdir().unwrap(); + let db = dir.path().join("shared.graph"); + + // Container A: opens the database and holds it. + let mut a = spawn_in_namespace(&prefix, &helper, &db, "hold"); + + // Wait for it to report that it holds the lock. + let mut held = false; + for _ in 0..80 { + if db.exists() && std::fs::metadata(&db).map(|m| m.len() > 0).unwrap_or(false) { + held = true; + break; + } + std::thread::sleep(std::time::Duration::from_millis(50)); + } + assert!(held, "container A never opened the database"); + + // Container A is OOMKilled. No Drop runs. + let _ = a.kill(); + let _ = a.wait(); + + // Container B: a different container, also PID 1 in its own namespace. + let b = spawn_in_namespace(&prefix, &helper, &db, "open") + .wait_with_output() + .expect("container B ran"); + + let out = String::from_utf8_lossy(&b.stdout); + assert!( + out.contains("OPEN_OK"), + "a replacement container must be able to open the database after its \ + predecessor was killed; this is #317" + ); +} + +#[test] +fn test_two_live_namespaced_holders_are_still_refused() { + let Some(prefix) = unshare_prefix() else { + eprintln!("SKIP: this environment cannot create PID namespaces"); + return; + }; + let helper = helper_binary(); + if !helper.exists() { + eprintln!("SKIP: helper not built; run `cargo build --examples` first"); + return; + } + + let dir = tempfile::tempdir().unwrap(); + let db = dir.path().join("contended.graph"); + + let mut a = spawn_in_namespace(&prefix, &helper, &db, "hold"); + let mut held = false; + for _ in 0..80 { + if db.exists() && std::fs::metadata(&db).map(|m| m.len() > 0).unwrap_or(false) { + held = true; + break; + } + std::thread::sleep(std::time::Duration::from_millis(50)); + } + assert!(held, "container A never opened the database"); + + // A is still alive. B must be refused — this is the guarantee PR #318's + // start-time comparison would have broken, admitting two live writers. + let b = spawn_in_namespace(&prefix, &helper, &db, "open") + .wait_with_output() + .expect("container B ran"); + let out = String::from_utf8_lossy(&b.stdout); + + let _ = a.kill(); + let _ = a.wait(); + + assert!( + out.contains("OPEN_ERR"), + "a second live holder in another PID namespace must still be refused" + ); +} +``` + +- [ ] **Step 2: Write the helper binary** + +Create `examples/pid_ns_helper.rs`: + +```rust +//! Helper for `tests/pid_namespace_test.rs`. Opens a database and either holds +//! it or reports whether the open succeeded. +//! +//! An example rather than a test so it is a standalone binary the test can run +//! under `unshare`. + +fn main() { + let args: Vec = std::env::args().collect(); + let path = &args[1]; + let mode = &args[2]; + + match minigraf::Minigraf::open(path) { + Ok(db) => { + db.execute(r#"(transact [[:a :name "A"]])"#).ok(); + println!("OPEN_OK"); + if mode == "hold" { + std::thread::sleep(std::time::Duration::from_secs(300)); + } + } + Err(e) => { + println!("OPEN_ERR {e}"); + std::process::exit(3); + } + } +} +``` + +- [ ] **Step 3: Build the helper and run the tests** + +Run: `cargo build --examples && cargo test --test pid_namespace_test -- --nocapture` +Expected: PASS, 2 tests (or SKIP with a printed reason if namespaces are unavailable). + +- [ ] **Step 4: Verify the test actually catches the bug** + +Confirm the test has real catching power by checking it against the old behaviour: + +`git stash` cannot help here: Tasks 1 and 2 are already committed, and stash only touches uncommitted work. Use a throwaway worktree at the pre-fix commit instead. The test uses only the public `Minigraf::open` API, so it compiles unchanged against the old code. + +```bash +TMP=$(mktemp -d) +git worktree add "$TMP/prefix" e056905 +cp tests/pid_namespace_test.rs "$TMP/prefix/tests/" +cp examples/pid_ns_helper.rs "$TMP/prefix/examples/" +(cd "$TMP/prefix" && cargo build --examples && cargo test --test pid_namespace_test -- --nocapture) +git worktree remove --force "$TMP/prefix" +rm -rf "$TMP" +``` + +Expected: `test_lock_survives_holder_death_in_another_pid_namespace` FAILS against the pre-fix code, reporting that container B could not open the database. If it passes, the test is not reproducing #317 and must be fixed before continuing. Report the observed failure output in your report file. + +- [ ] **Step 5: Commit** + +```bash +git add tests/pid_namespace_test.rs examples/pid_ns_helper.rs +git commit -m "test: reproduce #317 across PID namespaces + +Two processes at PID 1 in separate namespaces sharing one file, via +unshare -- no container runtime needed. Verified to fail against the +pre-fix code: the killed holder's sidecar left pid 1 behind and the +replacement was refused permanently. + +The second test guards the other direction: two live holders must still +be refused, which is what PR #318's start-time comparison would have +broken. + +Co-Authored-By: Olive Casazza " +``` + +--- + +### Task 6: Documentation + +**Files:** +- Modify: `CHANGELOG.md` — under the existing `## Unreleased` heading +- Modify: `docs/ERROR_REFERENCE.md` — new `STG-025`, `STG-026`, `STG-027` entries plus their Quick Reference Table rows +- Modify: `docs/TEST_COVERAGE.md` — lines 8, 48, 50, 509, 510, 557, 705 +- Modify: `.wiki/Architecture.md` — lines 205 and 231 +- Modify: `CLAUDE.md` — the stale test count on the "Test Coverage" line + +**Interfaces:** +- Consumes: the error text written in Task 2. +- Produces: nothing consumed later. + +- [ ] **Step 1: Add the CHANGELOG entry** + +In `CHANGELOG.md`, under `## Unreleased`, above the existing `### Bug fixes` heading, add a new section. The version-relevant facts are recorded explicitly because the release this lands in has not been decided: + +```markdown +### Breaking-ish changes + +- **Minimum supported Rust version is now 1.89** (was effectively 1.85, implied + by `edition = "2024"`). Required for `std::fs::File::try_lock`, stabilised in + 1.89. Declared as `rust-version` in `Cargo.toml`, so cargo reports a clear + error rather than a confusing one. +- **On Windows, an open database can no longer be read by other processes.** + `LockFileEx` locks are mandatory rather than advisory, so copying or backing + up an open `.graph` file now fails on Windows where it previously succeeded. + Unix locks remain advisory and are unaffected. Close the database before + copying it — which also avoids a torn copy. +- **`open()` now fails on filesystems that cannot lock at all** rather than + proceeding unprotected. Set `OpenOptions::allow_unlocked(true)` to accept the + risk on single-writer deployments. +``` + +Then under the existing `### Bug fixes` heading, above the keywords entry: + +```markdown +- **A `.graph` file is no longer bricked when its holder dies in a container** + (#317): `FileLock` recorded the holder's PID in a `.graph.lock` sidecar and + refused to open when that PID equalled our own. Every container's main process + is PID 1 in its own namespace, so a container killed with SIGKILL left the + sidecar holding `1`, and the replacement container — also PID 1 — read its own + PID back and refused. The refusal was permanent; no code path could clear it, + and an operator had to delete the file by hand. The `pid == our_pid` refusal + was itself the fix for #304, so the two defects were entangled. + + A PID cannot serve as a liveness token: it is not unique across PID namespaces + and is recycled within one. The sidecar is gone, and the lock now lives in the + kernel via `std::fs::File::try_lock` on the `.graph` file itself. The kernel + releases it whenever the process exits, however it exits, so a crashed holder + leaves nothing behind. Because `flock` and OFD locks attach to the open file + description rather than the process, a second open within one process is also + denied, so #304 stays fixed with no PID bookkeeping at all. + + A leftover `.graph.lock` from an earlier version is ignored, never deleted — a + still-running old process may still depend on it. Running mixed versions + against one file is not supported; upgrade all writers together. +``` + +- [ ] **Step 2: Add the three error entries** + +In `docs/ERROR_REFERENCE.md`, at the end of the `## STG — Storage Errors` section (after the `STG-024` entry, before `## WAL`): + +```markdown +### STG-025 Database already open in this process + +**Error text**: `Database is already open in this process (/path/to/db.graph). A second handle on one file would give each its own page table and corrupt both — reuse the existing handle instead.` + +**Cause**: This process already holds an open handle on this file. Two `FileBackend` instances on one file each cache their own `header.page_count`, allocate new pages from that count, and bounds-check reads against it, so the two page tables diverge and produce intermittent `Page N out of bounds` errors. + +**Resolution**: +- Reuse the handle you already have. `Minigraf` is cheap to clone and all clones share one database. +- If you cannot find the other handle, it is usually held by a longer-lived object than you expect — a cache, a registry, or a background task. + +**Scenario**: A request handler calls `Minigraf::open` per request while a connection pool already holds the same path open. + +### STG-026 Database locked by another process + +**Error text**: `Database is locked by another process (/path/to/db.graph). The lock is held on the file itself and is released automatically when the holding process exits.` + +**Cause**: Another process holds the kernel file lock on this `.graph` file. Minigraf is single-writer: one process at a time. + +**Resolution**: +- Wait for the other process to exit; the kernel releases the lock automatically, including when the process is killed. +- There is no lock file to delete. If you find a stale `.graph.lock`, it is a leftover from a version before 1.3 and has no effect. +- If two services genuinely need the same file, put one in front of the other. Minigraf is embedded, not a server. + +**Scenario**: A rolling deployment starts the new pod before the old one has exited, and both mount the same volume. + +### STG-027 Filesystem does not support file locking + +**Error text**: `Failed to lock database at /path/to/db.graph: Operation not supported (os error 95). This filesystem does not support file locking.` + +**Cause**: The underlying filesystem rejected the lock outright. Common on NFSv3 mounts with no `lockd` running, and on some FUSE filesystems. + +**Resolution**: +- Prefer moving the database to a filesystem that supports locking — this is the safe fix. +- On NFS, ensure NFSv4, or start `lockd` for NFSv3. +- If you are certain there is exactly one writer, set `OpenOptions::allow_unlocked(true)`. This accepts the risk that concurrent writers corrupt the file; Minigraf cannot detect them without a working lock. + +**Scenario**: A `.graph` file placed on an NFSv3 export whose `lockd` is not running. +``` + +Then add the matching rows to the Quick Reference Table, after the last `STG-024` row: + +```markdown +| STG-025 | Database already open in this process | Storage | +| STG-026 | Database locked by another process | Storage | +| STG-027 | Filesystem does not support file locking | Storage | +``` + +- [ ] **Step 3: Update the test coverage document** + +In `docs/TEST_COVERAGE.md`: + +- Line 48: replace the `FileLock` unit-test description with: `` - ✅ `src/storage/backend/file.rs` — lock unit tests: same-process second open refused (#304), sequential reopen-after-drop, leftover v1.2.x sidecar ignored and preserved, path registry not corrupted by a refused open, plus 4 `classify` tests covering the unsupported-filesystem branch in both `allow_unlocked` settings `` +- Line 50: replace the crash-simulation-idiom paragraph with: `` - ✅ Crash simulation uses a real child process that `abort()`s (`run_crashing_child`), replacing the former `mem::forget` idiom. `mem::forget` leaks the `File`, so the kernel lock stays held for the life of the test process; only real process death releases it, which is also what production does. `` +- Lines 509 and 510: replace the two `FileLock` bullets with: `` - ✅ **Same-process exclusion** (#304): a second open while a handle is live is refused, enforced by the kernel — `flock`/OFD locks attach to the open file description, not the process `` and `` - ✅ **Holder death across PID namespaces** (#317): `tests/pid_namespace_test.rs` runs two processes at PID 1 in separate namespaces via `unshare`; the survivor opens after the holder is killed, and two live holders are still refused `` +- Lines 557 and 705: replace `mem::forget` crash-simulation references with `real subprocess crash (`run_crashing_child`)`. +- Line 8: update the unit-test count after running the suite. + +- [ ] **Step 4: Update the wiki** + +In `.wiki/Architecture.md`, replace the File Locking paragraph at line 205: + +```markdown +A `.graph` file is guarded by a kernel file lock taken on the file itself +(`std::fs::File::try_lock`: `flock` on Unix, `LockFileEx` on Windows). The lock +is released by the kernel whenever the holding process exits, however it exits, +so a crashed holder never leaves the database unopenable. Because `flock` and +OFD locks attach to the open file description rather than the process, a second +open within one process is refused too. + +There is no lock file. A `.graph.lock` sidecar left behind by versions before +1.3 is ignored and never deleted, since a still-running old process may depend +on it. Running mixed versions against one file is not supported. + +On a filesystem that cannot lock at all, `open()` fails rather than proceeding +unprotected. `OpenOptions::allow_unlocked(true)` overrides this, and accepts the +corruption risk that comes with it. + +On Windows these locks are mandatory rather than advisory, so other processes +cannot read an open `.graph` file. +``` + +And at line 231, replace `enforced by the sidecar lock (see File Locking above)` with `enforced by the kernel file lock (see File Locking above)`. + +- [ ] **Step 5: Fix the stale test count** + +Run `cargo test 2>&1 | grep "^test result:"` and total the counts. Update the "Test Coverage" line in `CLAUDE.md` and line 8 of `docs/TEST_COVERAGE.md` to the real figure. Both currently claim "998 tests passing (990 passing, 8 ignored)", which was already stale before this work — the pre-change count was 999 passing plus 8 ignored. + +- [ ] **Step 6: Verify docs build** + +Run: `cargo test --doc && cargo doc --no-deps` +Expected: PASS, no broken intra-doc links. + +- [ ] **Step 7: Commit** + +```bash +git add CHANGELOG.md docs/ERROR_REFERENCE.md docs/TEST_COVERAGE.md CLAUDE.md +git commit -m "docs: record the kernel locking change + +CHANGELOG carries the semver-relevant facts (new public option, MSRV +1.85 -> 1.89, behaviour change on unlockable filesystems, Windows +mandatory locks) so the release decision can be made later. + +Adds STG-025 through STG-027; the lock errors had no reference entries." +``` + +The `.wiki/` directory is a separate git repository AND it does not exist in this worktree — it is a plain clone that lives only in the main checkout. Make the wiki edit there, and commit it in that repository: + +```bash +cd /home/aditya/Work/AMC/Minigraf/minigraf/.wiki +# edit Architecture.md as described above +git add -A && git commit -m "docs: kernel file locking replaces the PID sidecar" +``` + +Do NOT push. Pushing the wiki is a shared-branch side effect for the human to authorise. + +--- + +### Task 7: Verify the MSRV against the binding repositories + +The MSRV bump is the one change that can break downstream without any test here failing. Seven binding repositories consume this crate. + +**Files:** +- No source changes. Produces a recorded result for the PR description. + +**Interfaces:** +- Consumes: the `rust-version` from Task 1. +- Produces: a finding to include in the PR body. + +- [ ] **Step 1: Check each repository's declared toolchain** + +```bash +for repo in minigraf-python minigraf-node minigraf-wasm minigraf-java minigraf-android minigraf-swift minigraf-c; do + echo "=== $repo ===" + gh api "repos/project-minigraf/$repo/contents/rust-toolchain.toml" --jq '.content' 2>/dev/null | base64 -d 2>/dev/null | grep -i channel || echo " no rust-toolchain.toml" + gh api "repos/project-minigraf/$repo/contents/Cargo.toml" --jq '.content' 2>/dev/null | base64 -d 2>/dev/null | grep -i "rust-version" || echo " no rust-version declared" +done +``` + +If a repository name differs, list the organisation's repositories first with `gh repo list project-minigraf --limit 50`. + +- [ ] **Step 2: Record the result** + +Any repository pinning a toolchain below 1.89 needs its pin raised in the cascade release that follows. Note each one by name in the PR description. Do not change those repositories in this PR — the cascade is a separate process. + +- [ ] **Step 3: Run the full suite once more** + +Run: `cargo test --no-fail-fast && cargo clippy --all-features -- -D warnings && cargo fmt --check` +Expected: PASS on all three. + +Use `--all-features`, NOT `--all-targets`. `--all-features` is exactly what `.github/workflows/rust-clippy.yml` runs, and `main` passes it cleanly. `--all-targets` additionally lints the test binaries, which carry substantial pre-existing lint debt on `main` (approximate `PI` constant, indexing-may-panic, and several hundred more across seven test binaries) that CI has never gated on. Gating this branch on that debt would block it for reasons unrelated to the change. + +- [ ] **Step 4: Check the binary size gate** + +```bash +cargo build --release && echo "$(( $(stat -c %s target/release/minigraf) / 1024 )) KiB of 1024 KiB limit" +``` + +Expected: no growth versus the pre-change baseline; std locking adds no dependency. Local builds run a few KiB above CI's, so compare the delta rather than the absolute number — CI's baseline was 1006 KiB. + +--- + +## Self-Review + +**Spec coverage.** Every section of the spec maps to a task: locking and `classify` to Tasks 1-2, same-process diagnostics to Task 2, `allow_unlocked` to Task 3, errors to Tasks 2 and 6, platform differences and compatibility to Task 6, in-process testing to Task 4, tier 1 to Task 5, and the MSRV risk to Task 7. Tiers 2 and 3 are deliberately excluded and tracked in #324. + +**Type consistency.** `classify` and `LockOutcome` are defined in Task 1 and used unchanged in Task 2. `open_with` is defined in Task 2 and called in Task 3. `run_crashing_child` has one signature, used in both test files. `PathGuard` is defined and consumed within Task 2. + +**Verified against std.** `TryLockError` has exactly the variants `Error(io::Error)` and `WouldBlock`, and `try_lock(&self) -> Result<(), TryLockError>`, both checked against the std source. The `PathGuard::drop` let-chain and `LazyLock` were compile-checked under edition 2024. diff --git a/docs/superpowers/specs/2026-08-22-kernel-file-locking-design.md b/docs/superpowers/specs/2026-08-22-kernel-file-locking-design.md new file mode 100644 index 0000000000..f50207d7df --- /dev/null +++ b/docs/superpowers/specs/2026-08-22-kernel-file-locking-design.md @@ -0,0 +1,252 @@ +# Kernel File Locking — Design + +**Date:** 2026-08-22 +**Status:** Approved, not yet implemented +**Issues:** #317 (bricked `.graph` file after pod restart), #304 (two handles, one file, one process) +**Supersedes:** PR #318 (closed unmerged — mechanism unsound across PID namespaces) + +## Problem + +`FileLock` (`src/storage/backend/file.rs`) implements advisory locking with a PID +sidecar: `acquire` writes `std::process::id()` into `.graph.lock`, created with +`create_new`, and deletes it on `Drop`. + +Two defects follow from using a PID as the liveness token. + +**#317.** A container killed with SIGKILL never runs `Drop`, so the sidecar survives +holding the value `1`. The replacement container is also PID 1 in its own namespace. +`acquire` reads `1`, compares it against `our_pid`, takes the `pid == our_pid` branch, +and refuses. The refusal is permanent: nothing in the code path can ever clear it. The +database is unopenable until an operator deletes the file by hand. + +Reproduced in ~3 seconds with `unshare --user --map-root-user --pid --fork --mount-proc`, +no container runtime required. Container A opens and is SIGKILLed; the sidecar is left +holding `1`; container B is refused with the #304 same-process error text. + +**#304.** A second `FileBackend` on one file within one process gives each its own +cached `header.page_count`. The two page tables diverge, producing intermittent +`Page N out of bounds (total pages: M)`. Whichever handle drops first deletes the lock +file belonging to the survivor. The current code addresses this by refusing when +`pid == our_pid` — which is exactly what makes #317 unrecoverable. + +## Root cause + +A PID is not a liveness token. It is not unique across PID namespaces, and it is +recycled within one. Any scheme that infers "the holder is alive" from a PID is +unsound wherever namespaces exist, which today means anywhere containers run. + +PR #318 proposed comparing `/proc//stat` start times. This was rejected: across +PID namespaces `/proc/` does not refer to the holder at all, so a differing start +time proves only "the holder is not the process I can see", never "the holder is dead". +It converts a safe refusal into silent cross-process corruption. See the review on #318. + +## Decisions + +| # | Decision | +|---|---| +| 1 | Use `std::fs::File::try_lock` — the kernel is the source of truth | +| 2 | Fail closed when locking is unsupported, with an `allow_unlocked` opt-in | +| 3 | Delete the sidecar; lock the `.graph` fd directly; ignore leftovers | +| 4 | Declare `rust-version = "1.89"` | +| 5 | Simulate crashes with real subprocesses; ship no test hook | +| 6 | Merge to main with no version bump; CHANGELOG records the semver facts | + +### Why std rather than a crate or hand-rolled FFI + +`File::try_lock`/`unlock` stabilised in Rust 1.89 (`#[stable(feature = "file_lock", +since = "1.89.0")]`). It uses `flock` on Unix and `LockFileEx` on Windows, both +maintained by the Rust project. + +Measured, not assumed: + +- Both `flock` and `F_OFD_SETLK` deny a same-process second lock (`EWOULDBLOCK`), + because they attach to the open file description rather than the process. Verified + directly. **The kernel lock therefore fixes #304 as well**, with no bookkeeping. +- CI's binary-size gate stands at 1006 KiB against a 1024 KiB limit — **18 KiB of + headroom**. `fs4` would add `rustix`, `bitflags` and `linux-raw-sys` on Linux plus + `windows-sys` on Windows, for an unmeasured delta against that margin. std adds zero. +- `fs4`'s own documentation describes its methods as mirroring the std ones. + +SQLite hand-rolls all of its locking across ~8,000 lines of `os_unix.c` with five +selectable styles, but for reasons that do not apply here: it needs byte-range +granularity to encode SHARED/RESERVED/PENDING/EXCLUSIVE at distinct offsets, which +`flock` cannot express. Its `unixInodeInfo` table exists solely to paper over POSIX +`fcntl` locks being per-process. We have one whole-file exclusive lock and a primitive +that is already per-description, so neither complication applies. + +`edition = "2024"` already implies Rust 1.85, so the MSRV moves four releases, not from +an ancient baseline. + +## Design + +### Locking + +`FileLock`, `is_process_alive`, the sidecar, and the unbounded recursion in `acquire` +are all deleted. `FileBackend` already holds an open `File` on the `.graph` file; the +lock moves onto that fd. No `Drop` impl is needed — closing the file releases the lock, +and so does process death. That is the entirety of the #317 fix. + +`FileBackend::open` classifies `file.try_lock()`: + +| Result | Meaning | Action | +|---|---|---| +| `Ok(())` | lock held | proceed | +| `Err(WouldBlock)` | another open file description holds it | refuse | +| `Err(Error(e))` | filesystem cannot lock | refuse unless `allow_unlocked` | + +The classification is a pure function so the unsupported-filesystem branch is testable +without an exotic filesystem: + +```rust +enum LockOutcome { Acquired, Held, Unsupported(std::io::Error), ProceedUnlocked } + +fn classify(r: Result<(), TryLockError>, allow_unlocked: bool) -> LockOutcome +``` + +### Same-process diagnostics + +`WouldBlock` does not identify the holder, but the #304 message is worth preserving — +the code comment records that generic "another process" wording sent that investigation +looking for a second process for a long time. + +A process-global `Mutex>` of canonicalised open paths, inserted on +success and removed on drop, selects between the same-process and other-process +message. This is **purely diagnostic**; correctness comes from the kernel. If +`canonicalize` fails, fall back to the generic message. + +### `allow_unlocked` + +`OpenOptions` exposes both public fields and chainable builder methods (see +`page_cache_size`), so this needs both, to match the existing pattern: + +```rust +pub allow_unlocked: bool, // default: false +pub fn allow_unlocked(self, allow: bool) -> Self; // builder +``` + +Consulted **only** when locking is unsupported. It never overrides a live `WouldBlock` +— doing so would reintroduce the corruption the lock exists to prevent. Enforce this in +`classify`, not by convention. + +### Errors + +Three distinct messages, all new to `docs/ERROR_REFERENCE.md`, which currently +documents none: + +1. **Same-process** — retains the #304 explanation and the advice to clone the handle. +2. **Another process** — names the path; no PID, since the kernel does not tell us one + and a guess is what caused #317. +3. **Unsupported filesystem** — names the underlying errno and points at + `allow_unlocked`, stating the risk it accepts. + +### Platform differences + +**Windows locks are mandatory**, not advisory. While a database is open, other +processes' reads of the `.graph` file will fail — so copying or backing up an open +database stops working on Windows, where today it succeeds. On Unix the lock stays +advisory and nothing changes. Documented rather than worked around; it also prevents +torn backups. + +**NFS.** Linux emulates `flock` via `fcntl` since 2.6.37, which works on NFSv4 but +needs `lockd` on NFSv3. The failure mode that matters is a lock that silently no-ops +rather than erroring. Tier 2 testing is designed specifically to detect this — but it is deferred to +issue #324 and is NOT delivered by this branch, so the risk is unmitigated on +merge. See the delivery note above. + +### Compatibility + +A leftover `.graph.lock` from v1.2.x is **ignored, not deleted** — deleting it would +break mutual exclusion for a still-running old process. Concurrent access by mixed +versions is unsupported and documented as such: upgrade all writers together. + +> **Delivery note.** Tiers 2 and 3 below are design, not delivery. This branch +> ships the core fix and Tier 1 only; the NFS and Docker nightly suites are +> tracked in issue #324. That leaves one risk in this document's own register +> unmitigated on merge: if `flock` silently no-ops on some network filesystem, +> two processes both receive `Ok(())` and both write. Tier 2 exists precisely +> to detect that, and it is not built yet. The trade is still favourable — the +> sidecar it replaces could not evaluate a remote holder at all, and bricked +> the database on any crash — but it should be stated plainly rather than left +> implicit in a linked issue. + +## Non-goals + +- Shared/read locks. One exclusive lock; the multi-reader model is not in scope. +- Byte-range locking. Nothing here needs sub-file granularity. +- Making `allow_unlocked` bypass a live lock. +- Kubernetes-specific code paths. + +## Testing + +### In-process + +`run_crashing_child()` re-execs the test binary via `current_exe()` with an env marker; +the child opens, writes, and `abort()`s. The kernel releases the lock on death, exactly +as in production. Each of the 14 crash-simulation call sites (13 in `tests/wal_test.rs`, 1 in +`tests/migration_matrix_test.rs`) becomes a one-line call, and `simulate_crashed_holder` +(`tests/wal_test.rs:840`) is deleted. + +No test hook ships in the crate. `#[doc(hidden)] pub` would be callable in production, +and `#[cfg(test)]` is invisible to `tests/*.rs` — integration tests link against the +crate built without `cfg(test)`, verified directly. A real subprocess needs neither. + +### Tier 1 — PID namespaces, every PR + +`tests/pid_namespace_test.rs`, `#[cfg(target_os = "linux")]`, runtime-probing for a +working `unshare` and skipping cleanly when absent. Ubuntu 24.04's +`kernel.apparmor_restrict_unprivileged_userns` may block the unprivileged form; fall +back to `sudo unshare`, which needs no user namespace. + +- **#317 regression** — PID-1 child holds, is SIGKILLed, a second PID-1 process opens + successfully. Fails against today's code, so its catching power is established rather + than assumed. Carries `Co-Authored-By: Olive Casazza `. +- **Mutual exclusion holds** — two live PID-1 processes in separate namespaces, the + second is refused. Guards precisely what PR #318 would have broken. + +### Tier 2 — NFS, nightly — NOT DELIVERED BY THIS BRANCH, see issue #324 + +`nfs-kernel-server` on the runner, loopback export, lock suite over NFSv4 and +NFSv3-without-`lockd`. Asserts a lock is either genuinely exclusive or refused, never a +silent no-op. This is the surface no other tier reaches and the likeliest place for a +real bug in the new code. + +### Tier 3 — Docker, nightly — NOT DELIVERED BY THIS BRANCH, see issue #324 + +Two containers sharing a volume. Mechanically the same path as tier 1, but matches the +reporter's setup literally and yields an artifact worth posting on #317. + +Kubernetes (`kind`/`k3s`) was considered and rejected: pod lifecycle reduces to SIGKILL +plus a fresh PID-1 process, which tier 1 already covers, and `kind` provisions +`hostPath` rather than NFS, so it would not exercise the RWX semantics that motivate it. + +## Files touched + +| File | Change | +|---|---| +| `src/storage/backend/file.rs` | Delete `FileLock`/`is_process_alive`; lock the fd; `classify`; rewrite 3 unit tests | +| `src/db.rs` | `OpenOptions::allow_unlocked` field + docs | +| `Cargo.toml` | `rust-version = "1.89"` | +| `tests/wal_test.rs` | `run_crashing_child`; convert 13 sites; delete `simulate_crashed_holder` | +| `tests/migration_matrix_test.rs` | Convert 1 site | +| `tests/pid_namespace_test.rs` | New — tier 1 | +| `.github/workflows/nfs-lock.yml` | New — tiers 2 and 3. **Deferred to issue #324; not created by this branch.** | +| `CHANGELOG.md` | Unreleased entry recording new API, MSRV bump, behaviour change | +| `docs/ERROR_REFERENCE.md` | Three new lock errors | +| `docs/TEST_COVERAGE.md` | Lines 50, 557, 705 describe the removed idiom | +| `.wiki/Architecture.md` | Lines 205, 231 describe the sidecar lock | + +## Risks + +| Risk | Mitigation | +|---|---| +| `flock` silently no-ops on some network filesystem | **UNMITIGATED ON MERGE.** Tier 2 is designed for this but is deferred to #324. Fail-closed on error still applies, but a silent no-op returns `Ok(())` and so is not an error we can catch. | +| GitHub runners block unprivileged `unshare` | `sudo unshare` fallback; test skips rather than fails | +| MSRV 1.89 breaks a binding repo | Verify all 7 build on 1.89 before merge | +| Windows mandatory locks break someone's backup flow | Documented as a platform difference in CHANGELOG and wiki | +| Mixed v1.2.x/new writers on one file | Documented unsupported; leftover sidecar left untouched | + +## Attribution + +Reported and diagnosed by @ocasazza (#317), including the reproduction and the correct +identification of #304 as the reason for the `pid == our_pid` refusal. PR #318 is not +merged, but one of its tests carries over as the tier 1 regression case. diff --git a/examples/pid_ns_helper.rs b/examples/pid_ns_helper.rs new file mode 100644 index 0000000000..6fc018e910 --- /dev/null +++ b/examples/pid_ns_helper.rs @@ -0,0 +1,83 @@ +//! Helper for `tests/pid_namespace_test.rs`. Opens a database and either dies +//! holding the lock, or reports whether the open succeeded. +//! +//! An example rather than a test so it is a standalone binary the test can run +//! under `unshare`. +//! +//! # Why the release marker is an argument, not an environment variable +//! +//! It was an environment variable first, and that silently did nothing on CI. +//! GitHub's runners restrict unprivileged user namespaces, so the test falls +//! back to `sudo unshare ...`, and sudo defaults to `env_reset`: everything +//! outside a small allowlist is stripped before the command runs. The holder +//! therefore saw no marker, took the "nothing to wait for" path, and aborted +//! the instant it had the lock -- so the second container found the file free +//! and opened it, and the test reported that a live holder had not been +//! excluded, when in truth there had been no live holder to exclude. +//! Arguments cross that boundary; environment variables do not. +//! +//! # Why this kills itself +//! +//! The obvious design is for the test to spawn a holder and kill it. That does +//! not survive contact with CI. GitHub's runners restrict unprivileged user +//! namespaces, so the test falls back to `sudo unshare ...`, and Ubuntu's sudo +//! defaults to `use_pty`, which runs the command in a NEW SESSION. Signalling +//! the process we spawned then reaches only `sudo` itself — SIGKILL runs no +//! handler, so nothing is forwarded — and killing the process group misses the +//! holder too, because it is no longer in that group. The holder survives, +//! still holding the flock, and the #317 regression test fails identically +//! whether or not the bug is present. +//! +//! So the holder ends its own life instead. `abort()` is the right instrument: +//! it terminates without unwinding and without running any `Drop`, which is +//! precisely what a SIGKILLed container leaves behind — no checkpoint, no lock +//! release by the library, nothing but whatever the kernel does on its own. +//! That is the exact state #317 is about. + +fn main() { + let args: Vec = std::env::args().collect(); + let path = &args[1]; + let mode = &args[2]; + // Optional: a path this process waits for before dying. An argument rather + // than an env var so it survives `sudo` -- see the note above. + let release_marker = args.get(3).map(std::path::PathBuf::from); + + match minigraf::Minigraf::open(path) { + Ok(db) => { + db.execute(r#"(transact [[:a :name "A"]])"#).ok(); + println!("OPEN_OK"); + + if mode == "hold" { + // Hold, then die WITHOUT running Drop. + // + // If the test names a release marker, wait for it to appear. + // A fixed sleep cannot work here: the test must spawn a second + // container through `sudo unshare` and let it attempt an open, + // and how long that takes varies wildly between a laptop and a + // loaded CI runner. A fixed hold that is comfortable locally + // expires mid-attempt on a slow runner, the second container + // then succeeds, and the test fails claiming a live holder was + // not excluded. Letting the parent say when it is done removes + // the guesswork entirely. + // + // The deadline is a backstop, not the mechanism: if the parent + // dies without writing the marker, this process must not sit on + // the lock indefinitely. + if let Some(marker) = release_marker { + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60); + while !marker.exists() && std::time::Instant::now() < deadline { + std::thread::sleep(std::time::Duration::from_millis(20)); + } + } + // Flush stdout first: abort() will not. + use std::io::Write; + let _ = std::io::stdout().flush(); + std::process::abort(); + } + } + Err(e) => { + println!("OPEN_ERR {e}"); + std::process::exit(3); + } + } +} diff --git a/src/db.rs b/src/db.rs index 0982b2e4a1..554f9c4506 100644 --- a/src/db.rs +++ b/src/db.rs @@ -73,6 +73,16 @@ pub struct OpenOptions { pub max_derived_facts: usize, /// Maximum total query results. Defaults to 1_000_000. pub max_results: usize, + /// Open even when the filesystem cannot lock files at all. + /// + /// Defaults to `false`, which refuses to open rather than run without the + /// protection the lock provides. Set this only when you know the database + /// has a single writer by other means — for example an NFSv3 mount with no + /// `lockd`, where locking is unavailable rather than merely contended. + /// + /// This does **not** override a lock that another handle actually holds. + /// Opening a database twice is refused regardless of this setting. + pub allow_unlocked: bool, } impl Default for OpenOptions { @@ -82,6 +92,7 @@ impl Default for OpenOptions { page_cache_size: 256, max_derived_facts: DEFAULT_MAX_DERIVED_FACTS, max_results: DEFAULT_MAX_RESULTS, + allow_unlocked: false, } } } @@ -118,6 +129,14 @@ impl OpenOptions { self } + /// Open even when the filesystem cannot lock files. See + /// [`OpenOptions::allow_unlocked`] for when this is appropriate. + #[must_use] + pub fn allow_unlocked(mut self, allow: bool) -> Self { + self.allow_unlocked = allow; + self + } + /// Set the path for a file-backed database. #[cfg(not(target_arch = "wasm32"))] pub fn path(self, path: impl AsRef) -> OpenOptionsWithPath { @@ -290,7 +309,7 @@ impl Minigraf { let db_path = path.as_ref().to_path_buf(); // Open the main .graph file - let backend = FileBackend::open(&db_path)?; + let backend = FileBackend::open_with(&db_path, opts.allow_unlocked)?; let pfs = PersistentFactStorage::new(backend, opts.page_cache_size)?; // Share the fact storage @@ -607,13 +626,13 @@ impl Minigraf { // replayed WAL entries on open (crash-recovery path). `pfs.is_dirty()` // catches any facts marked dirty via the normal write path. // - // File locking (`.graph.lock` sidecar, acquired by FileBackend::open) - // already prevents a second *process* from opening the file while this - // handle holds the lock, which covers the main multi-process exposure - // described in issue #226. This guard closes the remaining edge cases: - // same-process double-opens (same PID bypasses the stale-lock check) - // and environments where the advisory lock can be bypassed (e.g. - // network filesystems, manual lock deletion). + // The kernel file lock taken by FileBackend::open covers both a + // second process and a second handle in this one, since flock + // and OFD locks attach to the open file description. This guard + // remains for environments where the filesystem cannot lock at all — + // for example, NFSv3 without lockd or some FUSE mounts — where the + // caller set `allow_unlocked` to accept that Minigraf cannot detect + // concurrent writers. if *wal_entry_count == 0 && !pfs.is_dirty() { return Ok(()); } @@ -1539,6 +1558,7 @@ mod tests { page_cache_size: 256, max_derived_facts: 100_000, max_results: 1_000_000, + allow_unlocked: false, }; let db = Minigraf::open_with_options(&path, opts).unwrap(); assert_eq!(db.inner.options.wal_checkpoint_threshold, 5); @@ -1900,6 +1920,32 @@ mod tests { "file size must not change after read-only handle drop" ); } + + #[test] + fn test_allow_unlocked_defaults_to_false() { + assert!(!OpenOptions::default().allow_unlocked); + } + + #[test] + fn test_allow_unlocked_builder_sets_the_field() { + assert!(OpenOptions::default().allow_unlocked(true).allow_unlocked); + } + + /// `allow_unlocked` must not open a door past a live lock. It applies only + /// where the filesystem cannot lock at all. + #[test] + fn test_allow_unlocked_does_not_bypass_a_live_lock() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("allow_unlocked.graph"); + + let first = Minigraf::open(&path).unwrap(); + let opts = OpenOptions::default().allow_unlocked(true); + assert!( + Minigraf::open_with_options(&path, opts).is_err(), + "allow_unlocked must not bypass a lock that is actually held" + ); + drop(first); + } } // ─── WASI smoke test ───────────────────────────────────────────────────────── diff --git a/src/storage/backend/file.rs b/src/storage/backend/file.rs index 08a0882574..1a35b5187e 100644 --- a/src/storage/backend/file.rs +++ b/src/storage/backend/file.rs @@ -5,114 +5,103 @@ use std::fs::{File, OpenOptions}; use std::io::{Read, Seek, SeekFrom, Write}; use std::path::{Path, PathBuf}; -/// Advisory file lock to prevent multi-process corruption. +/// What `FileBackend::open` should do with the result of `File::try_lock`. /// -/// Uses a sidecar `.lock` file with exclusive creation semantics. -/// The lock is released (file deleted) on drop. -struct FileLock { - path: PathBuf, +/// Split out from the I/O so the unsupported-filesystem branch is testable +/// without an exotic filesystem: no CI runner can be relied on to provide a +/// mount whose locks fail. +enum LockOutcome { + /// We hold the lock. + Acquired, + /// Another open file description holds it. May be in this process. + Held, + /// The filesystem cannot lock, and the caller did not opt in to running + /// without one. + Unsupported(std::io::Error), + /// The filesystem cannot lock and the caller accepted the risk. + ProceedUnlocked, } -impl FileLock { - /// Attempt to acquire an exclusive lock for the given database path. - /// Returns `Err` if another process already holds the lock. - /// - /// If a stale lock file exists (the holder is a *different* process that is - /// no longer running), it is automatically removed and a new lock is - /// acquired. This handles the case where the previous process crashed - /// without cleaning up. - /// - /// A lock held by our OWN pid is never treated as stale. It used to be, on - /// the theory that it could only be a leaked handle — but the overwhelmingly - /// more likely explanation is a handle that is open and in use right now, - /// somewhere else in this process. Stealing it produced two live - /// `FileBackend`s on one file, each caching its own `header.page_count`, - /// each allocating new pages from that stale count and bounds-checking - /// `read_page` against it. That is the source of the intermittent - /// "Page N out of bounds (total pages: M)" in #304: not an allocation - /// off-by-one, but two page tables diverging. It also corrupted the lock - /// itself — whichever handle dropped first ran `FileLock::drop` and deleted - /// the lock file belonging to the survivor, leaving a live handle unlocked - /// and letting other processes in too. - fn acquire(db_path: &Path) -> Result { - let lock_path = db_path.with_extension("graph.lock"); - // create_new fails with AlreadyExists if the lock file is present - match OpenOptions::new() - .write(true) - .create_new(true) - .open(&lock_path) - { - Ok(mut f) => { - // Write PID for diagnostics (best-effort) - let _ = write!(f, "{}", std::process::id()); - Ok(FileLock { path: lock_path }) - } - Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { - // Check if the holder process is still alive - let holder = std::fs::read_to_string(&lock_path).unwrap_or_default(); - if let Ok(pid) = holder.trim().parse::() { - let our_pid = std::process::id(); - if pid == our_pid { - // A handle in THIS process holds the lock. Say so - // explicitly: the generic "another process" wording sent - // #304 looking for a second process for a long time. - anyhow::bail!( - "Database is already open in this process (lock file: {}, \ - holder PID: {} == our own). A second handle on one file \ - would give each its own page table and corrupt both — \ - reuse the existing handle instead. `Minigraf` is cheap to \ - clone and all clones share the same database.", - lock_path.display(), - pid - ); - } - if !Self::is_process_alive(pid) { - // Stale lock — the holding process died without cleaning - // up. Remove and retry. - let _ = std::fs::remove_file(&lock_path); - return Self::acquire(db_path); - } - } - anyhow::bail!( - "Database is locked by another process (lock file: {}, holder PID: {}). \ - If no other process is using this database, delete the lock file manually.", - lock_path.display(), - holder.trim() - ); - } - Err(e) => { - anyhow::bail!( - "Failed to acquire database lock at {}: {}", - lock_path.display(), - e - ); - } - } +/// Decide what a `try_lock` result means. +/// +/// `allow_unlocked` is consulted ONLY for `TryLockError::Error`, never for +/// `WouldBlock`. Letting it bypass a live lock would reintroduce the +/// two-writers-one-file corruption the lock exists to prevent. +fn classify(result: Result<(), std::fs::TryLockError>, allow_unlocked: bool) -> LockOutcome { + match result { + Ok(()) => LockOutcome::Acquired, + Err(std::fs::TryLockError::WouldBlock) => LockOutcome::Held, + Err(std::fs::TryLockError::Error(_)) if allow_unlocked => LockOutcome::ProceedUnlocked, + Err(std::fs::TryLockError::Error(e)) => LockOutcome::Unsupported(e), } +} - /// Check if a process with the given PID is still running. - fn is_process_alive(pid: u32) -> bool { - // On Linux/Android, /proc/ exists iff the process is alive. - let proc_path = format!("/proc/{}", pid); - if std::path::Path::new(&proc_path).exists() { - return true; - } - // On Linux, if /proc exists but /proc/ doesn't, the process is dead. - if std::path::Path::new("/proc").exists() { - return false; +/// Canonicalised paths this process currently has open. +/// +/// Correctness comes from the kernel lock, not this registry -- but this is +/// no longer *purely* diagnostic. Two things read it: which error message to +/// print (#304 was misdiagnosed for a long time when the generic "another +/// process" wording sent the investigation looking for a second process that +/// did not exist), and whether `open_with` retries a `WouldBlock` at all. A +/// same-process conflict is unwinnable by waiting, so that decision skips the +/// retry loop entirely; only this registry can tell the two cases apart. If +/// `already_open_here` ever wrongly returned `true`, `open_with` would both +/// report the wrong error *and* skip the retry that would have let it +/// succeed. +static OPEN_PATHS: std::sync::LazyLock>> = + std::sync::LazyLock::new(|| std::sync::Mutex::new(std::collections::HashSet::new())); + +/// Removes this backend's path from `OPEN_PATHS` when the backend drops. +struct PathGuard(Option); + +impl Drop for PathGuard { + fn drop(&mut self) { + if let Some(path) = self.0.take() + && let Ok(mut open) = OPEN_PATHS.lock() + { + open.remove(&path); } - // On non-procfs systems (macOS, Windows), assume alive (conservative). - // Users must manually delete stale lock files on these platforms. - true } } -impl Drop for FileLock { - fn drop(&mut self) { - let _ = std::fs::remove_file(&self.path); - } +/// True if this process already has `path` open. +/// +/// Not diagnostic-only: besides choosing which error message to print, this +/// also gates whether `open_with` retries a `WouldBlock` at all (see +/// `OPEN_PATHS` above). +fn already_open_here(path: &Path) -> bool { + OPEN_PATHS + .lock() + .map(|open| open.contains(path)) + .unwrap_or(false) } +/// How many times to retry a cross-process `WouldBlock` before concluding +/// another process genuinely holds the lock. +/// +/// `Command::spawn` (in this process or any other) forks before exec'ing the +/// child; until the child reaches `execve` and its close-on-exec descriptors +/// close, it transiently holds a duplicate of every flock its parent has +/// open. A concurrent `try_lock()` elsewhere -- another thread in this +/// process, a sibling process, or bindings that shell out (Python/Node +/// commonly do) -- can observe `WouldBlock` during that few-microsecond +/// window even though no other process is really contending for the file. +/// SQLite has the same class of problem and solves it with `busy_timeout`; +/// this is our version, bounded so a database that is genuinely locked by +/// another process still fails -- after paying the full retry budget below, +/// not instantly -- rather than `open` hanging indefinitely. +const LOCK_RETRY_ATTEMPTS: u32 = 10; + +/// Initial backoff between retries, doubled each attempt up to +/// `LOCK_RETRY_MAX_DELAY`. +const LOCK_RETRY_INITIAL_DELAY: std::time::Duration = std::time::Duration::from_millis(5); + +/// Backoff cap. With `LOCK_RETRY_ATTEMPTS` retries doubling from +/// `LOCK_RETRY_INITIAL_DELAY` and capping here, the worst case is +/// 5+10+20+40+50+50+50+50+50+50 = 375ms of total sleep -- comfortably longer +/// than a fork-to-`execve` window, comfortably shorter than a human notices. +const LOCK_RETRY_MAX_DELAY: std::time::Duration = std::time::Duration::from_millis(50); + /// File-based storage backend for native platforms. /// /// Stores graph data in a single `.graph` file with a page-based structure: @@ -127,10 +116,17 @@ impl Drop for FileLock { pub struct FileBackend { #[allow(dead_code)] path: PathBuf, + // `_path_guard` must drop before `file`: dropping `file` releases the + // kernel lock, and if the registry entry were still present after that, + // another thread in this process could win the lock and insert its own + // entry between the two drops -- which this guard would then delete, + // leaving the registry claiming "not open" while a live handle exists. + // Rust drops struct fields in declaration order, so this field must be + // declared before `file`. + _path_guard: PathGuard, file: File, header: FileHeader, is_new: bool, - _lock: FileLock, } impl FileBackend { @@ -139,14 +135,30 @@ impl FileBackend { /// If the file doesn't exist, creates it with an initial header. /// If it exists, validates and loads the header. /// - /// Acquires an advisory file lock (sidecar `.graph.lock` file) to prevent - /// multi-process corruption. Returns an error if the database is already - /// opened by another process. + /// Takes a kernel file lock on the `.graph` file itself, which prevents + /// both multi-process corruption and a second handle within this process. + /// The kernel releases the lock when the process exits, however it exits, + /// so a crashed holder never leaves the database unopenable (#317). + /// + /// A `WouldBlock` from a possible cross-process conflict is retried with + /// bounded backoff (see `LOCK_RETRY_ATTEMPTS`) before being treated as a + /// real conflict, to ride out the transient window where a forked but + /// not-yet-exec'd subprocess holds a duplicate of someone else's lock. + /// + /// Production code calls `open_with` directly. This wrapper survives as a + /// convenience for the ~40 in-crate test call sites. + #[cfg(test)] pub fn open>(path: P) -> Result { - let path = path.as_ref().to_path_buf(); + Self::open_with(path, false) + } - // Acquire advisory lock before touching the database file. - let lock = FileLock::acquire(&path)?; + /// As [`FileBackend::open`], but `allow_unlocked` permits opening on a + /// filesystem that cannot lock at all. + /// + /// `allow_unlocked` does NOT override a lock held by someone else. It + /// applies only when the filesystem rejects locking outright. + pub fn open_with>(path: P, allow_unlocked: bool) -> Result { + let path = path.as_ref().to_path_buf(); let mut file = OpenOptions::new() .read(true) @@ -155,6 +167,104 @@ impl FileBackend { .truncate(false) .open(&path)?; + // Canonicalise only after the file exists. Used for the diagnostic + // registry; if it fails we fall back to the generic message. + let canonical = std::fs::canonicalize(&path).unwrap_or_else(|_| path.clone()); + + let mut lock_result = file.try_lock(); + + // Retry a `WouldBlock` with backoff, but ONLY when the conflict might + // be with another process. If this process already has the path + // open, waiting is pointless: we are the only thing that could ever + // release that lock, and we are standing right here not releasing + // it, so every retry would just burn the budget on a certain + // failure. Skipping this check keeps `test_second_open_in_same_process_is_refused` + // fast instead of paying the full retry budget on every same-process + // rejection. + if matches!(lock_result, Err(std::fs::TryLockError::WouldBlock)) + && !already_open_here(&canonical) + { + let mut delay = LOCK_RETRY_INITIAL_DELAY; + for _ in 0..LOCK_RETRY_ATTEMPTS { + std::thread::sleep(delay); + lock_result = file.try_lock(); + if !matches!(lock_result, Err(std::fs::TryLockError::WouldBlock)) { + break; + } + delay = (delay * 2).min(LOCK_RETRY_MAX_DELAY); + } + } + + let path_guard = match classify(lock_result, allow_unlocked) { + LockOutcome::Acquired => { + if let Ok(mut open) = OPEN_PATHS.lock() { + open.insert(canonical.clone()); + } + PathGuard(Some(canonical)) + } + LockOutcome::Held if already_open_here(&canonical) => { + anyhow::bail!( + "Database is already open in this process ({}). A second handle \ + on one file would give each its own page table and corrupt both \ + — reuse the existing handle instead. `Minigraf` is cheap to clone \ + and all clones share the same database.", + path.display() + ); + } + LockOutcome::Held => { + anyhow::bail!( + "Database is locked by another process ({}). The lock is held on \ + the file itself and is released automatically when the holding \ + process exits, so there is no lock file to clean up.", + path.display() + ); + } + LockOutcome::Unsupported(e) => { + anyhow::bail!( + "Failed to lock database at {}: {}. This filesystem does not \ + support file locking (common on NFSv3 without lockd, and on some \ + FUSE mounts). Set `allow_unlocked` in `OpenOptions` to open \ + anyway — that accepts the risk that concurrent writers corrupt \ + the file.", + path.display(), + e + ); + } + LockOutcome::ProceedUnlocked => { + // There is no kernel lock in this arm, so the registry is not + // merely diagnostic here: it is the only thing standing + // between us and the #304 two-page-tables corruption if a + // second handle in this process opens the same unlockable + // path. + // + // Because it is the only guard, the check and the insert must + // be ONE atomic operation. Reading through `already_open_here` + // and inserting afterwards releases the mutex in between, and + // two threads opening the same path would then both observe + // "absent", both insert, and both proceed. `HashSet::insert` + // returns false when the key was already present, which gives + // us test-and-set under a single lock acquisition. + // + // A poisoned mutex yields `false` here, so it fails CLOSED: + // we refuse to open rather than risk admitting a second + // handle we cannot account for. + let inserted = OPEN_PATHS + .lock() + .map(|mut open| open.insert(canonical.clone())) + .unwrap_or(false); + if !inserted { + anyhow::bail!( + "Database is already open in this process ({}). A second handle \ + on one file would give each its own page table and corrupt both \ + — reuse the existing handle instead. `Minigraf` is cheap to clone \ + and all clones share the same database.", + path.display() + ); + } + PathGuard(Some(canonical)) + } + }; + // Check file size using the open file handle's metadata. // This is more reliable than checking path metadata separately, // as it uses the same file descriptor we'll be reading from. @@ -187,7 +297,7 @@ impl FileBackend { file, header, is_new, - _lock: lock, + _path_guard: path_guard, }) } @@ -307,12 +417,8 @@ mod tests { use super::*; /// #304: a second handle on a file this process already has open must be - /// refused, not granted by deleting our own lock file. - /// - /// Before the fix `acquire` treated `pid == our_pid` as a stale lock, so - /// this second open succeeded and the two backends diverged: each caches - /// its own `header.page_count`, allocates new pages from it, and - /// bounds-checks `read_page` against it. + /// refused. `flock` and OFD locks attach to the open file description, not + /// the process, so the kernel denies this without any bookkeeping from us. #[test] fn test_second_open_in_same_process_is_refused() { let dir = tempfile::tempdir().unwrap(); @@ -328,20 +434,147 @@ mod tests { }; assert!( msg.contains("already open in this process"), - "error should name the same-process case, got: {msg}" + "error should name the same-process case" ); - // The refusal must not have removed the lock the first handle holds. - let lock_path = temp_path.with_extension("graph.lock"); + drop(first); + // Once the only handle is dropped, the kernel releases the lock. + FileBackend::open(&temp_path).unwrap(); + } + + /// The child half of [`test_cross_process_lock_retries_then_fails_within_budget`]: + /// opens `db_path`, holds the lock for `MINIGRAF_HOLD_MILLIS`, signalling + /// readiness by creating `MINIGRAF_HOLD_READY_MARKER` right after + /// acquiring it. A no-op when the marker environment variable is absent + /// -- same convention as `tests/wal_test.rs`'s `crash_child_entrypoint`, + /// which this mirrors for the same reason: `#[cfg(test)]` items are + /// invisible outside this crate, so a real second process is the only + /// way to exercise a genuinely external lock holder. + #[test] + fn hold_lock_entrypoint() { + let Ok(db_path) = std::env::var("MINIGRAF_HOLD_DB") else { + return; + }; + let ready_marker = std::env::var("MINIGRAF_HOLD_READY_MARKER").expect("ready marker"); + let millis: u64 = std::env::var("MINIGRAF_HOLD_MILLIS") + .expect("hold millis") + .parse() + .expect("hold millis is a number"); + + let backend = FileBackend::open(&db_path).expect("child holds lock"); + std::fs::write(&ready_marker, "").expect("write ready marker"); + std::thread::sleep(std::time::Duration::from_millis(millis)); + drop(backend); + // Falling off the end of the test runs Drop normally (unlike the + // WAL crash tests, this one is not simulating a crash -- it is + // simulating an ordinary second process that is simply still open), + // which releases the kernel lock exactly as the retrying parent + // needs to see happen once its budget is spent... except the parent + // stops retrying before that, by design: see the driver test. + } + + /// A genuinely cross-process lock must still refuse `open` -- with the + /// cross-process wording, not the same-process one -- and must do so + /// within the bounded retry budget rather than hang. + /// + /// This is the production-code counterpart to `LOCK_RETRY_ATTEMPTS` + /// and `LOCK_RETRY_MAX_DELAY`: nothing else on this branch spawns a + /// second process to hold the lock while another tries to open it, so + /// nothing else exercises either the cross-process error wording or the + /// bound on retry duration. Point (c) below matters beyond timing: if + /// `already_open_here` ever wrongly returned `true` for a path this + /// process does not actually have open, this is the only test that + /// would catch it -- everything else in this module opens and closes + /// within a single process. + #[test] + fn test_cross_process_lock_retries_then_fails_within_budget() { + let dir = tempfile::tempdir().unwrap(); + let db_path = dir.path().join("test_cross_process_hold.graph"); + let ready_marker = { + let mut p = db_path.clone().into_os_string(); + p.push(".ready"); + PathBuf::from(p) + }; + let _ = std::fs::remove_file(&ready_marker); + + let exe = std::env::current_exe().expect("test binary path"); + let mut holder = std::process::Command::new(&exe) + .args([ + "storage::backend::file::tests::hold_lock_entrypoint", + "--exact", + "--nocapture", + ]) + .env("MINIGRAF_HOLD_DB", &db_path) + .env("MINIGRAF_HOLD_READY_MARKER", &ready_marker) + // Comfortably longer than LOCK_RETRY_ATTEMPTS' ~375ms budget, so + // this process's retries must exhaust while the lock is still + // genuinely held. + // + // 3s rather than something nearer the budget: at 600ms only + // ~215ms of slack separated the two, and if this process were + // descheduled for longer than that anywhere across its ten retry + // iterations, the child would release mid-loop, this open would + // SUCCEED, and the test would fail claiming a cross-process lock + // was not refused. CI runs this on three platforms with 34 test + // binaries in flight, so that margin was not safe. + .env("MINIGRAF_HOLD_MILLIS", "3000") + .spawn() + .expect("spawn lock holder"); + + // Wait for the child to actually hold the lock before racing it -- + // otherwise this test would be racing its own setup, not the retry. + let setup_deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); + while !ready_marker.exists() { + assert!( + std::time::Instant::now() < setup_deadline, + "lock-holding child never signalled readiness" + ); + std::thread::sleep(std::time::Duration::from_millis(5)); + } + + let start = std::time::Instant::now(); + let result = FileBackend::open(&db_path); + let elapsed = start.elapsed(); + + // (a) a real cross-process lock is still refused. + let msg = match result { + Ok(_) => panic!("a genuinely cross-process lock must be refused"), + Err(e) => e.to_string(), + }; + + // (b) the retry budget is bounded: this must fail in roughly the + // ~375ms budget, NOT hang for the holder's full 3s hold. The 2s bound + // is far below the hold, so a regression that waited for the holder + // instead of giving up would fail here. assert!( - lock_path.exists(), - "a refused acquire must leave the existing lock in place" + elapsed < std::time::Duration::from_secs(2), + "retry budget must be bounded, not hang" + ); + + // (c) the error names the cross-process case, not the same-process + // one -- this process never had the path open. + assert!( + msg.contains("locked by another process"), + "must report the cross-process wording" ); - drop(first); assert!( - !lock_path.exists(), - "dropping the only handle should release the lock" + !msg.contains("already open in this process"), + "must not report the same-process wording for a real second process" ); + + // Once the holder exits, the lock -- and the holder's own registry + // entry, which only the holder's process could ever remove -- are + // both gone, so a fresh open succeeds without retrying at all. + // + // Kill rather than waiting out the remaining hold: it keeps the test + // fast, and release-on-SIGKILL is the property that actually matters + // here. A holder that exits cleanly is the easy case; #317 was about + // one that never got to run any cleanup at all. + holder.kill().expect("kill lock holder"); + holder.wait().expect("reap lock holder"); + FileBackend::open(&db_path).expect("open must succeed once the holder exits"); + + let _ = std::fs::remove_file(&ready_marker); } /// The refusal must not regress the sequential open/drop/reopen cycle that @@ -358,36 +591,49 @@ mod tests { assert!(FileBackend::open(&temp_path).is_ok()); } - /// A lock left behind by a *different*, dead process is still stale and - /// must still be self-healed — that behaviour is what the `pid == our_pid` - /// branch was overreaching from, and it must survive the fix. - /// - /// procfs-only: without `/proc`, `is_process_alive` cannot distinguish a - /// dead PID from a live one and deliberately errs towards "alive" (see its - /// own comment), so reclamation is a Linux/Android guarantee only. On other - /// platforms a stale lock must still be removed by hand. - #[cfg(any(target_os = "linux", target_os = "android"))] + /// A leftover sidecar from v1.2.x must not block an open. We neither read + /// nor delete it: a still-running old process may rely on it. #[test] - fn test_dead_other_process_lock_is_still_reclaimed() { + fn test_leftover_sidecar_is_ignored_and_preserved() { let dir = tempfile::tempdir().unwrap(); - let temp_path = dir.path().join("test_minigraf_stale.graph"); - let lock_path = temp_path.with_extension("graph.lock"); + let temp_path = dir.path().join("test_minigraf_leftover.graph"); + let legacy = temp_path.with_extension("graph.lock"); + std::fs::write(&legacy, "1").unwrap(); - // PID 1 is always alive; instead spawn a real child and let it exit so - // we have a genuinely dead PID that is not us. - let child = std::process::Command::new("true").spawn().unwrap(); - let dead_pid = child.id(); - let mut child = child; - child.wait().unwrap(); + let backend = FileBackend::open(&temp_path).unwrap(); + drop(backend); + + assert!( + legacy.exists(), + "a leftover sidecar must be left alone, not deleted" + ); + } + + /// The path registry must not leak entries: a refused open must leave the + /// set exactly as it found it, so a later refusal still reports the + /// same-process case rather than degrading to the generic + /// "locked by another process" wording. + #[test] + fn test_refused_open_does_not_corrupt_path_registry() { + let dir = tempfile::tempdir().unwrap(); + let temp_path = dir.path().join("test_minigraf_registry.graph"); - std::fs::write(&lock_path, dead_pid.to_string()).unwrap(); + let first = FileBackend::open(&temp_path).unwrap(); + assert!(FileBackend::open(&temp_path).is_err()); // first refusal - let backend = FileBackend::open(&temp_path); + // A refused open must not have removed first's registry entry: a + // second refusal must still see it and report the same-process case. + let msg = match FileBackend::open(&temp_path) { + Ok(_) => panic!("must be refused"), + Err(e) => e.to_string(), + }; assert!( - backend.is_ok(), - "a dead other process's lock must still be reclaimed: {:?}", - backend.err() + msg.contains("already open in this process"), + "registry entry must survive a refused open" ); + + drop(first); + FileBackend::open(&temp_path).unwrap(); } #[test] @@ -474,4 +720,42 @@ mod tests { backend.write_page(2, &vec![0u8; PAGE_SIZE]).unwrap(); assert_eq!(backend.page_count().unwrap(), 3); } + + #[test] + fn test_classify_ok_is_acquired() { + assert!(matches!(classify(Ok(()), false), LockOutcome::Acquired)); + assert!(matches!(classify(Ok(()), true), LockOutcome::Acquired)); + } + + #[test] + fn test_classify_would_block_is_held_regardless_of_allow_unlocked() { + // allow_unlocked must NOT override a live lock: doing so reintroduces + // exactly the cross-process corruption the lock exists to prevent. + assert!(matches!( + classify(Err(std::fs::TryLockError::WouldBlock), false), + LockOutcome::Held + )); + assert!(matches!( + classify(Err(std::fs::TryLockError::WouldBlock), true), + LockOutcome::Held + )); + } + + #[test] + fn test_classify_unsupported_fails_closed_by_default() { + let e = std::io::Error::from_raw_os_error(95); // ENOTSUP + assert!(matches!( + classify(Err(std::fs::TryLockError::Error(e)), false), + LockOutcome::Unsupported(_) + )); + } + + #[test] + fn test_classify_unsupported_proceeds_when_allowed() { + let e = std::io::Error::from_raw_os_error(37); // ENOLCK + assert!(matches!( + classify(Err(std::fs::TryLockError::Error(e)), true), + LockOutcome::ProceedUnlocked + )); + } } diff --git a/tests/migration_matrix_test.rs b/tests/migration_matrix_test.rs index 7ecda2c29b..e08a3c207f 100644 --- a/tests/migration_matrix_test.rs +++ b/tests/migration_matrix_test.rs @@ -2,7 +2,7 @@ #![cfg(not(target_arch = "wasm32"))] use minigraf::QueryResult; -use minigraf::db::Minigraf; +use minigraf::db::{Minigraf, OpenOptions}; const PAGE_SIZE: usize = 4096; const MAGIC_NUMBER: [u8; 4] = *b"MGRF"; @@ -14,6 +14,127 @@ fn count_results(r: QueryResult) -> usize { } } +/// Serializes `Command::spawn` calls within this test binary. +/// +/// `Command::spawn` forks the whole (multi-threaded) test process before +/// exec'ing the child. Until that child reaches `execve` and its +/// close-on-exec descriptors are closed, it transiently holds a duplicate of +/// every flock any other thread's test currently has open on a `.graph` +/// file — which can make an unrelated, concurrent `try_lock()` elsewhere in +/// this binary observe `WouldBlock` for a few microseconds and fail with +/// "Database is locked by another process", even though no other process +/// was ever really involved. Capping this binary to one fork in flight at a +/// time keeps that window rare enough not to flake the suite. +/// +/// This is now belt-and-braces: `FileBackend::open_with` (src/storage/backend/file.rs) +/// retries a cross-process `WouldBlock` with bounded backoff, which is the +/// real fix for the false-negative window described above. This mutex is +/// cheap insurance on top of that, not load-bearing on its own. +static CRASH_CHILD_SPAWN_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + +/// Whether the crashing child commits `statements` as one explicit +/// transaction or as separate auto-committed writes. +#[derive(Clone, Copy)] +enum CrashTx { + /// Each statement is its own auto-committed `db.execute()` call — the + /// implicit-transaction path. + Implicit, + /// All statements run inside one `begin_write()` / `commit()` — pins + /// that `WriteTransaction::commit()` itself writes to the WAL before a + /// crash, and that multiple pending facts survive (or are lost) as one + /// atomic unit rather than as independent writes. + #[allow(dead_code)] + Explicit, +} + +/// Runs a child process that opens `db_path`, applies `statements` (per +/// `tx`), then dies by `abort()` without running any `Drop`. +/// +/// This is a real crash, not a simulated one: the child's file descriptors are +/// closed by the kernel, which releases the file lock exactly as it would for a +/// SIGKILLed container. Forgetting the handle in-process cannot model this — it +/// leaks the `File`, so the lock stays held for the life of the test process. +fn run_crashing_child( + db_path: &std::path::Path, + wal_checkpoint_threshold: usize, + statements: &[&str], + tx: CrashTx, +) { + let exe = std::env::current_exe().expect("test binary path"); + let mut command = std::process::Command::new(exe); + command + .args(["crash_child_entrypoint", "--exact", "--nocapture"]) + .env("MINIGRAF_CRASH_DB", db_path) + .env( + "MINIGRAF_CRASH_THRESHOLD", + wal_checkpoint_threshold.to_string(), + ) + .env("MINIGRAF_CRASH_STMTS", statements.join("\u{1e}")); + if matches!(tx, CrashTx::Explicit) { + command.env("MINIGRAF_CRASH_TX", "1"); + } + + let status = { + let _guard = CRASH_CHILD_SPAWN_LOCK + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()); + command.status().expect("spawn crash child") + }; + + // On Unix `abort()` raises SIGABRT and `code()` is None. On Windows it + // terminates with a nonzero exit code instead. The property that matters + // on both is that the child did not exit cleanly. + assert!( + status.code().unwrap_or(1) != 0, + "crash child must not exit cleanly" + ); +} + +/// The child half of [`run_crashing_child`]. A no-op in ordinary test runs. +/// +/// This is a `#[test]` purely so the harness will dispatch to it by name; when +/// the marker environment variable is absent it returns immediately. +#[test] +fn crash_child_entrypoint() { + let Ok(db_path) = std::env::var("MINIGRAF_CRASH_DB") else { + return; + }; + let threshold: usize = std::env::var("MINIGRAF_CRASH_THRESHOLD") + .expect("threshold") + .parse() + .expect("threshold is a number"); + let stmts = std::env::var("MINIGRAF_CRASH_STMTS").expect("statements"); + let explicit_tx = std::env::var("MINIGRAF_CRASH_TX").is_ok(); + + let db = Minigraf::open_with_options( + &db_path, + OpenOptions { + wal_checkpoint_threshold: threshold, + ..Default::default() + }, + ) + .expect("child opens db"); + + if explicit_tx { + // All statements commit together as one explicit transaction, so a + // crash right after `commit()` must recover either all of them or + // none of them -- not a subset. + let mut tx = db.begin_write().expect("child begin_write"); + for stmt in stmts.split('\u{1e}').filter(|s| !s.is_empty()) { + tx.execute(stmt).expect("child tx statement"); + } + tx.commit().expect("child commit"); + } else { + for stmt in stmts.split('\u{1e}').filter(|s| !s.is_empty()) { + db.execute(stmt).expect("child statement"); + } + } + + // Die without running Drop, so no checkpoint happens and the WAL is left + // for the next open to replay. + std::process::abort(); +} + #[test] fn v7_round_trip_is_idempotent() { let dir = tempfile::tempdir().unwrap(); @@ -93,20 +214,14 @@ fn wal_replay_after_migration_is_idempotent() { db.execute(r#"(transact [[:e1 :color "red"]])"#).unwrap(); db.checkpoint().unwrap(); } - { - let db = Minigraf::open(&path).unwrap(); - db.execute(r#"(transact [[:e2 :color "blue"]])"#).unwrap(); - // Simulate a crash: no Drop, so no checkpoint, and the WAL is left for - // the next open to replay. - std::mem::forget(db); - } - // mem::forget also skips FileLock::drop, leaving the sidecar lock behind - // holding OUR pid. A lock held by a live pid means a handle that is open - // right now, so reopening is correctly refused (#304) -- but that is not - // what a crash leaves behind. Clear it, which models the state the next - // open finds once a crashed holder is cleaned up. The WAL-replay assertion - // below is unchanged. - std::fs::remove_file(path.with_extension("graph.lock")).unwrap(); + // Crash before checkpoint: no Drop runs, so the WAL is left for the next + // open to replay. + run_crashing_child( + &path, + 1000, + &[r#"(transact [[:e2 :color "blue"]])"#], + CrashTx::Implicit, + ); let db3 = Minigraf::open(&path).unwrap(); let n = count_results( db3.execute("(query [:find ?c :where [?e :color ?c]])") diff --git a/tests/pid_namespace_test.rs b/tests/pid_namespace_test.rs new file mode 100644 index 0000000000..06f6829ead --- /dev/null +++ b/tests/pid_namespace_test.rs @@ -0,0 +1,232 @@ +//! #317: a `.graph` file must stay openable after its holder dies in another +//! PID namespace. +//! +//! Kubernetes is where this was reported, but the bug has nothing to do with +//! Kubernetes: every container's main process is PID 1 in its own namespace, so +//! a lock keyed on a PID cannot tell a crashed holder from a live one. `unshare` +//! reproduces it in a few seconds with no container runtime. +#![cfg(target_os = "linux")] + +use std::path::Path; +use std::process::{Command, Stdio}; + +/// The `unshare` invocation that puts a command at PID 1 in a fresh namespace, +/// or `None` if this environment cannot create one. +/// +/// Ubuntu 24.04 restricts unprivileged user namespaces via +/// `kernel.apparmor_restrict_unprivileged_userns`, so fall back to `sudo` where +/// it is available passwordlessly (GitHub runners). If neither works the test +/// skips rather than fails: not every environment permits namespaces. +fn unshare_prefix() -> Option> { + let unprivileged: Vec = [ + "unshare", + "--user", + "--map-root-user", + "--pid", + "--fork", + "--mount-proc", + "--kill-child", + ] + .iter() + .map(|s| s.to_string()) + .collect(); + + let privileged: Vec = [ + "sudo", + "-n", + "unshare", + "--pid", + "--fork", + "--mount-proc", + "--kill-child", + ] + .iter() + .map(|s| s.to_string()) + .collect(); + + for candidate in [unprivileged, privileged] { + let ok = Command::new(&candidate[0]) + .args(&candidate[1..]) + .args(["sh", "-c", "test \"$$\" = 1"]) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .map(|s| s.success()) + .unwrap_or(false); + if ok { + return Some(candidate); + } + } + None +} + +/// Runs the helper binary at PID 1 in a fresh PID namespace. +fn spawn_in_namespace( + prefix: &[String], + helper: &Path, + db: &Path, + mode: &str, + release_marker: Option<&Path>, +) -> std::process::Child { + let mut cmd = Command::new(&prefix[0]); + cmd.args(&prefix[1..]) + .arg(helper) + .arg(db) + .arg(mode) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()); + + // With no marker the holder aborts the moment it has the lock. With one, + // it holds until this process creates that file. See the module comment on + // `examples/pid_ns_helper.rs` for why the holder ends its own life rather + // than being killed from here, why the hold cannot be a fixed sleep, and + // why this is an argument rather than an environment variable -- sudo + // strips the environment, and an env var here silently did nothing on CI. + if let Some(marker) = release_marker { + cmd.arg(marker); + } + + cmd.spawn().expect("spawn in namespace") +} + +/// Path to the compiled helper binary (see `examples/pid_ns_helper.rs`). +fn helper_binary() -> std::path::PathBuf { + // target/debug/examples/pid_ns_helper, resolved relative to the test binary + // at target/debug/deps/pid_namespace_test-. + let mut dir = std::env::current_exe().expect("test binary path"); + dir.pop(); // deps/ + dir.pop(); // debug/ + dir.join("examples").join("pid_ns_helper") +} + +/// Fails loudly if the helper binary this environment could have run is +/// missing, instead of silently skipping. +/// +/// Call this only after `unshare_prefix()` has already returned `Some`: at +/// that point this environment has proven it can create PID namespaces, so +/// the one remaining reason `helper_binary()` could be absent is that +/// `cargo build --examples` was not run -- a build problem the developer +/// should see immediately, not a reason to assert nothing and report green. +/// A skip is reserved for `unshare_prefix()` returning `None`, i.e. this +/// environment genuinely cannot create PID namespaces at all. +/// +/// This also guards against the assumption (verified by hand, not encoded +/// anywhere else) that plain `cargo test` builds `examples/` before running +/// integration tests: if a future cargo stops doing that, this test turns +/// red and visible in CI instead of green and vacuous. +fn require_helper_built(helper: &Path) { + assert!( + helper.exists(), + "helper binary not found at {}; run `cargo build --examples` first", + helper.display() + ); +} + +#[test] +fn test_lock_survives_holder_death_in_another_pid_namespace() { + let Some(prefix) = unshare_prefix() else { + eprintln!("SKIP: this environment cannot create PID namespaces"); + return; + }; + let helper = helper_binary(); + require_helper_built(&helper); + + let dir = tempfile::tempdir().unwrap(); + let db = dir.path().join("shared.graph"); + + // Container A: opens the database and holds it. Wrapped so a panic + // unwinding past the explicit `kill`/`wait` below (e.g. the `assert!` + // right after this, on a namespace flake or a slow runner) still kills + // and reaps it instead of orphaning a namespaced holder sitting on the + // flock for its 300-second sleep. + // Container A opens the database and then dies WITHOUT running Drop -- + // no checkpoint, no library-side lock release, exactly what a SIGKILLed + // container leaves behind. The holder aborts itself rather than being + // killed from here; see the module comment on examples/pid_ns_helper.rs + // for why killing across the privilege boundary cannot be made reliable. + let a = spawn_in_namespace(&prefix, &helper, &db, "hold", None) + .wait_with_output() + .expect("container A ran"); + + // A must actually have taken the lock before dying, or this proves + // nothing. It printed OPEN_OK before aborting, and it did not exit + // cleanly. + let a_out = String::from_utf8_lossy(&a.stdout); + assert!( + a_out.contains("OPEN_OK"), + "container A never opened the database.\n\ + prefix used: {prefix:?}\n\ + container A stdout: {a_out}\n\ + container A stderr: {}", + String::from_utf8_lossy(&a.stderr) + ); + assert!( + a.status.code().unwrap_or(1) != 0, + "container A must die by abort, not exit cleanly" + ); + + let b = spawn_in_namespace(&prefix, &helper, &db, "open", None) + .wait_with_output() + .expect("container B ran"); + + let out = String::from_utf8_lossy(&b.stdout); + let err = String::from_utf8_lossy(&b.stderr); + assert!( + out.contains("OPEN_OK"), + "a replacement container must be able to open the database after its \ + predecessor was killed; this is #317.\n\ + prefix used: {prefix:?}\n\ + container B stdout: {out}\n\ + container B stderr: {err}" + ); +} + +#[test] +fn test_two_live_namespaced_holders_are_still_refused() { + let Some(prefix) = unshare_prefix() else { + eprintln!("SKIP: this environment cannot create PID namespaces"); + return; + }; + let helper = helper_binary(); + require_helper_built(&helper); + + let dir = tempfile::tempdir().unwrap(); + let db = dir.path().join("contended.graph"); + + // panicking (B fails to spawn/run) without leaking a namespaced holder. + // A holds until this process releases it, however long B takes to start + // and be refused. The helper caps the wait as a backstop, so a panic here + // cannot strand a namespaced process on the flock. + let release = dir.path().join("release-a"); + let a = spawn_in_namespace(&prefix, &helper, &db, "hold", Some(&release)); + let mut held = false; + for _ in 0..80 { + if db.exists() && std::fs::metadata(&db).map(|m| m.len() > 0).unwrap_or(false) { + held = true; + break; + } + std::thread::sleep(std::time::Duration::from_millis(50)); + } + assert!(held, "container A never opened the database"); + + // A is still alive. B must be refused — this is the guarantee PR #318's + // start-time comparison would have broken, admitting two live writers. + let b = spawn_in_namespace(&prefix, &helper, &db, "open", None) + .wait_with_output() + .expect("container B ran"); + let out = String::from_utf8_lossy(&b.stdout); + let err = String::from_utf8_lossy(&b.stderr); + + // Release A now that B's attempt is recorded, then reap it. + std::fs::write(&release, b"").expect("write release marker"); + let mut a = a; + let _ = a.wait(); + + assert!( + out.contains("OPEN_ERR"), + "a second live holder in another PID namespace must still be refused.\n\ + prefix used: {prefix:?}\n\ + container B stdout: {out}\n\ + container B stderr: {err}" + ); +} diff --git a/tests/wal_test.rs b/tests/wal_test.rs index 18eb775496..4d4d7bd26f 100644 --- a/tests/wal_test.rs +++ b/tests/wal_test.rs @@ -2,7 +2,7 @@ //! //! These tests exercise the file-backed `Minigraf` API end-to-end, verifying: //! - Basic persistence (write → drop → reopen) -//! - WAL crash recovery (simulated crash via `mem::forget`) +//! - WAL crash recovery (real crash via a child process that aborts) //! - Duplicate-free recovery after post-checkpoint crash //! - Partial WAL entry discarding //! - Manual checkpoint behaviour @@ -15,6 +15,126 @@ use minigraf::QueryResult; use minigraf::db::{Minigraf, OpenOptions}; +/// Serializes `Command::spawn` calls within this test binary. +/// +/// `Command::spawn` forks the whole (multi-threaded) test process before +/// exec'ing the child. Until that child reaches `execve` and its +/// close-on-exec descriptors are closed, it transiently holds a duplicate of +/// every flock any other thread's test currently has open on a `.graph` +/// file — which can make an unrelated, concurrent `try_lock()` elsewhere in +/// this binary observe `WouldBlock` for a few microseconds and fail with +/// "Database is locked by another process", even though no other process +/// was ever really involved. Capping this binary to one fork in flight at a +/// time keeps that window rare enough not to flake the suite. +/// +/// This is now belt-and-braces: `FileBackend::open_with` (src/storage/backend/file.rs) +/// retries a cross-process `WouldBlock` with bounded backoff, which is the +/// real fix for the false-negative window described above. This mutex is +/// cheap insurance on top of that, not load-bearing on its own. +static CRASH_CHILD_SPAWN_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + +/// Whether the crashing child commits `statements` as one explicit +/// transaction or as separate auto-committed writes. +#[derive(Clone, Copy)] +enum CrashTx { + /// Each statement is its own auto-committed `db.execute()` call — the + /// implicit-transaction path. + Implicit, + /// All statements run inside one `begin_write()` / `commit()` — pins + /// that `WriteTransaction::commit()` itself writes to the WAL before a + /// crash, and that multiple pending facts survive (or are lost) as one + /// atomic unit rather than as independent writes. + Explicit, +} + +/// Runs a child process that opens `db_path`, applies `statements` (per +/// `tx`), then dies by `abort()` without running any `Drop`. +/// +/// This is a real crash, not a simulated one: the child's file descriptors are +/// closed by the kernel, which releases the file lock exactly as it would for a +/// SIGKILLed container. Forgetting the handle in-process cannot model this — it +/// leaks the `File`, so the lock stays held for the life of the test process. +fn run_crashing_child( + db_path: &std::path::Path, + wal_checkpoint_threshold: usize, + statements: &[&str], + tx: CrashTx, +) { + let exe = std::env::current_exe().expect("test binary path"); + let mut command = std::process::Command::new(exe); + command + .args(["crash_child_entrypoint", "--exact", "--nocapture"]) + .env("MINIGRAF_CRASH_DB", db_path) + .env( + "MINIGRAF_CRASH_THRESHOLD", + wal_checkpoint_threshold.to_string(), + ) + .env("MINIGRAF_CRASH_STMTS", statements.join("\u{1e}")); + if matches!(tx, CrashTx::Explicit) { + command.env("MINIGRAF_CRASH_TX", "1"); + } + + let status = { + let _guard = CRASH_CHILD_SPAWN_LOCK + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()); + command.status().expect("spawn crash child") + }; + + // On Unix `abort()` raises SIGABRT and `code()` is None. On Windows it + // terminates with a nonzero exit code instead. The property that matters + // on both is that the child did not exit cleanly. + assert!( + status.code().unwrap_or(1) != 0, + "crash child must not exit cleanly" + ); +} + +/// The child half of [`run_crashing_child`]. A no-op in ordinary test runs. +/// +/// This is a `#[test]` purely so the harness will dispatch to it by name; when +/// the marker environment variable is absent it returns immediately. +#[test] +fn crash_child_entrypoint() { + let Ok(db_path) = std::env::var("MINIGRAF_CRASH_DB") else { + return; + }; + let threshold: usize = std::env::var("MINIGRAF_CRASH_THRESHOLD") + .expect("threshold") + .parse() + .expect("threshold is a number"); + let stmts = std::env::var("MINIGRAF_CRASH_STMTS").expect("statements"); + let explicit_tx = std::env::var("MINIGRAF_CRASH_TX").is_ok(); + + let db = Minigraf::open_with_options( + &db_path, + OpenOptions { + wal_checkpoint_threshold: threshold, + ..Default::default() + }, + ) + .expect("child opens db"); + + if explicit_tx { + // All statements commit together as one explicit transaction, so a + // crash right after `commit()` must recover either all of them or + // none of them -- not a subset. + let mut tx = db.begin_write().expect("child begin_write"); + for stmt in stmts.split('\u{1e}').filter(|s| !s.is_empty()) { + tx.execute(stmt).expect("child tx statement"); + } + tx.commit().expect("child commit"); + } else { + for stmt in stmts.split('\u{1e}').filter(|s| !s.is_empty()) { + db.execute(stmt).expect("child statement"); + } + } + + // Die without running Drop, so no checkpoint happens and the WAL is left + // for the next open to replay. + std::process::abort(); +} + /// File format page size (4 KiB) — matches the internal `PAGE_SIZE` constant. const PAGE_SIZE: usize = 4096; @@ -60,7 +180,7 @@ fn test_file_backed_basic_persistence() { // ── 2. WAL recovery after simulated crash ───────────────────────────────────── /// Write a fact with a very high checkpoint threshold so the checkpoint never fires, -/// then `mem::forget` the DB to simulate a crash (skipping the Drop checkpoint). +/// then crash a child process holding the DB (skipping the Drop checkpoint). /// Verify the WAL exists, then reopen and confirm the fact was recovered. #[test] fn test_wal_recovery_after_simulated_crash() { @@ -69,26 +189,12 @@ fn test_wal_recovery_after_simulated_crash() { let wal_path = wal_path_for(&db_path); // "Crash" session: write fact, skip Drop - { - let db = Minigraf::open_with_options( - &db_path, - OpenOptions { - wal_checkpoint_threshold: usize::MAX, - ..Default::default() - }, - ) - .unwrap(); - db.execute(r#"(transact [[:alice :name "Alice"]])"#) - .unwrap(); - - // Simulate a crash: drop Inner without running Drop logic. - // mem::forget on the Arc-backed Minigraf drops the Arc but leaves - // the Inner alive as long as the clone lives — however, since this - // is the only handle, forgetting it leaks the Arc permanently and - // the Drop impl never runs. - std::mem::forget(db); - simulate_crashed_holder(&db_path); - } + run_crashing_child( + &db_path, + 1_000_000, + &[r#"(transact [[:alice :name "Alice"]])"#], + CrashTx::Implicit, + ); // WAL must still exist (no checkpoint happened) assert!(wal_path.exists(), "WAL must exist after simulated crash"); @@ -118,20 +224,12 @@ fn test_no_duplicate_facts_after_post_checkpoint_crash() { let wal_path = wal_path_for(&db_path); // Session 1: write fact and crash (skip Drop) - { - let db = Minigraf::open_with_options( - &db_path, - OpenOptions { - wal_checkpoint_threshold: usize::MAX, - ..Default::default() - }, - ) - .unwrap(); - db.execute(r#"(transact [[:alice :name "Alice"]])"#) - .unwrap(); - std::mem::forget(db); - simulate_crashed_holder(&db_path); - } + run_crashing_child( + &db_path, + usize::MAX, + &[r#"(transact [[:alice :name "Alice"]])"#], + CrashTx::Implicit, + ); // Back up the WAL before the next open checkpoints it away let wal_backup = std::fs::read(&wal_path).unwrap(); @@ -178,20 +276,12 @@ fn test_partial_wal_entry_discarded_earlier_entries_intact() { let wal_path = wal_path_for(&db_path); // Session 1: write 1 fact and crash - { - let db = Minigraf::open_with_options( - &db_path, - OpenOptions { - wal_checkpoint_threshold: usize::MAX, - ..Default::default() - }, - ) - .unwrap(); - db.execute(r#"(transact [[:alice :name "Alice"]])"#) - .unwrap(); - std::mem::forget(db); - simulate_crashed_holder(&db_path); - } + run_crashing_child( + &db_path, + usize::MAX, + &[r#"(transact [[:alice :name "Alice"]])"#], + CrashTx::Implicit, + ); // Append garbage bytes after the valid WAL entry (simulate partial second write) { @@ -261,9 +351,24 @@ fn test_manual_checkpoint_deletes_wal() { ); assert_eq!(n, 1, "Alice must still be visible after checkpoint"); + // Nothing is pending after the checkpoint above (no WAL to lose), so a + // real crash and a normal close are observationally identical here. + // Dropping `db` releases the kernel file lock exactly as process death + // would — no subprocess is needed just to prove that. + drop(db); + // Main file header must reflect the checkpoint. // Reads the raw bytes: version is at offset 4..8 (u32 LE), // last_checkpointed_tx_count is at offset 24..32 (u64 LE). + // + // This read has to come AFTER the drop above. Windows file locks are + // mandatory rather than advisory, and they exclude every other handle -- + // including another handle in this same process. Reading the header + // through a second `File` while `db` still holds the lock fails on + // Windows with os error 33, "another process has locked a portion of the + // file", even though the "other process" is us. On Unix the lock is + // advisory and the read would succeed either way, which is exactly why + // this only ever failed on one leg of the CI matrix. { use std::io::Read; let mut f = std::fs::File::open(&db_path).unwrap(); @@ -276,10 +381,6 @@ fn test_manual_checkpoint_deletes_wal() { ); } - // Simulate crash: skip Drop (checkpoint already happened, no WAL to write) - std::mem::forget(db); - simulate_crashed_holder(&db_path); - // Reopen: must recover the fact from the main file alone (no WAL needed) let db2 = Minigraf::open(&db_path).unwrap(); let n2 = count_results( @@ -294,9 +395,9 @@ fn test_manual_checkpoint_deletes_wal() { // ── 6. Auto-checkpoint fires at threshold ───────────────────────────────────── -/// Set threshold=2, write 2 facts (triggering auto-checkpoint on the 2nd write). -/// Crash (mem::forget), then reopen. The facts must be in the main file — no WAL -/// needed for recovery. +/// Set threshold=2, write 2 facts (triggering auto-checkpoint on the 2nd write), +/// close, then reopen. The facts must be in the main file — no WAL needed for +/// recovery. #[test] fn test_auto_checkpoint_fires_at_threshold() { let dir = tempfile::tempdir().unwrap(); @@ -321,9 +422,10 @@ fn test_auto_checkpoint_fires_at_threshold() { !wal_path.exists(), "WAL must be deleted after auto-checkpoint at threshold=2" ); - // Crash: skip Drop checkpoint (but checkpoint already happened) - std::mem::forget(db); - simulate_crashed_holder(&db_path); + // Crash: skip Drop checkpoint. Nothing is pending though — the + // auto-checkpoint above already flushed the WAL, so a normal drop + // here releases the kernel lock exactly as a real crash would, with + // nothing left to lose. } // No WAL after crash @@ -346,34 +448,30 @@ fn test_auto_checkpoint_fires_at_threshold() { // ── 7. Explicit tx: all-or-nothing commit ───────────────────────────────────── -/// begin_write → 2 transacts → commit → crash (mem::forget) → reopen. +/// begin_write → 2 transacts → commit → crash → reopen. /// Both facts must be present. #[test] fn test_explicit_tx_all_or_nothing_commit() { let dir = tempfile::tempdir().unwrap(); let db_path = dir.path().join("explicit_commit.graph"); - // Session 1: explicit commit then crash - { - let db = Minigraf::open_with_options( - &db_path, - OpenOptions { - wal_checkpoint_threshold: usize::MAX, - ..Default::default() - }, - ) - .unwrap(); - - let mut tx = db.begin_write().unwrap(); - tx.execute(r#"(transact [[:alice :name "Alice"]])"#) - .unwrap(); - tx.execute(r#"(transact [[:bob :name "Bob"]])"#).unwrap(); - tx.commit().unwrap(); - - // Crash before Drop checkpoint - std::mem::forget(db); - simulate_crashed_holder(&db_path); - } + // Session 1: one explicit transaction with both facts, committed + // together, then crash before checkpoint. `CrashTx::Explicit` makes the + // child wrap both statements in a single `begin_write()` / `commit()`, + // so this pins two distinct things: that `WriteTransaction::commit()` + // itself writes to the WAL (not just the implicit per-statement + // `db.execute()` path, which `test_implicit_tx_execute_survives_replay` + // already covers), and that the commit is atomic as a unit — recovery + // below must see both facts together, never just one of them. + run_crashing_child( + &db_path, + usize::MAX, + &[ + r#"(transact [[:alice :name "Alice"]])"#, + r#"(transact [[:bob :name "Bob"]])"#, + ], + CrashTx::Explicit, + ); // Recovery session let db2 = Minigraf::open(&db_path).unwrap(); @@ -497,7 +595,7 @@ fn test_concurrent_reads_while_writer_holds_lock() { /// Test strategy: /// 1. Open a file-backed database with a very high checkpoint threshold. /// 2. Call `execute("(transact ...)")` — the implicit-transaction path. -/// 3. `mem::forget` the database to simulate a crash (no Drop checkpoint). +/// 3. Crash a child process holding the database (no Drop checkpoint). /// 4. Reopen the database (triggers WAL replay). /// 5. Assert the fact is present — proving the WAL was written during step 2. #[test] @@ -507,23 +605,12 @@ fn test_implicit_tx_execute_survives_replay() { let wal_path = wal_path_for(&db_path); // Session 1: write via implicit execute() then crash (skip Drop) - { - let db = Minigraf::open_with_options( - &db_path, - OpenOptions { - wal_checkpoint_threshold: usize::MAX, - ..Default::default() - }, - ) - .unwrap(); - - db.execute(r#"(transact [[:alice :name "Alice"]])"#) - .unwrap(); - - // Simulate crash: skip Drop (and its checkpoint). - std::mem::forget(db); - simulate_crashed_holder(&db_path); - } + run_crashing_child( + &db_path, + usize::MAX, + &[r#"(transact [[:alice :name "Alice"]])"#], + CrashTx::Implicit, + ); // WAL must exist — no checkpoint fired. assert!(wal_path.exists(), "WAL must exist after simulated crash"); @@ -553,12 +640,12 @@ fn write_wal_bytes(db_path: &std::path::Path, bytes: &[u8]) { fn setup_db_with_one_fact() -> (tempfile::TempDir, std::path::PathBuf, Vec) { let dir = tempfile::tempdir().unwrap(); let db_path = dir.path().join("test.graph"); - { - let db = minigraf::db::Minigraf::open(&db_path).unwrap(); - db.execute(r#"(transact [[:e1 :name "Alice"]])"#).unwrap(); - std::mem::forget(db); - simulate_crashed_holder(&db_path); - } + run_crashing_child( + &db_path, + 1000, + &[r#"(transact [[:e1 :name "Alice"]])"#], + CrashTx::Implicit, + ); let wal_bytes = read_wal_bytes(&db_path); (dir, db_path, wal_bytes) } @@ -607,13 +694,15 @@ fn wal_recover_truncated_payload() { fn wal_recover_bad_checksum_second_entry() { let dir = tempfile::tempdir().unwrap(); let db_path = dir.path().join("test.graph"); - { - let db = minigraf::db::Minigraf::open(&db_path).unwrap(); - db.execute(r#"(transact [[:e1 :name "Alice"]])"#).unwrap(); - db.execute(r#"(transact [[:e2 :name "Bob"]])"#).unwrap(); - std::mem::forget(db); - simulate_crashed_holder(&db_path); - } + run_crashing_child( + &db_path, + 1000, + &[ + r#"(transact [[:e1 :name "Alice"]])"#, + r#"(transact [[:e2 :name "Bob"]])"#, + ], + CrashTx::Implicit, + ); let mut wal_bytes = read_wal_bytes(&db_path); assert!(wal_bytes.len() > 36, "WAL too short to corrupt"); let n = wal_bytes.len(); @@ -633,14 +722,16 @@ fn wal_recover_bad_checksum_second_entry() { fn wal_recover_committed_tx_crash_before_checkpoint() { let dir = tempfile::tempdir().unwrap(); let db_path = dir.path().join("test.graph"); - { - let db = minigraf::db::Minigraf::open(&db_path).unwrap(); - let mut tx = db.begin_write().unwrap(); - tx.execute(r#"(transact [[:e1 :name "Charlie"]])"#).unwrap(); - tx.commit().unwrap(); - std::mem::forget(db); - simulate_crashed_holder(&db_path); - } + // `CrashTx::Explicit` makes the child commit through + // `begin_write()`/`commit()`, pinning that an explicitly committed + // transaction (not just the implicit `db.execute()` path) survives a + // crash before checkpoint. + run_crashing_child( + &db_path, + 1000, + &[r#"(transact [[:e1 :name "Charlie"]])"#], + CrashTx::Explicit, + ); let names = query_names(&db_path); assert_eq!( names.len(), @@ -662,8 +753,9 @@ fn wal_recover_rollback_crash() { let mut tx = db.begin_write().unwrap(); tx.execute(r#"(transact [[:e1 :name "Dave"]])"#).unwrap(); tx.rollback(); - std::mem::forget(db); - simulate_crashed_holder(&db_path); + // Rollback discards pending facts before they are ever flushed to + // the WAL, so there is nothing here for a crash to lose. A normal + // drop releases the kernel lock exactly as process death would. } let names = query_names(&db_path); assert_eq!( @@ -677,13 +769,15 @@ fn wal_recover_rollback_crash() { fn wal_recover_multiple_committed_corrupt_tail() { let dir = tempfile::tempdir().unwrap(); let db_path = dir.path().join("test.graph"); - { - let db = minigraf::db::Minigraf::open(&db_path).unwrap(); - db.execute(r#"(transact [[:e1 :name "Eve"]])"#).unwrap(); - db.execute(r#"(transact [[:e2 :name "Frank"]])"#).unwrap(); - std::mem::forget(db); - simulate_crashed_holder(&db_path); - } + run_crashing_child( + &db_path, + 1000, + &[ + r#"(transact [[:e1 :name "Eve"]])"#, + r#"(transact [[:e2 :name "Frank"]])"#, + ], + CrashTx::Implicit, + ); let mut wal_bytes = read_wal_bytes(&db_path); wal_bytes.extend_from_slice(&[0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x00]); write_wal_bytes(&db_path, &wal_bytes); @@ -699,12 +793,12 @@ fn wal_recover_multiple_committed_corrupt_tail() { fn wal_corrupt_tail_never_applied() { let dir = tempfile::tempdir().unwrap(); let db_path = dir.path().join("test.graph"); - { - let db = minigraf::db::Minigraf::open(&db_path).unwrap(); - db.execute(r#"(transact [[:e1 :name "Grace"]])"#).unwrap(); - std::mem::forget(db); - simulate_crashed_holder(&db_path); - } + run_crashing_child( + &db_path, + 1000, + &[r#"(transact [[:e1 :name "Grace"]])"#], + CrashTx::Implicit, + ); let mut wal_bytes = read_wal_bytes(&db_path); let mut fake_entry: Vec = Vec::new(); fake_entry.extend_from_slice(&0u32.to_le_bytes()); @@ -820,23 +914,3 @@ fn test_v2_file_opens_and_upgrades_to_v3_on_checkpoint() { "last_checkpointed_tx_count must be set after checkpoint on v2→v6 upgrade" ); } - -/// Complete a `mem::forget`-based crash simulation. -/// -/// `mem::forget` skips `FileLock::drop`, so the sidecar `.graph.lock` is left -/// behind holding OUR pid. A lock held by a live pid means a handle that is -/// open right now, so reopening is correctly refused (#304) -- but that is not -/// what a crash leaves behind. A crashed process leaves the lock holding its -/// own, now-dead pid. -/// -/// Removing the lock models the state the next open actually finds after a -/// crashed holder is cleaned up, and does so on every platform. Writing a dead -/// pid instead would only work on procfs targets: `is_process_alive` cannot -/// tell dead from live without `/proc` and deliberately errs towards "alive", -/// so a dead pid still blocks on macOS and Windows. Reclamation of a dead -/// holder's lock is covered directly by -/// `storage::backend::file::tests::test_dead_other_process_lock_is_still_reclaimed`; -/// these tests are about WAL replay. -fn simulate_crashed_holder(db_path: &std::path::Path) { - let _ = std::fs::remove_file(db_path.with_extension("graph.lock")); -}