Skip to content

Consolidate SqliteWriter construction to a single per-process site #125

Open

Description

@utof

Post-Batch-C (commit 3ff4c45), production code constructs multiple SqliteWriter actors per process:

  • crates/cli/src/main.rs:233 — build_container
  • crates/cli/src/main.rs:314 — watch event-handler builder
  • crates/cli/src/main.rs:392 — scan sentinel-migration closure
  • crates/desktop/src/lib.rs:151 — Tauri .setup() watch writer
  • crates/desktop/src/lib.rs:234 — build_container
  • crates/desktop/src/commands.rs:{392, 513, 644} — _inner legacy helpers (deferred separately)

Each SqliteWriter::start opens a writable rusqlite::Connection and spawns an OS thread. The Batch-C design doc and the writer module doc state "one writer actor per process, owning the sole writable Connection for its lifetime." Multiple writer threads hold multiple writable connections, even though SQLite WAL serialises them at the OS lock level.

Why it's currently not a defect

Each extra callsite exists because the current construction order needs an already-constructed repo before the AppContainer is built:

  • CLI build_watch_db_handler builds a DbEventHandler that needs a SqliteFileRepository that needs a writer. Happens before AppContainer::new.
  • CLI scan sentinel closure is handed to ScanUseCase and executes on the scan worker thread before AppContainer is visible to it.
  • Desktop Tauri .setup() builds the watch event handler before AppContainer exists.

The WHY comments on each callsite acknowledge this.

What to investigate

  1. Restructure so that AppContainer::new is the single construction site that calls SqliteWriter::start exactly once, and the resulting Sender<WriteCmd> + ReadPool flow out to every caller (including the sentinel closure, watch event handler, _inner helpers).
  2. Alternatively: amend the design doc to permit auxiliary writer threads per logical component, with the proof-of-correctness argument that WAL + BEGIN IMMEDIATE + busy_timeout enforces the invariant at the SQL layer.

Acceptance

  • rg 'SqliteWriter::start\(' crates/{cli,desktop}/src/ returns exactly two production hits (one per binary), both inside AppContainer::new or the wiring layer immediately adjacent.
  • Or: design doc amended to document the multi-writer posture and explain the WAL-based serialisation contract.

Not blocking any future batch; surfaced by a code review.

Activity

  1. added
    status/needs-research
    on Apr 22, 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-research

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions