Skip to content

Intermittent deadlock: perima CLI hangs at startup under concurrent cargo test load #122

Open

Description

@utof

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 the perima binary.
  • Subprocess state after 13+ minutes:
    • 15 threads total (main + tokio workers + SQLite writer actor + auxiliaries).
    • 14 threads in futex_do_wait, 1 in ep_poll (tokio reactor parking).
    • 0% CPU, no stdout/stderr output.
    • /proc/<pid>/cmdline: perima ls --json.
  • Parent cargo test --workspace --exclude perima-desktop blocked 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 --workspace from 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).
  • wchan values per thread:
    • main: futex_do_wait
    • 1× ep_poll (tokio reactor)
    • 13× futex_do_wait (tokio workers + sqlite writer thread)

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:

  1. SqliteWriter::start(&db_path, bus) — opens a Connection, runs Refinery migrations synchronously, spawns writer thread.
  2. ReadPool::open(&db_path) — builds r2d2::Pool<SqliteConnectionManager> with SQLITE_OPEN_READ_ONLY, max_size 4.
  3. SqliteFileRepository::new(open_and_migrate(&db_path)?) — opens a fresh Connection, runs Refinery migrations again.
  4. Repeat step 3 for Tag, Metadata, Search repos — four total open_and_migrate calls.

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

  1. Add a unit test to crates/db/src/writer.rs that repeatedly spins up SqliteWriter::start + ReadPool::open in a loop against a shared tempfile-backed DB, exercising the startup interleave. Run with --test-threads=N for various N to catch the race.
  2. Instrument SqliteWriter::start with tracing::debug! spans at every SQLite open + migration boundary.
  3. 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.
  4. 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.
  5. Once the lock cycle is identified, the fix is likely one of:
    • Kill the 4× open_and_migrate redundancy 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 Mutex during build_container so Refinery sniff, pool warm-up, and legacy repo opens serialize.
    • Open the pool connections eagerly inside ReadPool::open (warm them before build_container returns), so the first get() doesn't race.

Mitigation already landed

  • CLAUDE.md now 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-nextest with --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.

References

Activity

  1. added
    type/bug
    status/needs-research
    on Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status/needs-researchtype/bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions