Skip to content

fix(sqlite-native): restore kv error hook#4635

Draft
NathanFlurry wants to merge 1 commit into04-12-perf_sqlite-native_avoid_cloning_cached_read_chunksfrom
04-12-fix_sqlite-native_restore_kv_error_hook
Draft

fix(sqlite-native): restore kv error hook#4635
NathanFlurry wants to merge 1 commit into04-12-perf_sqlite-native_avoid_cloning_cached_read_chunksfrom
04-12-fix_sqlite-native_restore_kv_error_hook

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

@claude
Copy link
Copy Markdown

claude bot commented Apr 13, 2026

PR Review: fix(sqlite-native): restore kv error hook

This PR restores and properly wires the KV error hook in the native SQLite VFS layer, surfacing storage failures through to JavaScript with actionable context. The overall approach is sound.


Bugs / Correctness

kv_vfs_get_last_error uses clone_last_error not take_last_error (vfs.rs)

Using clone_last_error is correct because SQLite may call xGetLastError more than once, but it means the error lingers until a later successful operation clears it. The clear_last_error() calls on success guard against stale values being returned via takeLastKvError(), but a comment explaining this design choice would help future readers.

wrapNativeStorageError always throws but callers don't signal that (wrapper.js)

Both branches of wrapNativeStorageError throw, so callers rely on implicit never semantics:

} catch (error) {
    wrapNativeStorageError(nativeDb, error);
    // silent fall-through if the above ever stops throwing
}
return [];

If a future change makes wrapNativeStorageError not always throw, the caller silently returns []. Prefer an explicit pattern:

} catch (error) {
    throw wrapNativeStorageError(nativeDb, error);
}

Or rename to throwNativeStorageError to make intent clear at call sites.

Missing // SAFETY: comment on raw pointer dereference (vfs.rs, KvVfs::take_last_kv_error)

fn take_last_kv_error(&self) -> Option<String> {
    unsafe { (*self.ctx_ptr).take_last_error() }
}

This needs a // SAFETY: comment explaining the invariant: ctx_ptr is heap-allocated in KvVfs::register and freed only in KvVfs::drop, so dereferencing it is valid for any call made through NativeDatabase (which owns the KvVfs).


Minor Issues

read_cache change is a silent type fix (vfs.rs, kv_io_write)

The change from state.read_cache.get(...) to state.read_cache.as_ref().and_then(...) implies read_cache is now Option<_>. Since the old code would not compile with Option<HashMap>, this must reflect a type change that landed elsewhere. A brief note in the commit or a comment would help during bisects.

Unrelated bug fixes bundled in

The notGlobal ?? false, read_cache.as_ref(), and .as_deref() fixes are correctness improvements but unrelated to the KV error hook. Separating them into their own commit would make the history cleaner.


Before merge

  • Tests for the takeLastKvError() round-trip (actor shutdown scenario)
  • // SAFETY: comment on the unsafe block in take_last_kv_error
  • Explicit throw (or rename) in wrapNativeStorageError callers

What is good

  • Error propagation path (Rust VFS -> NAPI -> JS) is clean and well-layered.
  • clone_last_error in xGetLastError correctly handles SQLite calling the callback multiple times.
  • The typeof nativeDb.takeLastKvError === "function" defensive check is a solid backwards-compatibility guard.
  • Error message in wrapNativeStorageError is specific and actionable (c.abortSignal guidance).
  • sqlite_error_message helper gives better diagnostics on open/pragma failures.
  • Proper null-termination and length bounds in kv_vfs_get_last_error -- no buffer overflow possible.

Generated with Claude Code

@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_avoid_cloning_cached_read_chunks branch from ceec0d4 to ff117f9 Compare April 13, 2026 05:38
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_restore_kv_error_hook branch from 6eac78f to 4e380c8 Compare April 13, 2026 05:38
@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_avoid_cloning_cached_read_chunks branch from ff117f9 to e25c1b6 Compare April 13, 2026 05:50
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_restore_kv_error_hook branch from 4e380c8 to 7fbbf37 Compare April 13, 2026 05:50
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_restore_kv_error_hook branch from 7fbbf37 to fe8cf4f Compare April 13, 2026 07:03
@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_avoid_cloning_cached_read_chunks branch from e25c1b6 to 532364f 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