Repository navigation
Description
Observed behavior
During the writer-actor + read-pool refactor (commit 8ba3f04 on architecture-audit/v0.6.x), the ls_output::ls_json_deserializes integration test (crates/cli/tests/ls_output.rs) hung indefinitely.
- Test harness:
std::process::Command::new(bin()).arg("ls").arg("--json")...spawning theperimabinary. - Subprocess state after 13+ minutes:
- 15 threads total (main + tokio workers + SQLite writer actor + auxiliaries).
- 14 threads in
futex_do_wait, 1 inep_poll(tokio reactor parking). - 0% CPU, no stdout/stderr output.
/proc/<pid>/cmdline:perima ls --json.
- Parent
cargo test --workspace --exclude perima-desktopblocked forever waiting on the test subprocess to exit. - Reproduced once locally; did NOT reproduce on a serial re-run of the same test. Classic race-condition signature.
When it triggers
Seen during concurrent cargo invocations sharing target/debug:
cargo test --workspace(the primary command),- plus a parallel
cargo clippy --workspacefrom another shell, - plus incidental
cargo check -p <crate>invocations.
Lock contention on target/debug is known to serialize cargo processes; what's new is that one of the spawned test binaries wedges at startup instead of waiting.
Stack data on hand
- No gdb backtrace captured: the process was killed before we could attach (default ptrace scope requires same-ancestor or sudo).
wchanvalues per thread:- main:
futex_do_wait - 1×
ep_poll(tokio reactor) - 13×
futex_do_wait(tokio workers + sqlite writer thread)
- main:
Hypothesis — where the race likely lives
crates/cli/src/main.rs::build_container on the architecture-audit/v0.6.x branch performs this startup sequence:
SqliteWriter::start(&db_path, bus)— opens aConnection, runs Refinery migrations synchronously, spawns writer thread.ReadPool::open(&db_path)— buildsr2d2::Pool<SqliteConnectionManager>withSQLITE_OPEN_READ_ONLY, max_size 4.SqliteFileRepository::new(open_and_migrate(&db_path)?)— opens a freshConnection, runs Refinery migrations again.- Repeat step 3 for
Tag,Metadata,Searchrepos — four totalopen_and_migratecalls.
This means 5 independent Connection handles opened against the same SQLite file at startup, each running Refinery's migration sniff. Refinery takes a write lock during migration-history check. The r2d2 pool is lazy — first get() opens read-only connections that must attach to the -shm file. With WAL mode plus five concurrent migration sniffs racing against the writer thread's initial state, a deadlock is plausible under specific interleaving.
Candidate failure mode: reader tries to open against a DB file whose -wal/-shm are being initialized by another connection that's itself waiting on the migration history lock. Combined with tokio's startup (async work scheduled in #[tokio::main]), a thread can end up parked on a futex held by a thread parked on another futex — classic lock-order inversion.
Important: the reviewer subagent re-ran the suite and observed green; the intermittence is real, which is worse than a deterministic failure. This class will reappear under CI load if left unaddressed.
Proposed investigation path
- Add a unit test to
crates/db/src/writer.rsthat repeatedly spins upSqliteWriter::start+ReadPool::openin a loop against a shared tempfile-backed DB, exercising the startup interleave. Run with--test-threads=Nfor various N to catch the race. - Instrument
SqliteWriter::startwithtracing::debug!spans at every SQLiteopen+ migration boundary. - Reproduce the hang under
cargo nextest run --slow-timeout 30s(see issue for nextest adoption) so the test self-kills and dumps stacks instead of silently hanging. - Capture gdb backtrace of a hung subprocess:
sudo sysctl kernel.yama.ptrace_scope=0; gdb -p <PID> -batch -ex \"thread apply all bt 30\"— keeps the process alive long enough to see which futex each thread is parked on. - Once the lock cycle is identified, the fix is likely one of:
- Kill the 4×
open_and_migrateredundancy entirely (subsequent batches C-3 through C-6 migrate each repo to the writer+pool; the race window shrinks to zero then). - Explicit startup barrier: hold a single
Mutexduringbuild_containerso Refinery sniff, pool warm-up, and legacy repo opens serialize. - Open the pool connections eagerly inside
ReadPool::open(warm them beforebuild_containerreturns), so the firstget()doesn't race.
- Kill the 4×
Mitigation already landed
CLAUDE.mdnow documents the symptom and detection recipe (strace +/proc/<pid>/task/*/wchan) so the next person recognizes it instantly.- Issue Adopt cargo-nextest with per-test slow-timeout for workspace tests #121 tracks adoption of
cargo-nextestwith--slow-timeout, which would have surfaced this in minutes rather than leaving the test to hang indefinitely.
Priority
High for the architecture-audit branch — reappears every time we run the full test suite on a busy system; will reliably fail CI once CI load exercises the interleave. Should be investigated before merging architecture-audit/v0.6.x to main.
Activity