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

What the Change Does

The delete_file function in the native Rust SQLite VFS swaps the order of two KV deletion operations.

Before: chunks deleted first, metadata deleted second
After: metadata deleted first, chunks deleted second


Correctness: The Fix is Sound

This is a genuine crash-safety improvement. The two KV operations are not atomic, so failure/crash ordering matters.

Old order (chunks first, metadata second): If the process crashes after kv_delete_range succeeds but before kv_delete(meta_key) completes, the metadata entry survives pointing to zero chunks. On the next xOpen or xAccess, the VFS sees a valid file that has no data. This is silent corruption.

New order (metadata first, chunks second): If the process crashes after deleting metadata but before the range delete finishes, orphaned chunk data is left behind. Since no metadata key points to those chunks, xOpen and xAccess treat the file as nonexistent. Orphaned chunks waste KV storage but do not corrupt reads. This is the correct, safer failure mode.


Issues Found

Required: WASM VFS Parity Violation

CLAUDE.md mandates the native Rust VFS and the WASM TypeScript VFS must match 1:1, and when changing any VFS behavior in one implementation, update the other.

The WASM counterpart in packages/sqlite-wasm/src/vfs.ts still deletes chunks before metadata, the same pattern that was just fixed in the Rust implementation. The same crash-safety fix should be applied to the WASM VFS in this PR.

Low: Missing Ordering Rationale Comment

delete_file has no comment explaining why metadata is deleted first. Future readers may view this as arbitrary ordering. A short comment would prevent regressions, e.g.: "Delete metadata first so a crash mid-delete leaves only unreachable orphan chunks rather than dangling metadata pointing to absent data."

Low: No Test Coverage

No test verifies the crash-safety property. A test confirming that a file with deleted metadata but present chunks is treated as nonexistent by xAccess/xOpen would lock in the intended behavior.


Summary

Category Finding Severity
Correctness Fix is correct, metadata-first is the safe ordering Good
Parity WASM VFS still deletes chunks first, violating required 1:1 parity Medium
Code clarity No comment explaining ordering rationale Low
Testing No test for crash-safety behavior Low

@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