A WAL snapshot is one C sqlite3_backup_step (pages=-1, so foreign writes
cannot restart it). Python runs signal handlers only on the main thread
between bytecodes, so the Desktop's deadline SIGTERM was serviced only once
the whole copy had finished: on a 1.5 GB db, 10.8 s after the signal. The
copy now runs on a worker thread (sqlite releases the GIL) while the main
thread waits in short joins, so the handler exits through the staging
cleanup at once (measured 0.05-0.18 s vs 1.3-3.4 s on base). An interrupted
copy leaves its connections to the exiting worker (close() would wait for
the copy), and the staging copy's -journal is removed with it. The rollback
journal size estimate also floors the measured rate so it can never divide
by zero. (itsflownium review on #144894, #1 + small item.)
Review finding "Signal test accepts normal completion": the test timed its
signal by database size, so a fast child could finish first, send_signal
hit an exited process and the empty-staging assertion passed anyway.
The test now holds an exclusive lock on a rollback-journal state.db, so the
child's copy busy-waits until it is signalled whatever the disk speed. It
asserts the child is still running with its staging file present before
sending SIGTERM, then asserts the handler's exit status (128 + SIGTERM)
before checking cleanup. No sleeps: tests.waiting.wait_until.
The staging file is now named before it is created (stamp + pid, created
owner-only by _safe_copy_db, which refuses a symlink), so a signal landing
right after creation still reaches the cleanup; mkstemp left a window
between creating the file and the try block.
Red under mutation (SIGTERM handler removed): returncode -15.
Review finding "Backup blocks live message saves": with
database.journal_mode: delete a one-step backup (pages=-1) holds the
source's read lock for the whole copy, so the live backend's commits wait
for all of it; on slow NFS/SMB that outlasts SessionDB's write patience.
The one-step copy now runs only for WAL sources, where readers never block
writers. A rollback-journal source copies in steps that release the lock
between batches (256 pages first, then sized to ~0.1s at the measured
rate). Each foreign write restarts a stepped copy, so restarts are bounded
(20); a continuously written source then gets one whole-copy hold only
when the measured rate says it lasts <= 0.25s (under the 0.5s activity
write patience), otherwise the snapshot is refused with a message to
retry when no turn runs.
Live (writer process committing every 5 ms, timeout=20): a 392 MiB
delete-mode state.db held one commit 654 ms on the previous head (writer
alone: 33 ms); now the longest commit is 196 ms and the pre-flight refuses
in 5.7 s. 16 MiB delete-mode: snapshot in 0.27 s. WAL unchanged
(0.80 s, longest commit 72 ms).
The Desktop ran the pre-update state.db snapshot through execFileSync with a
fixed 30 s timeout. A healthy 0.9-6 GB database cannot meet that, so every
in-app update died with `spawnSync ... ETIMEDOUT` before the backend stopped,
the Electron main process froze for the whole 30 s, and the killed snapshot
left a full-size .partial behind each time.
The snapshot now runs asynchronously (the caller still awaits it before
stopping the backend) with a deadline of 5 min + size/(5 MiB/s), capped at
30 min, sized from state.db plus its WAL. The Python helper exits through its
cleanup on SIGTERM (the deadline's kill signal) and reclaims staging copies
older than two hours that a hard-killed run (Windows TerminateProcess) could
not remove.
Existing tests: their call sites gained `await` only; assertions unchanged.
Co-authored-by: itsflownium <itsflownium@users.noreply.github.com>
sqlite3's stepped backup (pages=256) restarts from the first page whenever
another connection writes the source database. Desktop runs the state.db
pre-flight with a 30s hard timeout while the backend is still live, so any
active agent turn, cron job or gateway message kept restarting the copy and
the update was cancelled with "state.db pre-flight failed: ... ETIMEDOUT".
Copy all pages in a single backup step instead. In WAL mode the read
transaction does not block writers, and the snapshot stays consistent.
Measured on a 342 MB state.db with a writer committing every 0.5s:
pages=256 timed out after 25s with 49 restarts; pages=-1 finished in 2.9s.