Skip to content

fix(sqlite-native): delete metadata before chunk range#4638

Open
NathanFlurry wants to merge 1 commit into04-12-fix_sqlite-native_keep_truncate_cache_coherentfrom
04-12-fix_sqlite-native_delete_metadata_before_chunk_range
Open

fix(sqlite-native): delete metadata before chunk range#4638
NathanFlurry wants to merge 1 commit into04-12-fix_sqlite-native_keep_truncate_cache_coherentfrom
04-12-fix_sqlite-native_delete_metadata_before_chunk_range

Conversation

@NathanFlurry
Copy link
Copy Markdown
Member

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copy link
Copy Markdown
Member Author

NathanFlurry commented Apr 13, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@claude
Copy link
Copy Markdown

claude bot commented Apr 13, 2026

PR Review: fix(sqlite-native): delete metadata before chunk range

Overview

A minimal 4-line swap in rivetkit-typescript/packages/sqlite-native/src/vfs.rs that reverses the deletion order in delete_file: the metadata key is now deleted before the chunk range, not after.


Correctness: The Fix Is Right

The new ordering is safer for crash/error recovery:

  • Old order (chunks first, then metadata): If kv_delete_range succeeds but the subsequent kv_delete on the metadata key fails, SQLite finds a metadata key pointing to a file with no chunks. On next open it reads a non-zero size but gets empty pages, which can silently corrupt the database.
  • New order (metadata first, then chunks): If kv_delete succeeds but kv_delete_range fails, orphaned chunks remain in KV with no reachable metadata key. SQLite treats the file as non-existent. The orphaned chunks are a storage leak but not a correctness issue.

Metadata-first deletion is the standard "delete the pointer before the data" pattern for crash-consistent KV-backed filesystems.


Parity Violation with WASM VFS

Per CLAUDE.md, the Rust native VFS and the TypeScript WASM VFS must match 1:1, and the delete/truncate strategy is explicitly called out. However, the WASM VFS (packages/sqlite-vfs/src/vfs.ts, lines 1402-1407) still uses the old order: chunks are deleted before metadata. This PR fixes the Rust side but leaves the TypeScript side with the unsafe ordering.

Recommendation: Either include the WASM fix (deleteBatch([metaKey]) before deleteRange) in this PR, or track it as an explicit follow-up.


Other Notes

  • The PR description is empty (template checkboxes unchecked, no motivation or test plan). A short explanation of the bug and its impact would help future readers.
  • The change itself is clean and appropriately minimal.

Summary

Fix correctness Correct: metadata-first is the right crash-safe order
Parity with WASM VFS Missing - packages/sqlite-vfs/src/vfs.ts still deletes chunks before metadata
Code quality Clean, no style issues
Tests No new test added

The core fix is sound. The main ask is to also update the WASM VFS for parity.

@NathanFlurry NathanFlurry marked this pull request as ready for review April 13, 2026 05:18
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_delete_metadata_before_chunk_range branch from 12c0baa to 61b5457 Compare April 13, 2026 05:38
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_keep_truncate_cache_coherent branch 2 times, most recently from 459dd12 to d7b2c43 Compare April 13, 2026 05:50
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_delete_metadata_before_chunk_range branch from 61b5457 to bc7e3d7 Compare April 13, 2026 05:50
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_keep_truncate_cache_coherent branch from d7b2c43 to 6c17fae Compare April 13, 2026 07:03
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_delete_metadata_before_chunk_range branch from bc7e3d7 to 49fae2b Compare April 13, 2026 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant