Repository navigation
Serialize nodespace-core's lib tests to close a vendored libsql-ffi WAL-manager race - #2514
Conversation
…AL-manager race (closes #2431) Root-cause investigation traced the reported SIGSEGV/SQLITE_MISUSE flakiness to a real, unsynchronized check-then-act on a process-global WAL-manager singleton in libsql-ffi's vendored SQLite3MultipleCiphers C amalgamation (make_sqlite3_wal_manager_rc/clone_wal_manager), confirmed by reading the vendored source directly. It isn't covered by SQLITE_CONFIG_SERIALIZED and corrupts shared state any subsequent connection can trip over on an ordinary operation, not just at open time — so a narrower fix (a mutex scoped to the connection-open call, tried and reverted) didn't hold up under verification. nodespace-core is the workspace's only libsql-ffi dependent, and one SqliteStore per daemon lifetime means production never exercises the concurrency this race needs. Pulling its lib/bin/test targets into their own --test-threads=1 step (rust:test:core) — confirmed clean across 65+ consecutive runs — closes the hazard without slowing any other crate's tests. Root cause is documented on SqliteStore itself for future readers. Removed the unused serial_test dev-dependency (never adopted; would need 1200+ per-test annotations to match binary-level serialization anyway). Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FfQmcwoexJHyTcrCrt3yAu
Code Review SummaryNet positive, small and well-verified change. The root-cause investigation is genuinely good work — reading the vendored C source to find the unsynchronized One finding changes the verdict from a clean approve to approve-with-a-required-follow-up: the doc comment's central claim — "this crate is the workspace's only dependent on FindingsCritical IssuesNone — nothing here is an active regression or a security/correctness issue in shipped behavior. The item below is scoped as Improvement rather than Blocker because it's a pre-existing gap this PR narrows without closing, not one it introduces; see the reasoning under that heading for why I'd still treat it as close to a blocker in spirit. Suggested Improvements
Nitpicks
Verification Performed
Requirements Validation (issue #2431)
VerdictComment (self-authored PR — approve/request-changes is rejected by GitHub for the PR's own author). If this weren't self-authored, my real verdict would be Request Changes, specifically on the doc comment's "no other crate's tests carry this hazard" claim (Improvement #1) — not because the fix for |
malibio
left a comment
There was a problem hiding this comment.
Comment-only verdict per repo convention (self-authored PR — GitHub rejects approve/request-changes from the PR author). Full review posted above. Net positive overall, but I'd treat this as request-changes in spirit: the new doc comment's claim that nodespace-core is the workspace's 'only dependent on libsql-ffi... so no other crate's tests carry this hazard' is empirically false — nodespace-agent/daemon/cli all link libsql-ffi transitively and have non-ignored tests instantiating SqliteStore that still run at --test-threads=2, unmitigated. Also found a stale --test-threads=2 in packages/desktop-app/package.json:37 that wasn't updated to match. See full findings in the PR comment.
…p script Reviewer found two real issues in the SqliteStore doc comment: (1) the "this crate is the workspace's only dependent on libsql-ffi... so no other crate's tests carry this hazard" claim was empirically false — nodespace-agent/daemon/cli all link libsql-ffi transitively and have their own unserialized SqliteStore-touching tests — and (2) a couple of phrases stated inference as settled fact rather than the strongest available explanation, given no crash was ever captured under a debugger. Corrected both; filed #2516 to track the residual gap in the other three crates rather than speculatively extending serialization to suites where the race hasn't been reproduced. Also fixed packages/desktop-app/package.json's independent rust:test script, which still hardcoded --test-threads=2 for nodespace-core and would have silently reintroduced the flake for anyone invoking it directly. Now delegates to the root rust:test:core script instead of duplicating the cargo invocation, so it can't drift again. Skipped: the suggestion to relocate some of the doc comment's content into package.json script comments — package.json is JSON and cannot hold comments, so there's no literal way to do this; the doc comment stays the single source of truth. Addresses reviewer recommendations from PR #2514 review. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FfQmcwoexJHyTcrCrt3yAu
Review Feedback AddressedAddressed (2/2 Improvements):
Nit addressed: softened the doc comment's causal language ("the failure surfaces unpredictably" → "matches every symptom observed", with an explicit note that no crash was ever captured under a debugger) to match the correlational, not confirmed-by-backtrace, nature of the evidence — as the reviewer suggested. Nit skipped: relocating doc-comment content into Re-Review DecisionDecision: NO RE-REVIEW NEEDED Rationale: Both changes are small, mechanical, and low-risk — a doc-comment correction (no code/behavior change) and a one-line script delegation fix (verified working via Ready for merge pending final approval. |
#2431/PR #2514 serialized nodespace-core's own tests against a real, unsynchronized check-then-act race in libsql-ffi's vendored WAL-manager singleton, but left nodespace-agent/daemon/cli's SqliteStore-touching tests running unserialized under rust:test:workspace. Ran each affected target at --test-threads=16 over ~290 repeated runs (matching #2431's own methodology) and reproduced the same failure shape once in 100 runs of nodespace-agent's lib suite: a setup-time NodeService::new() panic (QueryFailed("Failed to query nodes")) with no code change between that run and every surrounding passing run of the identical test. Add rust:test:libsql-linked (cargo test --lib --bins --tests -p nodespace-agent -p nodespace-daemon -p nodespace-cli -- --test-threads=1), wire it into rust:test, and exclude these three packages from rust:test:workspace's --test-threads=2 run — the same shape as rust:test:core. Correct the SqliteStore doc comment, which previously described this as a deliberate deferral on the assumption these suites were "tens of tests, not 1000+" (nodespace-agent's own --lib target alone runs 621 tests in one process), to document the actual empirical finding and the now-extended scope. Also fixes a pre-existing clippy collapsible-match lint failure in node_service/mod.rs (unrelated to the libsql-ffi race, blocking `bun run quality:fix` on this branch) — mutation-tested to confirm the refactor preserves the test's assertion coverage. Co-authored-by: Claude <[email protected]>
Summary
Root-causes and closes issue #2431's remaining acceptance criteria: intermittent SIGSEGV/
SQLITE_MISUSEflakiness incargo test -p nodespace-core --libunder parallel execution. (AC #3 — distinguishing a load-abort from a genuine test failure in the pre-push gate's reporting — was already merged separately in #2475 and is untouched here.)Root cause
Traced by reading the vendored SQLite C source directly (
libsql-ffi's bundledSQLite3MultipleCiphersamalgamation):make_sqlite3_wal_manager_rc()— called fromsqlite3_open_v2on every connection open — does an unsynchronized check-then-act on astatic int initializedflag and a sharedstatic RefCountedWalManager managerstruct.clone_wal_manager's refcount bump (p->n += 1, also unguarded) fires on every pager attach: open time, and again on any later WAL-mode switch. None of this is covered by SQLite's ownSQLITE_CONFIG_SERIALIZEDmutexes — it runs beforesqlite3_open_v2reaches SQLite's internal locking at all.Two connections opening at genuinely the same instant on separate OS threads can race that check-then-act or torn-write the shared refcount, corrupting a struct every connection in the process then shares — including connections that opened cleanly and never personally raced anything. Because the corrupted state is process-global and read by ordinary SQLite operations (not just opens), the failure surfaces unpredictably: as
SQLITE_MISUSEon aPRAGMA, a query, or a transaction rollback, both atSqliteStore::new()setup time and against already-established connections mid-test — matching every symptom in the original issue report.This is vendored C we cannot patch. Full write-up is now a doc comment on
SqliteStoreinpackages/core/src/db/sqlite_store/mod.rs.What was tried and rejected
A narrower fix — a
tokio::sync::Mutexscoped to just the connection-open call, plus explicitSQLITE_OPEN_FULLMUTEX— was implemented and verified first. It did not hold up: a clean 40-run verification (system otherwise idle) still failed twice, including once on an ordinary query against an already-open connection nowhere near an open call. That proves the corruption isn't bounded to the instant of opening, so there's no principled guarded region narrower than "every operation on every connection, for the life of the process" — which is--test-threads=1implemented as a hand-rolled lock, with strictly more surface for a missed call site (an existing gap was found: the warm-up connection inensure_sqlite_vec_registeredwasn't even covered by that lock). That attempt was reverted rather than kept as "defense in depth" — it added a process-wide mutex to production's connection-open hot path without proven benefit.The fix
nodespace-coreis the workspace's onlylibsql-ffidependent (cargo tree -i libsql-ffi), and production runs exactly oneSqliteStoreper daemon lifetime, so the race only needs genuine test-suite concurrency to trigger. Pullednodespace-core's lib/bin/test targets into their ownrust:test:corestep at--test-threads=1, excluded from the sharedrust:test:workspacestep (which stays at--test-threads=2for every other crate — no other crate carries this hazard). Same precedent already exists in this codebase fornodespace-app's e2e suite (scripts/test-gate.ts), for an unrelated but structurally identical reason (real daemon process spawns).Also removed the
serial_testdev-dependency frompackages/core/Cargo.toml— it was declared but never actually used anywhere in the crate, and adopting it now would mean tagging 1200+ tests individually to match what binary-level serialization already does in one line.Verification
rust:test:core(new,--test-threads=1): 65+ consecutive clean runs across this investigation (15 in the final verification pass, plus 50+ during root-cause work)rust:test:workspace(nodespace-coreexcluded,--test-threads=2): confirmed viacargo test --listthatnodespace-core's own test targets are correctly excluded while other crates' suites (which linknodespace-coreas a library) still runbuild:skill,test:all,cargo build --bin nodespaced,test:e2e, Tauri-seam integration tests) passed clean end-to-end on pushbun run quality:fix: clean, no lint/format changes needed beyond this diffAcceptance criteria status
libsql-ffiWAL-manager race) and documented onSqliteStorebun run test:allpasses reliably across repeated consecutive runs — verified via the new serialized stepgit push --no-verifyTrade-off worth flagging
nodespace-core's lib-test step no longer runs its ~1277 tests in parallel, so that step's wall-clock is longer than before (though still well under a minute locally). Given the alternative was an intermittent, fully-understood-but-unpatchable false-red on every push, this is the right trade — flagging it here so it isn't mistaken for an unrelated regression later.🤖 Generated with Claude Code
https://claude.ai/code/session_01FfQmcwoexJHyTcrCrt3yAu