Skip to content

SqliteStore holds a reader connection across nested reads in 10 methods, pinning two connections per call #2410

Closed

Description

@malibio

Problem

Ten read-path methods in SqliteStore call another store method while an outer result cursor is still alive, which pins two reader connections per call instead of one.

ReadRows owns its reader connection until dropped — this is deliberate and documented in packages/core/src/db/sqlite_store/connections.rs:

A result cursor that holds its reader connection for as long as it lives.

So a nested self.read() issued before the outer cursor is dropped checks out a second connection, and neither returns to the pool until the whole function ends.

Why it matters

MAX_IDLE_READERS is 8. The pool grows to the peak number of simultaneously in-flight reads, so doubling the connections held per call halves the effective concurrency before the pool starts closing connections instead of pooling them, and inflates the peak. Every one of these is on a read path that can be called concurrently.

Measured on the schema path: three sequential GetAllSchemas calls left the daemon holding 8 open database fds; after fixing those two sites, 6.

Affected sites

Found by scanning for a cursor bound from self.read() that is still live across a subsequent self.<method>(...) call:

File Line Method Nested call
nodes.rs 2679 get_all_schemas get_all_schema_declarations
nodes.rs 2649 get_schema_node get_schema_declarations
nodes.rs 1594 get_subtree_with_relationships get_node
nodes.rs 1833 assert_may_gain_parent get_node_memberships
embeddings.rs 431 search_embeddings get_node
embeddings.rs 511 search_embeddings_by_node_type get_node
embeddings.rs 600 bm25_search_roots get_nodes_by_ids
relationships.rs 109 get_incoming_mention_containers get_node_type
relationships.rs 528 get_collection_by_name get_node
relationships.rs 550 get_collections_by_names get_node

The three embeddings.rs sites and get_subtree_with_relationships look worst: they call the nested method inside the row loop, so they hold the outer cursor across N nested reads rather than one.

Fix

Drain the outer cursor into an owned collection, drop(rows), then do the nested reads. The first two sites are already fixed this way locally (kept back from #2409 to avoid mixing an unrelated fix into a deletion PR) — that patch can seed this work.

Acceptance criteria

  • All ten sites release the outer cursor before any nested store call
  • The in-loop cases (embeddings.rs ×3, get_subtree_with_relationships) collect row data first, then perform nested reads outside the loop
  • A regression test asserts a bounded number of connections is held across a representative nested read
  • Consider a debug assertion or a doc note on ReadConn making the "do not nest a read under a live cursor" rule enforceable rather than conventional — the contract is currently documented but nothing catches a violation
  • Full test suite green

Context

Surfaced while investigating #2402. It is not the cause of that hang (fixing two sites left it unchanged), but it is a real defect found on the way.

Activity

  1. malibio commented on Sep 7, 2026

    @malibio
    CollaboratorAuthor

    Baseline (worktree issue-2410-nested-reads): Frontend: 231 test files passed, 4857 tests passed.

  2. self-assigned this
    on Sep 7, 2026
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