From e28a38abc504f483518f51bd516d64d4dd87eaf4 Mon Sep 17 00:00:00 2001 From: PThorpe92 Date: Fri, 15 Aug 2025 22:40:25 -0400 Subject: [PATCH] Fix wal tag safety issues, and add debug assertion that we are reading the proper frames --- core/storage/pager.rs | 1 + core/storage/sqlite3_ondisk.rs | 3 +++ core/storage/wal.rs | 8 +------- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/core/storage/pager.rs b/core/storage/pager.rs index d1da59d4b..a0f36d749 100644 --- a/core/storage/pager.rs +++ b/core/storage/pager.rs @@ -287,6 +287,7 @@ impl Page { self.get().wal_tag.store(TAG_UNSET, Ordering::Release) } + #[inline] pub fn is_valid_for_checkpoint(&self, target_frame: u64, seq: u32) -> bool { let (f, s) = self.wal_tag_pair(); f == target_frame && s == seq && !self.is_dirty() diff --git a/core/storage/sqlite3_ondisk.rs b/core/storage/sqlite3_ondisk.rs index c11682dbf..36537d024 100644 --- a/core/storage/sqlite3_ondisk.rs +++ b/core/storage/sqlite3_ondisk.rs @@ -912,6 +912,9 @@ pub fn finish_read_page(page_idx: usize, buffer_ref: Arc, page: PageRef) page.get().contents.replace(inner); page.clear_locked(); page.set_loaded(); + // we set the wal tag only when reading page from log, or in allocate_page, + // we clear it here for safety in case page is being re-loaded. + page.clear_wal_tag(); } } diff --git a/core/storage/wal.rs b/core/storage/wal.rs index c4eda17bb..79b7cb75a 100644 --- a/core/storage/wal.rs +++ b/core/storage/wal.rs @@ -1375,12 +1375,6 @@ impl WalFile { shared: Arc>, buffer_pool: Arc, ) -> Self { - let checkpoint_page = Arc::new(Page::new(0)); - let buffer = buffer_pool.get_page(); - { - checkpoint_page.get().contents = Some(PageContent::new(0, Arc::new(buffer))); - } - let header = unsafe { shared.get().as_mut().unwrap().wal_header.lock() }; let last_checksum = unsafe { (*shared.get()).last_checksum }; let disable_checkpoint_cache = @@ -1813,7 +1807,7 @@ impl WalFile { /// Must be invoked while writer and checkpoint locks are still held. fn restart_log(&mut self, mode: CheckpointMode) -> Result<()> { turso_assert!( - matches!(mode, CheckpointMode::Restart | CheckpointMode::Truncate), + mode.should_restart_log(), "CheckpointMode must be Restart or Truncate" ); turso_assert!(