ef8d682200
Every read-write SessionDB open issued three writes that usually change nothing, and a write statement takes the database write lock even when it matches no rows: - _ensure_db_file_generation's INSERT OR IGNORE into state_meta. The stamp is minted once per FILE, so every open after the first inserted nothing. - the NULL-`active` heal, UPDATE messages SET active = 1 WHERE active IS NULL, which matches nothing on a healthy database. - the fts_storage_version stamp, which re-wrote the same value on every open of an already-optimized database. The connection is opened with timeout=1.0, so each blocked write costs a full busy timeout while a sibling process holds the write lock, and the open path's patience loop can ultimately give up and raise. Gate all three on a read. Measured on a real 99 MB state.db (239 sessions, 6470 messages) with a sibling holding the write lock: 2117-2136 ms -> 3.6-7.1 ms. On an already-optimized database the unpatched open does not merely stall, it raises `database is locked`; patched it completes in 3.7-6.8 ms. With a sibling running 200 ms write transactions in a loop (n=20 opens): p50 894.6 -> 9.6 ms, p90 1094.7 -> 13.9 ms. A settled database now issues zero main-database write statements to open. The reads cost nothing measurable: the state_meta probe is a primary-key seek (2.0 us), the messages probe is 1.7 us on the modern NOT NULL column (unsatisfiable constraint, short-circuited) and 0.4-0.6 us at 300k rows on a legacy default-less column via the partial index that already exists for exactly this predicate. An uncontended open is unchanged. Semantics are preserved. INSERT OR IGNORE still resolves the first-opener race inside SQLite and racers still converge on the winner's token via the re-read; the application_id gate and the PASSIVE-only checkpoint are untouched; the heal is still considered on every startup, as #60108 deliberately made it, with only the write now conditional on a read proving there is something to repair. Read-first also fixes a correctness bug. Under contention the generation block was abandoned by its `except sqlite3.Error` handler, so a process ended up with no generation token at all even though the value was already on disk and a plain read would have returned it -- and that token feeds the deleted-WAL and replaced-file guards added by #101221. The heal's `except OperationalError: pass` likewise skipped the repair silently, so the unconditional form did not even deliver the unconditional repair it advertised whenever it mattered most. The probe deliberately does not use INDEXED BY: that hint raises OperationalError("no query solution") against the modern NOT NULL column, and the existing handler would swallow it, disabling the repair forever.