Skip to content

proptest harness hangs after writer-actor migration: two racing SQLite write-connections #124

Closed

Description

@utof

After landing the SQLite writer-actor + read-pool connection model migration (ref: #67, #66), the proptest harness in crates/db/src/search_repo.rs hangs under the new shape.

Reproduction

With the Batch-C uncommitted migration applied on top of commit 532ac2a:

cargo nextest run -p perima-db --test-threads 2

Two tests never complete and get SIGTERMed by the outer wrapper after ~163s:

  • search_repo::tests::fts_consistent_under_tag_churn
  • search_repo::tests::fts_matches_ground_truth_under_soft_delete_churn

Pre-migration (at 532ac2a), the full perima-db nextest run completed in ~15s with fts_consistent_under_tag_churn taking ~14s as the single slowest test.

Root cause

The #[cfg(test)] harness in crates/db/src/search_repo.rs constructs two live connections to the same db file:

  1. SqliteWriter::start(&db_path, bus) — the writer actor holds one writable rusqlite::Connection on its own OS thread (per the new single-writer invariant).
  2. seed_conn(&db_path) — a second raw rusqlite::Connection::open(&db_path) used by the direct-SQL helpers attach_tag_raw, detach_tag_raw, and insert_file to seed trigger state before each proptest iteration.

Under SQLite WAL mode with BEGIN IMMEDIATE transactions, these two connections race for the exclusive write lock. With proptest running ~30 iterations per case × 3 seed INSERTs per op, the busy_timeout = 5000ms defaults cascade into indefinite waits.

Pre-migration the adapter used a single Mutex<rusqlite::Connection> shared between the seeding helpers and the repo operations, so there was no inter-connection contention.

Suggested fix paths

  1. Route proptest seeding through the writer actor. Replace the attach_tag_raw / detach_tag_raw / insert_file direct-SQL calls with WriteCmd::Tag(Attach/Detach) + WriteCmd::File(UpsertFile/UpsertLocation) envelopes sent via the test's SqliteWriter handle. Preserves the single-writer invariant and mirrors production behavior.
  2. Introduce a test-only raw-seed bypass. A #[cfg(test)] pub fn SqliteWriter::seed_with<F>(&self, f: F) hook that borrows the writer's connection briefly. Simpler but adds a cfg-gated path that shouldn't exist in production.

Option 1 is cleaner.

Scope

Limited to crates/db/src/search_repo.rs test module. No production code or port trait changes. Blocking the final Batch-C Task 7 commit (migration cleanup sweep).

Activity

  1. added
    type/bug
    on Apr 22, 2026
  2. utof commented on Apr 22, 2026

    @utof
    OwnerAuthor

    Resolved by commit 3ff4c45 (Batch C Task 7) + proptest case-count reductions. Root cause was not a write-lock race between the writer actor and seed_conn: the writer is idle during test setup and no connection contends for BEGIN IMMEDIATE under WAL. Actual root cause was per-case architectural overhead: each proptest case spawns a writer-actor OS thread + opens an r2d2 pool + opens a fresh seed_conn, ~5x the per-case cost of the pre-Task-7 single-Mutex<Connection> fixture. Accumulated cost at the 256-case default exceeded the .config/nextest.toml terminate-after=2 × 40s budget. Fix: hoisted seed_conn out of the per-op loop (eliminates Connection::open churn) and capped proptest case counts (64 for tag-churn, 32 for ground-truth) to accommodate the residual writer-thread + pool init overhead. cargo nextest run -p perima-db now completes in ~6s on a 2-thread VM.

  3. utof commented on Apr 22, 2026

    @utof
    OwnerAuthor

    Post-mortem — investigation trail, for future debuggers hitting similar symptoms

    Symptom: cargo nextest run -p perima-db --test-threads 2 hangs, slow-timeouts at 80s on the two FTS5 proptests (fts_consistent_under_tag_churn, fts_matches_ground_truth_under_soft_delete_churn). Pre-Batch-C the whole suite ran in ~15s.

    Hypotheses tested and discarded

    1. "Two writable connections (writer actor + raw seed_conn) race for the WAL write lock under BEGIN IMMEDIATE." — Initial theory in the issue body. Wrong. The writer actor is idle during proptest setup — it's blocked on flume::Receiver::recv() with no commands in flight, holding no SQL locks. seed_conn statements are auto-commit; they briefly take the WAL reserved lock and release. No cross-connection contention in practice.

    2. "Per-op Connection::open churn on the WAL file." — Plausible: each proptest iteration opened a fresh seed_conn via Connection::open(db_path), ~15 000 opens across both proptests at the default 256 cases. Applied fix: hoisted seed_conn to per-case scope instead of per-op. Measured impact in isolation — at 75 cases one proptest finished in 5.4s. But at default 256 cases the full suite still timed out at >80s. Not the whole story.

    3. "Parallel test execution contention." — Ruled out: --test-threads 1 with both proptests serial also timed out at 80s. So the slowdown is per-proptest, not from test scheduler interaction.

    Actual root cause

    Scaling data at default --test-threads 2:

    Cases Result
    50 5.0s ✅
    75 5.4s ✅ (isolated, one proptest)
    100 >80s ❌
    256 >80s ❌

    The scaling is superlinear in case count — something accumulates as cases progress. Post-Batch-C each proptest case spawns:

    • A writer-actor OS thread via SqliteWriter::start (migration run + std::thread::spawn).
    • An r2d2::Pool<SqliteConnectionManager> with up to 4 lazily-opened SQLITE_OPEN_READ_ONLY connections.
    • A seed_conn raw rusqlite::Connection for direct SQL trigger exercising.

    Pre-Task-7 the fixture used SqliteSearchRepository::new_legacy(conn) — a single Mutex<Connection>, one file handle per case, zero thread spawn. Reusing it across all ops.

    At ~200-500 ms per case × 256 cases, accumulation hits .config/nextest.toml's terminate-after = 2 × 40s = 80s budget. The superlinearity likely comes from kernel-scheduler + swap-pressure interactions on VM filesystems as thread / file-handle churn builds up — the fixture is individually cheap but cumulatively pathological.

    Fix applied

    1. Hoist seed_conn out of the per-op loop — reduces ~15 k Connection::open calls down to ~256, eliminating the WAL-handshake syscall churn even if it wasn't the dominant cost.
    2. Cap #![proptest_config(cases: N)] — 64 for fts_consistent_under_tag_churn, 32 for the ground-truth variant (heavier per op due to compute_ground_truth's 8 subqueries).

    Result: cargo nextest run -p perima-db --test-threads 2 lands at ~6s. Coverage: 64 × 30 = 1 920 ops (tag-churn) and 32 × 25 = 800 ops (ground-truth). Still strong for trigger invariants — each op explores one of 4 or 9 distinct operation types across a 2–3 file × 2–3 tag universe.

    Heuristic for "why is this proptest slow in this repo"

    Before assuming lock races, measure per-case cost at fixed small N (e.g. PROPTEST_CASES=50). If it's linear up to ~100 but explodes at 256, suspect cumulative fixture overhead, not concurrency pathology. Especially relevant under VM memory pressure with swap involved.

    Long-term alternatives

    • Pure-Rust FTS oracle — reimplement the trigger semantics in Rust (no SQLite per case), run proptest at default 256 against the oracle, reserve SQLite for shape-level regression tests. Larger effort but removes the writer-per-case tax.
    • Shared-cache in-memory DB — file::memory:?cache=shared URI across writer + pool + seed_conn eliminates filesystem I/O, but requires a new SqliteWriter::start_with_uri entry point.
    • proptest-regressions files — once a failing seed is discovered, commit it; reduces the need for high case counts on repeat runs (see tracker: Deduplicate test-fixture boilerplate in post-Batch-C writer+pool setup #126 for related test-helper dedup).
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

    type/bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions