Skip to content

nodespace-agent/daemon/cli integration tests share nodespace-core's unpatched libsql-ffi WAL-manager race, unserialized #2516

Closed

Description

@malibio

Overview

#2431 root-caused and fixed intermittent SQLITE_MISUSE/SIGSEGV flakiness in nodespace-core's lib tests. The fix (PR #2514) doesn't cover three other crates whose tests are exposed to the same underlying hazard.

Problem Statement

The root cause, documented on SqliteStore in packages/core/src/db/sqlite_store/mod.rs, is an unsynchronized static check-then-act race in libsql-ffi's vendored SQLite3MultipleCiphers WAL-manager singleton (make_sqlite3_wal_manager_rc/clone_wal_manager). Two connections opening at genuinely the same instant on separate OS threads can race that check-then-act or torn-write a shared refcount, corrupting a process-global struct that any subsequent SQLite operation on any connection may then trip over — not just the connections that raced.

PR #2514 fixed this for nodespace-core by serializing its own lib/bin/test targets to --test-threads=1 (rust:test:core in package.json), since nodespace-core is the workspace's only crate with a direct dependency on libsql/libsql-ffi.

However, libsql-ffi is also linked transitively into nodespace-agent, nodespace-daemon, and nodespace-cli (confirmed via cargo tree -i libsql-ffi), and each of these crates has its own non-#[ignore]d integration tests that call SqliteStore::new() directly and still run at --test-threads=2 under rust:test:workspace, unserialized:

  • packages/daemon/tests/grpc_round_trip.rs
  • packages/daemon/tests/import_round_trip.rs
  • packages/daemon/tests/agent_session_grpc.rs
  • packages/cli/tests/cli_integration.rs
  • packages/cli/tests/import_dir_multi.rs
  • packages/agent/tests/search_nodes_enumerate.rs
  • packages/agent/tests/live_unset_schema_field.rs
  • packages/agent/tests/live_update_node_noop_gate.rs
  • packages/agent/tests/matrix_scenario_winnability.rs
  • packages/agent/tests/live_resolve_query_decomposition_shapes.rs

The same race is reachable there in principle. It hasn't been observed to trigger yet in these suites, plausibly because they're far smaller (tens of tests, not 1000+ like nodespace-core's lib suite was), so the odds of two connections racing to open at literally the same instant are much lower — not because the hazard doesn't apply.

Proposed Solution

Investigate whether these crates' SqliteStore-touching test targets should get the same treatment preemptively (a serialized step analogous to rust:test:core), or whether it's acceptable to wait for empirical reproduction there before acting. Either extend the fix, or make and document a deliberate decision to defer with the reasoning (e.g. "odds are low enough at this suite size that we'll act if/when it's actually observed").

Acceptance Criteria

  • Determine whether nodespace-agent, nodespace-daemon, and nodespace-cli's SqliteStore-touching integration tests should be serialized against the same hazard
  • If yes: extend a --test-threads=1 treatment to the relevant test targets in those crates, analogous to rust:test:core, without unnecessarily slowing tests that don't touch SqliteStore
  • If no (deferring): document the reasoning explicitly, including what would change that decision (e.g. observed flakiness in one of the listed test files)
  • Correct or confirm the scope claim in packages/core/src/db/sqlite_store/mod.rs's doc comment on SqliteStore matches whatever decision is made here
  • bun run test:all passes with no new failures
  • Code passes bun run quality:fix

Technical Specifications

Reference Files

  • packages/core/src/db/sqlite_store/mod.rs: doc comment on SqliteStore — the root-cause write-up and the note pointing here
  • package.json: rust:test:core, rust:test:workspace — the pattern to extend or reference
  • packages/desktop-app/package.json: rust:test — delegates to the root rust:test:core, a precedent for how a sibling package.json should reference the canonical script rather than duplicating it

Verification approach

Per #2431's investigation, reproducing this hazard reliably needs many concurrent SqliteStore::new() calls at a high thread count over repeated runs (the nodespace-core investigation used --test-threads=16, 40+ consecutive runs, to get a ~5-15% failure rate). Given these three crates' suites are much smaller, a similar statistical approach (higher thread count, many repeated runs) is the way to determine whether the race is practically reachable there before deciding whether a fix is warranted.

Related Issues

Activity

  1. self-assigned this
    on Sep 9, 2026
  2. mstomar125 commented on Sep 9, 2026

    @mstomar125
    Contributor

    Investigated and resolved in #2524.

    Path taken: extend the --test-threads=1 treatment, not defer.

    Ran each SqliteStore-touching test target across nodespace-agent, nodespace-daemon, and nodespace-cli at --test-threads=16 over ~290 total repeated runs (matching #2431's own methodology — see PR for the full per-target breakdown). The race reproduced directly: once in 100 runs of nodespace-agent's --lib suite, hitting the exact failure shape #2431 documented for nodespace-core — 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.

    nodespace-daemon's lib and every explicitly-named integration target in the original issue stayed clean across ~190 further runs, but the root cause has no principled guarded region narrower than "every operation on every connection, for the life of the process" — so a clean run there reads as that suite being smaller (fewer concurrent SqliteStore::new() calls competing for the same instant), not as being unexposed to the hazard.

    Added rust:test:libsql-linked (mirrors rust:test:core's shape) covering all three crates' --lib --bins --tests targets at --test-threads=1, excluded them from rust:test:workspace's --test-threads=2 run, and corrected the SqliteStore doc comment (previously described this as a deliberate deferral on an assumption — "tens of tests, not 1000+" — that undercounted nodespace-agent's own lib suite, which alone runs 621 tests in one process).

    bun run test:all and bun run quality:fix both pass clean.

  3. mstomar125 commented on Sep 9, 2026

    @mstomar125
    Contributor

    Fixed via PR #2524, merged 9065eff1. Empirical finding: reproduced the WAL-manager race once in 100 runs of nodespace-agent's lib suite (matching #2431's exact failure shape); extended the --test-threads=1 serialization to all three libsql-ffi-linked crates rather than deferring, since the underlying hazard has no principled per-crate boundary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions