mirror of
https://github.com/allankoechke/turso.git
synced 2026-11-30 11:12:58 +00:00
An Antithesis stress run (WAL mode, two threads sharing one database, no
disk faults) failed PRAGMA integrity_check with index entries missing
from their tables and two pages that no B-tree used. Committed writes
were partly lost: one INSERT kept its table row but lost its index
entry, another kept its index entry but lost its table row.
The damage was done by a FULL checkpoint. It reported every frame of
the WAL as copied (log=23 checkpointed=23), but the database file it
left behind still had the old content of every page whose newest
version was in WAL frames 1 to 6. Every page with a newer frame had
been copied. The next WAL restart then discarded frames 1 to 6, so the
committed changes in them were gone from both the WAL and the database
file.
The checkpoint reads nbackfills from a WAL snapshot taken before it
acquires the checkpoint locks, and copies frames starting from
nbackfills + 1. The upper end of the range, the maximum safe frame, is
read after the locks are held. If, between the two reads, another
connection copies the whole WAL, restarts it and commits new frames,
the checkpoint copies frames old_nbackfills + 1 to new_max_frame of the
new WAL generation. It skips the first old_nbackfills frames of the new
generation without an error and then publishes nbackfills as
new_max_frame, so from then on those frames count as copied.
In the failing run, one thread published nbackfills = 6 with a PASSIVE
checkpoint and then started a FULL checkpoint, which read
nbackfills = 6. Before the thread took the checkpoint locks, the other
thread checkpointed the WAL, restarted it and committed 23 new frames.
The FULL checkpoint then copied frames 7 to 23 only.
The mismatch was introduced by commit 7c027fed8c ("Keep
should_checkpoint logic for now until greater checkpointing is fixed"),
which set the first frame to copy to nbackfills + 1 using the value
read before the checkpoint lock was taken, while the maximum safe frame
was read after it. Later changes to the checkpoint kept that order.
Fix this by reading nbackfills after the checkpoint locks are acquired,
so both ends of the copy range come from the same state. The check
before the locks that decides whether there is anything to copy still
uses the earlier read.