Files
turso/tlaplus
Pekka Enberg 50f3a217fb core/wal: fix multiprocess database file reader race
When the WAL is fully checkpointed, a new read transaction skips the WAL
and reads pages only from the database file. In multiprocess mode, other
processes could not see such a reader: it held read lock 0, but that
lock exists only inside its own process, and the reader did not register
in the shared reader table in the .tshm file. A checkpoint in another
process could therefore copy newer WAL frames into the database file
while the reader was still using its older snapshot, and the reader saw
a mix of old and new pages. The concurrent simulator hit this as FTS
index corruption.

Preston Thorpe's commit 121d5e5db, which added multiprocess WAL support,
did register these readers in the shared reader table, one slot per
reader. The follow-up commit 94c912fb0 removed the registration a day
later because many concurrent readers used up all the slots and new
connections failed with Busy. The test for that case was changed to
assert that these readers register nothing, so the missing protection
went unnoticed.

This change registers them again, but all such readers in one process
share a single slot, so they no longer run out of slots. While any
process has one registered, checkpoints in every process stop at the
current backfill point. This is the cross-process version of the read
lock 0 that a checkpoint already takes within one process, and it
matches SQLite. WAL restarts are still allowed, as in SQLite, because
these readers never read the WAL. Blocking restarts as well would make
the WAL grow without bound, because the restart before a write only runs
when the writer itself is one of these readers. A failed registration
now also releases the local read lock, which the WAL reader path used to
leak.

tlaplus/multiprocess/MultiprocessWal.tla models this coordination: the
local read locks, the shared reader table, the snapshot recheck when a
read begins, checkpoints that copy frames one at a time, and the restart
before a write. TLC checks every state of two processes with two
connections each and up to three commits (32 million states) and finds
no case where the database file holds a newer version than an active
reader's snapshot or where the WAL restarts under a WAL reader. With
database file readers left out of the shared reader table, TLC finds
this bug in 13 steps.

Aristo intents on the coordinator state the same rules at the code
sites that enforce them: the checkpoint safe frame stays below every
reader in every process, database file readers register in the shared
table and recheck the snapshot afterwards, the restart ignores database
file readers, and frame zero in a reader slot marks a database file
reader. One assume records that the tshm mapping is shared by every
process on the host.

testing/antithesis/multiprocess is a new Antithesis workload where
every driver is its own process and opens the database with the
multiprocess WAL feature. Writers stamp every row with the commit's
generation, so every commit rewrites every page. Readers pin a snapshot
by reading one row, then spawn a child process that commits and runs a
passive checkpoint under that snapshot, and then scan the whole table,
so the later pages come from the database file instead of the reader's
page cache. The child makes the cross-process overlap happen on every
read instead of waiting for the test composer to schedule it, which it
rarely does because it throttles parallel drivers on CPU use. A
checkpoint that copies newer frames under the snapshot shows up as rows
from two generations in one scan. Without the fix a local 30 second run
reports torn reads dozens of times; with the fix a 60 second run is
clean.

Tests: core/multiprocess_tests.rs
- subprocess_db_file_reader_stops_checkpoint_in_other_process
- subprocess_db_file_reader_allows_wal_restart_but_stops_backfill
- write_after_full_checkpoint_restarts_wal
Tests: testing/antithesis/multiprocess

Fixes #8648
2026-09-14 18:22:30 +03:00
..