Skip to content

Write transactions: deferred snapshots can surface SQLITE_BUSY_SNAPSHOT as 500s under concurrent writes #27

Closed

Description

@TusanHomichi

Problem or outcome

Every write service follows check-then-write inside a deferred sqlx transaction (pool.begin()): enrollments::enroll, programs::publish_version, lifecycle::record_enrollment_event / record_phase_event, assignments::create, and training_sessions::create / update_open / close. Under the WAL pool, two concurrent writers can both pass their validation reads on separate snapshots; the first commits, and the second's upgrade to a write then fails with SQLITE_BUSY_SNAPSHOT, which escapes through anyhow as an HTTP 500 instead of the typed refusal (e.g. interval_overlap, already_enrolled) or a clean retry. The database invariants still hold — nothing corrupts — but the caller gets an untyped error for a domain-expressible conflict.

Exact evidence: raised by review on #26 against training_sessions::create's overlap pre-check (thread sibling, commit 4c52a87); the same shape exists in every module listed above, including code merged in #24 and earlier — it is the repository's write pattern, not one slice's defect.

Scope

In scope:

  • one decided mechanism applied across all write services: begin write transactions immediately (write reservation before validation reads), or a bounded busy retry that re-runs validation on SQLITE_BUSY_SNAPSHOT, or both
  • a concurrency test that provokes the race and proves the loser receives the typed refusal

Out of scope:

  • changing any validation semantics or refusal vocabulary

Acceptance criteria

  • two concurrent conflicting writes yield one success and one typed domain refusal, never a 500
  • the chosen mechanism is uniform across write services and documented (storage or a small transaction helper)
  • all repository gates green

Expected proof

  • cargo test --workspace including a new concurrent-writers test
  • cargo fmt --check, cargo clippy --workspace --all-targets -- -D warnings

Related roadmap, decisions, or issues

Activity

  1. TusanHomichi commented on Aug 29, 2026

    @TusanHomichi
    MemberAuthor

    Direction adopted on the slice-3 draft paths (PR #29, commit 98b64e0): storage::write_tx begins an immediate (write) transaction, so check-then-write validates against the committed state and a concurrent writer waits out the busy timeout instead of failing its read snapshot — typed refusals stay typed under concurrency. That is the immediate-write-reservation option this issue weighs, now with one owner in storage.

    Remaining scope here: retrofit the earlier deferred write paths (enroll, publish, lifecycle events, assignments, sessions) onto the same helper, plus the concurrency test the issue calls for.


    Generated by Claude Code

  2. TusanHomichi commented on Aug 30, 2026

    @TusanHomichi
    MemberAuthor

    Status and plan, following the Milestone 3 completion decision (2026-08-29).

    #31 fixed one half of this class at the cause: refusal paths that returned while their BEGIN IMMEDIATE transaction was live only queued the rollback, so the write lock briefly outlived the decision — and SQLite refuses a deferred writer's read-to-write promotion immediately, without consulting the busy timeout (verified: 0.000s versus the full 5s wait for a fresh BEGIN IMMEDIATE; this is exactly the CI-only database is locked failure that hit programs::create_program). Every post-write_tx refusal now ends through storage::refuse, which awaits the rollback, and a zero-busy-timeout probe test pins the property.

    Remaining scope here is the other half: the older deferred check-then-write paths (programs, users, enrollments, assignments, the training_sessions close-time coverage check deferred from #26) still promote a read transaction to a write mid-flight, so under real concurrent writers they fail immediately with an untyped 500 instead of waiting out the busy timeout and resolving as their typed refusals. The retrofit is now mechanical: open with storage::write_tx, keep the checks where they are, end refusals through storage::refuse.

    Owner-approved plan: a standalone maintenance PR before or early in Milestone 4 (#32), one module-by-module pass with a concurrency test per retrofitted path.


    Generated by Claude Code

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions