mirror of
https://github.com/allankoechke/turso.git
synced 2026-11-30 11:12:58 +00:00
`checkpoint()` on a partial-sync database failed deterministically whenever the WAL was empty: - a checkpoint with frames in the WAL succeeds, and its final `TRUNCATE` truncates `-wal` to 0 bytes (`truncate_log`, `core/storage/wal.rs`) - every subsequent checkpoint while the WAL is empty fails with `I/O error (pread): unexpected end of file` `checkpoint()` → `checkpoint_passive()` begins by reading the WAL salt, and `read_wal_salt` (`sync/engine/src/database_sync_operations.rs`) is written for a WAL file that may be shorter than one 32-byte header: its completion callback zero-fills on a short read and returns "no salt". The file still exists after a truncating checkpoint, so `try_open` returns it and the header read runs against 0 bytes. Partial sync is the only mode that swaps the IO backend — `persistent_io(partial)` hands out `SparseLinuxIo` instead of `PlatformIO` (`sync/sdk-kit/src/rsapi.rs`, same in `bindings/javascript/sync/src/lib.rs`) — and `SparseLinuxFile::pread` used `read_exact_at`, which returns `UnexpectedEof` where a short read was expected. On Linux `libc::pread` never produces `UnexpectedEof`, so the reported error string could only come from this one call site. The fix makes `SparseLinuxFile::pread` report the number of bytes read and stop at end of file, matching `UnixFile::pread` in `core/io/unix.rs`. Both callers already handle a short read: `read_wal_salt` treats it as "no salt", and core's page reads treat a zero-length read as an absent page. Two clarifications on the original report: - Full-sync replicas were never affected, because they use `PlatformIO`. - The failure does not depend on unmaterialized pages. The engine-level test bootstraps a prefix covering the whole database — zero holes — and still reproduced it, since the failing read is on the `-wal` file and not on the database file. Materializing the database cannot avoid it. Reported by a user against both 0.7.2 and 0.8.0-pre.10: `checkpoint()` succeeds while the WAL holds frames and then fails with `I/O error (pread): unexpected end of file` on every checkpoint that follows, which leaves a partial-sync replica unable to ever fold its WAL twice in a row. Every existing partial-sync test — Rust bindings and TS alike — opens the replica at `:memory:`, which maps to `MemoryIO`, so the entire partial-sync suite ran on the one IO backend that is not affected. The new tests all use a real file path. Added coverage: | Test | Layer | |---|---| | `partial sync (checkpoint with empty WAL)` in `bindings/javascript/sync/packages/native/promise.test.ts` | e2e through the JS SDK against a local sync server | | `test_sync_partial_checkpoint_with_empty_wal` in `bindings/rust/src/sync.rs` | e2e through the Rust driver | | `partial_sync_checkpoint_succeeds_when_main_wal_is_empty` in `sync/engine/src/database_sync_engine.rs` | sync engine, over `SparseLinuxIo` | | `full_sync_checkpoint_succeeds_when_main_wal_is_empty` (control) | same sequence over `PlatformIO`, passes before and after | | `read_wal_salt_of_empty_wal_file_is_none_on_every_io` | pins the per- backend behaviour of the failing read | | `pread_reports_short_read_at_end_of_file` in `sync/engine/src/sparse_io.rs` | 32-byte reads of 0-, 10- and 32-byte files | Each of those fails before the fix and passes after, except the full- sync control, which exists to show the failure is partial-sync-specific. The TS test was verified in both directions by rebuilding the napi module with `read_exact_at` restored and confirming the exact reported error. The first commit is the reproductions on their own, so the failure is visible in isolation before the fix. Tests: `turso_sync_engine` 100 pass; `turso --features sync` 35 pass, 1 pre-existing ignore; `promise.test.ts` 34 pass, 5 skipped (the pre-existing `skipIf(LOCAL_SYNC_SERVER)` tests). Note for reviewers: `94bc9b2` carries unrelated churn in two checked-in generated files, produced by running the local debug napi build — 46 version-string lines in `index.js` (`0.8.0-pre.3` → `0.8.0-pre.10`, the checked-in file was stale) and a missing `filePaths(): Array<string>` declaration in `index.d.ts`. Happy to strip both from the branch if preferred. Driven with Claude Code (Opus 5). It located the root cause from the bug report alone, wrote the reproductions at the sync-engine, Rust-driver and JS-SDK layers, wrote the `pread` fix, and verified each test fails without the fix and passes with it. I reviewed the diff and committed it. Two model conclusions were checked against evidence rather than taken on trust: that the failing read is `read_wal_salt` (confirmed by tracing showing the second checkpoint dying between two adjacent log statements whose only intervening IO is that read), and that the bug is independent of page materialization (confirmed by a hole-free repro, which contradicted the "materializing fixes it" part of the report). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Reviewed-by: Pekka Enberg <penberg@iki.fi> Closes #8930