Repository navigation
SNKV v0.9.0 - fuzz and power-loss testing, thread-safe transactions
Pre-releaseBreaking release.
KVStoreConfigchanged size and two enum values changed: rebuild everything that uses SNKV and read Breaking changes below.Single-header build: download
snkv.hbelow, then#define SNKV_IMPLEMENTATIONin one .c file.
Breaking changes
KVStoreConfig.fullMutexremoved. Every handle now always has one lock,
so aKVStore*can always be shared between threads. Code that sets
cfg.fullMutexno longer compiles: delete the line.KVStoreConfig.walSizeLimitremoved. It checkpointed every N commits.
UsewalAutoCheckpointinstead, which counts WAL pages and is on by
default (1000 pages, like SQLite).KVSTORE_JOURNAL_DELETEis now 2 andKVSTORE_SYNC_OFFis now 3 (both
were 0). 0 now means the default for both fields (WAL, NORMAL), as the
docs always said: before, a zero-filledKVStoreConfigsilently meant
DELETE journal and no fsync.kvstore_open_encryptedwith a NULL
config, and so every encrypted vector store, was hit by this and ran
without fsync. Unknown values now returnKVSTORE_ERROR. Code that uses
the names only needs a rebuild; code that passes a literal0to mean
DELETE or OFF now gets WAL / NORMAL.- The size of
KVStoreConfigchanged. Rebuild everything that uses it. - The
python_bindingsbranch still usesfull_mutex/wal_size_limitand
has to be updated before it can move to this version.
Threading: transactions now belong to one thread
kvstore_begin(kv, 1) keeps the handle locked until kvstore_commit or
kvstore_rollback. Other threads' calls on that handle wait until then.
Before, they ran inside the open transaction, so one thread's rollback could
undo another thread's writes.
Two patterns now deadlock and must be changed:
- Thread A calls
kvstore_begin, then waits for thread B to write through
the same handle. B waits for A's commit, and A waits for B. - Thread A calls
kvstore_beginand thread B callskvstore_commit/
kvstore_rollback. B waits for the lock that A holds.
Commit or roll back on the thread that called begin. To let several threads
write at the same time, give each its own handle: WAL lets one writer run
alongside many readers, and a second writer gets KVSTORE_BUSY (or waits up
to busyTimeout).
Added
make fuzz(clang + libFuzzer, Linux / macOS):fuzz_apiruns random API
sequences (puts, deletes, TTL, transactions, scans, writes during scans,
reopen) against an in-memory model;fuzz_openopens and uses damaged
database files (seeded from real stores, including encrypted ones).
make fuzz-regressreplays every input that once found a bug. CI runs a
short fuzz on every PR and 30 minutes per fuzzer nightly, keeping the
corpus between runs.make test-powerloss: simulated power loss at every write and sync
(unsynced writes lost, kept, or torn per 512-byte sector), for WAL and
DELETE journals, sync FULL and NORMAL, and encrypted stores through
re-key and decryption. After every crash the store must open, pass the
integrity check and hold exactly a committed state. A self-check proves
the harness catches a disk that ignores fsync.kvstore_errmsg_copy(): copies the last error message under the handle
lock. Use it instead ofkvstore_errmsg()when threads share a handle.KVSTORE_VERSION,KVSTORE_VERSION_MAJOR/_MINOR/_PATCH,
KVSTORE_VERSION_NUMBER,kvstore_libversion()and
kvstore_libversion_number().KVStoreConfig.walAutoCheckpointand the build-time default
KVSTORE_DEFAULT_WAL_AUTOCHECKPOINT.cacheSize < 0sets the page cache as a memory budget in KiB.- Build-time Argon2 cost settings (
SNKV_ENC_M_COSTand limits) for small
devices.
Fixed
- Found by the new fuzzers (
make fuzz):- Rolling back a transaction that held a column family's first TTL write
left the handle pointing at the TTL index tables the rollback removed:
every laterput_ttl,get_ttl,purge_expiredandcf_clearon it
returnedKVSTORE_CORRUPTuntil the store was reopened. TTL index
tables created in a rolled-back transaction are now dropped from the
handle; tables that existed before are kept. - 4-byte lengths in stored records were decoded with a signed shift (
b[0] << 24): undefined behaviour for a top byte >= 0x80, and in practice a
negative length. On a damaged filegetand iteratorvalueused it to
size the value (one damaged byte caused a 1.6 GB allocation), and the
integrity check and column-family walk read root pages from the wrong
place. All 23 decodes now use one unsigned helper that rejects lengths
that do not fit, and the sites that use them check for that. - Opening a damaged plain store with a password (encrypt-on-open) could
loop forever: rewriting each value seeks past the last processed key,
and in a B-tree with out-of-order keys the seek moved backwards,
re-encrypting (and growing) the same rows endlessly. The rewrite now
requires each key to be greater than the last and returns
KVSTORE_CORRUPTotherwise. - 8-byte TTL timestamps were decoded the same signed way (undefined
behaviour for a damaged timestamp with the top bit set). Now decoded
as unsigned. - A damaged record header could claim an entry far larger than the file
(2 GB in a 35 KB file), and buffers were sized from that claim before
reading: encrypt-on-open,get, iterators and the column-family walk
could ask for gigabytes of memory. Every entry size is now checked
against the file size first (the check SQLite's SQL layer makes, which
SNKV bypasses); reads of such an entry returnKVSTORE_CORRUPTand
scans skip it.
- Rolling back a transaction that held a column family's first TTL write
- Vector store: after a power cut that followed a clean
kvstore_vec_close,
the index sidecar ({db}.usearch, written without fsync) could be torn
while its stamp ({db}.usearch.nid) survived. Open accepted the torn
file and the first search aborted the process. The stamp now also holds a
checksum of the sidecar; a sidecar that does not match (torn, truncated,
corrupt, or an old 8-byte stamp) is ignored and the index is rebuilt from
the database. kvstore_iterator_key/_valueafter a write through the handle moved
the iterator's row (for example, inserts that split its page) returned
KVSTORE_CORRUPTon a healthy store: they read the B-tree cursor without
restoring the position SQLite saved. They now restore it; if the current
row was deleted,valuereturnsKVSTORE_NOTFOUNDandnext/prev
still continue from the right place.- Thread safety: one lock per handle (the per-column-family locks are gone).
Iterator calls and closing a column family or iterator now run under the
handle lock. They used to read and release pages of the shared page cache
without it. - Vector store:
get,get_vector,get_metadata,kv_put,containsand
countnow take the store's mutex, and the "index dropped" check runs under
it (it raced withdrop_index). kvstore_openno longer fails when SQLite mutexes are switched off at run
time (SQLITE_CONFIG_SINGLETHREAD).- A leftover half of a column family's TTL index is emptied on open instead
of dropped (a drop could move the table of the column family being opened,
and cleanup was skipped while an iterator was open). - Creating an internal (vector store) column family inside
kvstore_beginno
longer commits the caller's transaction. - Re-encryption fails instead of continuing if random nonce generation fails.
purge_expiredreports delete errors and counts only committed deletes.kvstore_incremental_vacuumworks while iterators are open.kvstore_syncfsyncs; drops and rollbacks keep open handles correct;
failed internal commits roll back; TTL expiry after a rollback; a crash on
corrupt keys; key-only lookups for large values.- WAL growth: SNKV never installed SQLite's WAL auto-checkpoint, so the WAL
grew to the size of the database and slowed reads by 8–16%.
Performance
- Iterators now lock the handle (in 0.8.2 they took no lock, which was the
unsafe part), but only when they move:first/next/last/prev/
seekcopy the current row, andkey/valuereturn the copy without
locking. One lock per row instead of three. A full key+value scan is 1.78x
faster than with a lock on every call, and 1.73x faster than SQLite
(mean of 5 alternating runs, 1M rows on tmpfs: 5.9 → 10.6 M rows/s; SQLite
6.1 M rows/s). Point reads and writes are unchanged. Builds that never use
threads can compile with-DSQLITE_THREADSAFE=0to remove all locking. - Values up to 1 KB (
KVSTORE_ITER_PREFETCH_MAX) are copied when the
iterator moves; larger and encrypted values are still read when
kvstore_iterator_valueis called. So a write to the current key made
after the move is not seen bykvstore_iterator_valuefor small values
until the iterator moves again. The pointers fromkey/valuestay
valid until the iterator moves or is closed.
Removed
- Python bindings moved from
masterto thepython_bindingsbranch.