Skip to content

fix(storage): concurrent first opens of a new database all succeed (v0.4.0 RC) - #31

Merged
akosidencio merged 2 commits into
devfrom
fix/storage-concurrent-first-open
Nov 27, 2026
Merged

akosidencio merged 2 commits into
devfrom
fix/storage-concurrent-first-open

Conversation

@akosidencio

Copy link
Copy Markdown
Member

Found during the v0.4.0 release gate. The first CI attempt on dev failed on macOS for both 2044163 and b4cf1e2 (they went green on rerun).

  • concurrent_first_open_applies_the_migration_once failed with ForeignDatabase { application_id: 0 }.
  • events_committed_by_another_daemon_stream_live_and_resume_after_restart failed with "stream open". Two daemons start on one new data dir, and the one that loses the race runs memory-only.

Root cause (reproduced: 8 concurrent openers, 18–25 failures per 400 rounds)

  1. Split identity snapshot. page_count, application_id, user_version and the table count were read in four separate statements. An opener could see another process's unstamped page 1 (written by PRAGMA auto_vacuum) and then its new tables, and refuse TailFlow's own file as foreign. They are now read in one statement, so one snapshot. Within a snapshot, tables only ever appear together with the stamp.
  2. journal_mode = WAL ignores the busy timeout. Leaving rollback mode needs an exclusive lock. While another connection holds the file, SQLite returns SQLITE_BUSY at once without calling the busy handler. It is now retried on BUSY/LOCKED with a 1→50 ms backoff, for up to BUSY_TIMEOUT.
  3. The newer-schema refusal was snapshot-only (from review). If a newer build migrated the file between the identity check and migrate(), this build wrote into a newer database. migrate() and each migration transaction now re-check, and return NewerSchema without writing.

Evidence

  • concurrent_first_open_applies_the_migration_once now runs 60 rounds of 8 openers released together by a barrier, and checks the final version and journal mode.
  • Mutation checks:
    • Without the WAL retry, the test fails with "database is locked".
    • With the WAL retry but the old split reads, the 400-round probe still hits ForeignDatabase.
    • Without the re-check, a_schema_that_becomes_newer_before_migration_is_refused fails.
  • Full fix: 0 failures in 3 × 400 rounds (9,600 concurrent opens).
  • Local (macOS x86_64): cargo fmt --check, cargo +stable clippy --all-targets --all-features -D warnings, and cargo test --workspace --locked (501 passed, 0 failed, before the third commit). tailflow-storage: all pass after it.

No schema or file-format change; still v4.

Two daemons starting together on a new data directory could fail to open
the shared database, and the loser ran memory-only. Reproduced with 8
concurrent openers: 18-25 failures per 400 rounds. This is the cause of the
intermittent macOS CI failures in concurrent_first_open_applies_the_migration_once
and events_committed_by_another_daemon_stream_live_and_resume_after_restart.

Two causes:

- The identity check read page_count, application_id, user_version and the
  table count in separate snapshots. An opener could see another process's
  unstamped page 1 and then its new tables, and refuse TailFlow's own
  database as ForeignDatabase. The four values are now read in one
  statement, so one snapshot: tables are only ever visible together with
  the application id stamp.
- PRAGMA journal_mode = WAL needs an exclusive lock to leave rollback mode
  and returns SQLITE_BUSY at once, without calling the busy handler, while
  another connection holds the file ("database is locked"). It is now
  retried on BUSY/LOCKED with a short backoff for up to BUSY_TIMEOUT.

The regression test now runs 60 rounds of 8 openers released together by a
barrier and checks the final schema version and journal mode. It fails
without the WAL retry. With the fix: 0 failures in 3 x 400 rounds.
The NewerSchema check ran on the identity snapshot only. If a newer build
migrated the same new file past this build's schema between that check and
migrate(), this build returned Ok and wrote into the newer database,
breaking the rule that newer databases are never modified.

migrate() and each migration's write transaction now refuse a user_version
above the newest migration this build knows, without writing. Found in the
v0.4.0 release-candidate review.
@akosidencio
akosidencio merged commit 9da9018 into dev Nov 27, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant