From 06203f89f4185a67568c4bb414f4f281f442a6f3 Mon Sep 17 00:00:00 2001 From: AIEN Atlas Date: Fri, 16 Oct 2026 02:46:48 -0500 Subject: [PATCH 1/2] fix(kv-store): one process per store folder, an exclusive flock on root/LOCK (sc#521) Evidence: cargo test -p aien-kv-store: 18 test binaries pass, including the new a_second_opener_of_the_same_folder_is_refused_until_the_first_closes and the_lock_file_is_private_and_lives_in_the_root; with the lock weakened to LOCK_SH the second-opener test fails (red); serve-http kv_persist (17) and shutdown tests pass; clippy -D warnings clean. HostFsStore::open took no cross-process lock, so two runtimes could open one folder: recovery unlinks every temp it finds, which would fail the other process's in-flight parks at their rename, and each process would keep its own drifting index and quota. open now creates root/LOCK privately (no symlink followed, 0600) and takes flock(LOCK_EX | LOCK_NB) on it before recovery, held by the store for its lifetime (released after the worker joined). A second opener is refused with TaskRejected naming the folder; the serving layer already prints that as "persistent KV: REFUSED (...)" and runs without persistence. libc was already a dependency of the crate; no new crates. Fixes #521 Co-authored-by: Drake Stapleton Co-Authored-By: Claude Fable 5.1 --- crates/aien-kv-store/src/host_fs.rs | 47 +++++++++++++++++++ .../tests/host_fs_security_tests.rs | 42 +++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/crates/aien-kv-store/src/host_fs.rs b/crates/aien-kv-store/src/host_fs.rs index c6d57fea..61ec7eb2 100644 --- a/crates/aien-kv-store/src/host_fs.rs +++ b/crates/aien-kv-store/src/host_fs.rs @@ -13,10 +13,16 @@ //! ```text //! / //! STORE_ID +//! LOCK //! objects/00/ .. objects/ff/ //! tmp/ //! ``` //! +//! `LOCK` carries an exclusive advisory lock (`flock`) for the store's +//! lifetime: one process per store folder (sc#521). A second opener is +//! refused before recovery, which would otherwise unlink the first process's +//! in-flight temps and keep a second, drifting index of the same folder. +//! //! All 256 shard directories and `tmp/` are created at [`HostFsStore::open`] //! with a parent-directory `fsync`; the hot path never creates a directory. @@ -548,6 +554,9 @@ struct Shared { pub struct HostFsStore { shared: Arc, worker: Mutex>>, + /// The open `LOCK` file whose `flock` makes this process the folder's + /// only opener; released when the store drops (after the worker joined). + _folder_lock: File, } static STORE_ID_COUNTER: AtomicU64 = AtomicU64::new(1); @@ -702,6 +711,36 @@ fn create_private_file(path: &Path) -> std::io::Result { Ok(f) } +/// The lock file in the store root (sc#521). +const LOCK_FILE: &str = "LOCK"; + +/// Takes the folder's exclusive advisory lock: `root/LOCK` opened privately +/// (no symlink followed, ours, 0600) and `flock(LOCK_EX | LOCK_NB)` on it. +/// The lock belongs to the returned open file and ends when it closes. A +/// folder held by another opener (another process, or a second store in +/// this one: `flock` is per open file description) is refused with +/// `TaskRejected` and a message that names the folder; any other failure is +/// reported like every other store I/O error. +fn take_folder_lock(root: &Path) -> Result { + use std::os::unix::io::AsRawFd; + let path = root.join(LOCK_FILE); + let f = create_private_file(&path).map_err(|e| io_to_error(&e, "open LOCK"))?; + // SAFETY: `f` is an open descriptor this function owns; flock takes no + // memory and only sets the description's advisory lock. + let rc = unsafe { libc::flock(f.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) }; + if rc == 0 { + return Ok(f); + } + let e = std::io::Error::last_os_error(); + if e.raw_os_error() == Some(libc::EWOULDBLOCK) { + return Err(PlatformError::TaskRejected(format!( + "store folder {} is open in another process (LOCK held); one process per store folder", + root.display() + ))); + } + Err(io_to_error(&e, "flock LOCK")) +} + /// `create_private_file` plus the bytes (fault injection, temps). fn write_private_file(path: &Path, bytes: &[u8]) -> std::io::Result<()> { create_private_file(path)?.write_all(bytes) @@ -3014,6 +3053,13 @@ impl HostFsStore { } } + // One process per store folder (sc#521), taken before recovery: the + // walk below unlinks every temp it finds, so a second opener would + // fail the first process's in-flight parks at their rename and keep + // its own drifting index and quota of the same folder. + let folder_lock = take_folder_lock(root)?; + stats.io_ops += 1; + let alloc_unit = std::fs::metadata(&objects_dir) .map(|m| m.blksize()) .unwrap_or(4096) @@ -3145,6 +3191,7 @@ impl HostFsStore { Ok(Self { shared, worker: Mutex::new(handle), + _folder_lock: folder_lock, }) } diff --git a/crates/aien-kv-store/tests/host_fs_security_tests.rs b/crates/aien-kv-store/tests/host_fs_security_tests.rs index 6a0659c3..b977198e 100644 --- a/crates/aien-kv-store/tests/host_fs_security_tests.rs +++ b/crates/aien-kv-store/tests/host_fs_security_tests.rs @@ -295,3 +295,45 @@ fn a_permissive_umask_does_not_widen_the_store() { fn a_narrow_umask_does_not_lock_the_store_out_of_itself() { run_under_umask(0o277); } + +// ---- one process per store folder (sc#521) -------------------------------- + +/// `root/LOCK` is private like every other store file. +#[test] +fn the_lock_file_is_private_and_lives_in_the_root() { + let fx = Fx::new(); + let _s = fx.open_with(|_| {}); + let lock = fx.root().join("LOCK"); + assert!(std::fs::symlink_metadata(&lock).unwrap().is_file()); + assert_private(&lock); +} + +/// A second opener of the same folder is refused with a message that names +/// the folder, the first store keeps working, and the folder opens again +/// once the first store is dropped. `flock` locks belong to the open file +/// description, so a second `open` in this process is refused exactly as a +/// second process would be. +#[test] +fn a_second_opener_of_the_same_folder_is_refused_until_the_first_closes() { + let fx = Fx::new(); + let first = fx.open_with(|_| {}); + assert_eq!(first.capacity().state, StoreState::Open); + + let second = HostFsStore::open(fx.config()); + match &second { + Err(PlatformError::TaskRejected(why)) => { + assert!(why.contains("another process"), "{why}"); + assert!(why.contains(&fx.root().display().to_string()), "{why}"); + } + Ok(_) => panic!("second opener must be refused, but it opened"), + Err(e) => panic!("second opener must be refused with TaskRejected, got {e:?}"), + } + // The refusal touched nothing the first store relies on. + assert_eq!(first.capacity().state, StoreState::Open); + assert!(fx.root().join("STORE_ID").is_file()); + + drop(second); + drop(first); + let again = fx.open_with(|_| {}); + assert_eq!(again.capacity().state, StoreState::Open); +} From a4f3942309427cac43e87f9747cb779e3073e1cb Mon Sep 17 00:00:00 2001 From: AIEN Date: Fri, 16 Oct 2026 13:41:21 -0500 Subject: [PATCH 2/2] crumb: recompile after rebase onto main (judge tool /usr/local/bin/crumb, 2026-10-11 build) Co-Authored-By: Claude Fable 5.1 Co-authored-by: Drake Stapleton --- .crumb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/.crumb b/.crumb index f1d77dbe..caa30ae6 100644 --- a/.crumb +++ b/.crumb @@ -65,68 +65,68 @@ "children": [ { "digest": "4e7f370019e67f6c71565d1544530851eb1b3b1bed61c2712f4ac7d9614a89bf", - "last_commit": "6bbe2ec2", + "last_commit": "6bbe2ec", "name": "crates/aien-allen", "purpose": "ALLEN identity gate: resolve-only subject check, host pin record, deployment record (ADR 0035). " }, { "digest": "40a231c50dba92af7de2527ab33879c5d1eaaa6e7ea5d6a5ebaa9d60c7542075", - "last_commit": "b9b52f9c", + "last_commit": "b9b52f9", "name": "crates/aien-allen-memory", "purpose": "ALLEN scoped memory store: scopes enforced at recall, inspect/correct/export, key-destroying for" }, { "digest": "44dc482b8c9db8919a68ec69b019cd77967232a6ec8b1910c0dfe3cd4d71f141", - "last_commit": "b9b52f9c", + "last_commit": "b9b52f9", "name": "crates/aien-allen-profile", "purpose": "ALLEN persona profile v1: versioned, user-editable profile bound to the engaged ALLEN identity; " }, { "digest": "f5571bcc0bb95b44dd8bf8f0b47ded3bcb1e1ee76c81b4ff5edb87ac237d66d8", - "last_commit": "f8eea420", + "last_commit": "f8eea42", "name": "crates/aien-cli", "purpose": "" }, { "digest": "25fd9f8afbea19702750f8ddba2ef4aeafc5bc4515aa695340a5af6931989c03", - "last_commit": "8cca8020", + "last_commit": "8cca802", "name": "crates/aien-scheduler/src/bin", "purpose": "" }, { "digest": "a0a981f6168d15e43a6b036c3aa82d44547aadfc3b6c62139a5a3afab0644514", - "last_commit": "8cca8020", + "last_commit": "8cca802", "name": "crates/spark-cockpit-rs", "purpose": "" }, { "digest": "f38ccede5d794040581cf48d409424c923d705a2fc206140b52691ef7fb186ef", - "last_commit": "deff6f7a", + "last_commit": "deff6f7", "name": "scripts", "purpose": "" }, { "digest": "75e439cb2ae120e1807af894b70b6454971028f5527f56451f9d6558f503c88b", - "last_commit": "fa73e778", + "last_commit": "fa73e77", "name": "skills", "purpose": "" }, { "digest": "b95ea9baa15ce7ddc69a3ef4dc25cdcccba52ec1f5fd99e19f0f24af9181a306", - "last_commit": "247e0927", + "last_commit": "247e092", "name": "tools/aien-local-ci", "purpose": "Local CI sandbox exec contract (C8), aien-run user/caches install, Forgejo trigger hook, health " } ], "children_root": "sha256:7d8cf5937a13dd896fe511aee80a0873f4917a0a5eb07cbe0fe3690c0c878cbb", - "compiled_at": "2026-10-16T15:26:31Z", + "compiled_at": "2026-10-16T18:41:20Z", "compiler": "crumb-compile/1", - "digest": "sha256:5c3262fb97f4051b7780f820f039da497a16ac9d1cf8d077e593d3b2e786ba89", + "digest": "sha256:ac1e99fc11afe5487f84f75e29cabf2c798acb8eec3bc2a641c1a10438934305", "evidence_files": 1578, "evidence_root": "sha256:135e27a1a0704c4a8371cd2c1eb555f2b5beec61c212949b029a5457418a7160", "files": 5889, - "generated_at_commit": "228809d2f65ded6e8bbc011bc817209d82255bb5", - "source_tree": "sha256:7bdcd0cf99ffd24054c17de9287bf4506d8059c855d566eab42a2280024ec2a2" + "generated_at_commit": "06203f89f4185a67568c4bb414f4f281f442a6f3", + "source_tree": "sha256:179fe9f154ea7186adc871469bd1e5d0c934c45600e7a16aaf05ff1e91197bb4" } }, "history": [],