Skip to content

SharedNodeStore never evicts closed-tab node data (unbounded global cache growth) #2568

Closed

Description

@mstomar125

Problem Statement

SharedNodeStore (packages/desktop-app/src/lib/services/shared-node-store.svelte.ts) is a single global singleton (SharedNodeStore.getInstance()) holding every node ever loaded by any tab/pane in a SvelteMap<string, Node>. There is no reference-counting, LRU, or any other eviction path: once a node is loaded, it stays in memory for the life of the app session, even after every tab/pane that showed it has closed.

This was verified directly, not just read from code:

  • ReactiveNodeService.destroy() (the exact lifecycle call every BaseNodeViewer makes on unmount) is an explicit no-op — its own comment says "Node data reactivity now comes directly from SharedNodeStore's SvelteMap — there is no subscription to tear down."
  • closeTab() (packages/desktop-app/src/lib/stores/navigation.svelte.ts) only cleans up scroll-position state and tab/pane bookkeeping. It never touches SharedNodeStore.
  • shared-node-store.svelte.ts has no evict/unload/prune logic anywhere (grepped the full 4,475-line file — zero hits besides two unrelated doc-comments).
  • A real headless probe (create 10 ReactiveNodeService instances simulating 10 open tabs, initializeNodes() 300 distinct nodes into each = 3,000 distinct nodes, then call destroy() on all 10 — simulating closing every tab) showed the shared node count staying at 3,000 before and after close. Heap grew ~16.5MB for those 3,000 plain node objects in a headless unit-test harness (no forced GC, so this is directional, not a production number — the real Tauri app additionally carries a rendered Svelte component + DOM per visible node, which this data-layer probe does not include).

Relationship to #302

This supersedes #302 ("Memory & Performance Optimization for Multi-Tab System", filed 2025-10-20). That issue described the problem as "multiple ReactiveNodeService instances" each independently holding node data, and proposed generic "lazy loading" + "cleanup on tab close." The actual current architecture is different: there is exactly one global node-data store shared by every viewer (ReactiveNodeService is now a thin per-viewer UI-state adapter with no node data or subscription of its own — confirmed above). So:

  • The good news Memory & Performance Optimization for Multi-Tab System #302 didn't know about: reopening the same tabs/nodes repeatedly does not duplicate memory — there's one cache, not N.
  • The real, still-current gap: that one shared cache only ever grows. A session that visits many different documents over time accumulates all of them in memory forever, regardless of how many tabs are currently open.

The fix shape is therefore "define and implement eviction from a shared cache once nothing open references an entry," not "add lazy loading/cleanup to N per-viewer instances" as #302's proposed solution assumed.

What already exists (do not re-implement)

  • Component-code lazy loading: node-component-loader.svelte.ts / schema-form-loader.svelte.ts already lazy-load and cache Svelte component bundles per node type via the plugin registry. This issue is about node data, not component code — leave that alone.
  • A real performance test suite exists (bun run test:perf, packages/desktop-app/src/tests/performance/architecture-benchmarks.test.ts) covering structural-op latency, initial render, multi-client sync latency, and lookup performance for a single ReactiveNodeService. Its one memory test is explicitly a "baseline estimation" (formula-based, not process.memoryUsage()) and does not exercise multiple concurrent viewers/tabs at all — extend this file, don't replace it.

Proposed Solution

  1. Add a real (not estimated) multi-tab memory measurement to the perf suite: open N simulated tabs on distinct documents, measure SharedNodeStore node count and heap usage, close tabs back down, measure again. This becomes the regression guard for whatever eviction policy ships.
  2. Design and implement a reference/visibility tracking mechanism so SharedNodeStore knows which cached nodes are still reachable from at least one open tab/pane (accounting for the existing multi-pane case where the same node is legitimately shown in two panes at once — must not evict while any pane still shows it).
  3. On a node becoming unreachable from every open tab/pane, make it eligible for eviction from SharedNodeStore.nodes, PersistenceCoordinator, versions, and any other per-node bookkeeping map — after an inactivity threshold (not immediately on tab close, to avoid thrashing on quick tab switches) and never while the node has a pending/unflushed write.
  4. Wire actual eviction into the tab-close path so the new perf-suite test in (1) demonstrates real memory being released, not just held flat.

Acceptance Criteria

  • New test:perf test measures real (process.memoryUsage()), not estimated, memory for N concurrently-open simulated tabs on distinct documents
  • Same test demonstrates node count / memory going back down after those tabs close and the inactivity threshold elapses — currently it stays flat
  • A node open in two panes simultaneously is never evicted while either pane still shows it
  • A node with a pending/unflushed write is never evicted mid-write
  • No regression to architecture-benchmarks.test.ts's existing single-viewer benchmarks

Key Areas

Files likely touched:

  • packages/desktop-app/src/lib/services/shared-node-store.svelte.ts — add reachability tracking + eviction
  • packages/desktop-app/src/lib/stores/navigation.svelte.ts — closeTab() needs to signal reachability change
  • packages/desktop-app/src/tests/performance/architecture-benchmarks.test.ts — add the real multi-tab memory test

Priority

Medium — confirmed real (not theoretical) unbounded growth, but bounded by session length; not a crash-causing leak in normal usage today.

Activity

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

    @mstomar125
    ContributorAuthor

    Resolution summary

    PR: #2571 — Addresses #2568

    Design: SharedNodeStore now tracks reachability by walking each cached node's structureTree ancestor chain up to its root and checking membership in openDocumentRootIds — the set of currently-open tabs' document root ids, pushed wholesale by navigation.svelte.ts's NavigationStore on every tab-state mutation (open, close, content change, session restore) via a new updateOpenDocumentRoots(). This reuses the parent index viewer data-loading already populates rather than adding a second membership map to keep in sync, and it handles the two-panes-same-node case for free (the shared node/root id just stays in the set as long as any open tab still reports it — no refcounting needed).

    A node whose root drops out of that set becomes an eviction candidate after a 30s inactivity window (long enough that quick tab-switching or an accidental close-and-reopen never trips it, short enough to reclaim memory well within a session), at which point it's cleared from nodes, versions, pendingUpdates, persistedNodeIds, taskFieldWriteSeq, and the other per-node bookkeeping maps — but only after re-verifying it's still unreachable AND has no pending/in-flight/queued persistence operation or active atomic batch; a dirty node gets rescheduled instead of evicted. Reachability starts fully dormant (nothing ever evicts) until the first report, so nothing outside real navigation usage is affected.

    Real measurement (not estimated) — TEST_FULL_PERFORMANCE=1 bun run test:perf, 10 tabs × 300 nodes = 3,000 nodes, opened/closed through the real addTab()/closeTab() path:

    Node count: 3000 while open -> 0 after close + eviction (baseline 0)
    Heap used (process.memoryUsage, GC forced via --expose-gc):
      baseline 49.01MB, peak 51.23MB, final 46.12MB
    

    Node count is deterministic (every node from every closed tab is gone). Heap fully recovered — the final reading came in below the pre-test baseline.

    Verification: bun run test:all (frontend + skill + full Rust workspace, including rust:test:app-units) and bun run quality:fix (eslint, svelte-check, tsc, cargo fmt/clippy with -D warnings) both pass clean. New tests: a dedicated shared-node-store-eviction.test.ts unit-tests the mechanism directly (dormancy, threshold timing, pending-write guard, active-batch guard, cancel-on-reopen); architecture-benchmarks.test.ts gets the real multi-tab memory test above plus an end-to-end two-panes-share-a-node test through the real navigation store; navigation.test.ts gets 4 tests locking in the closeTab() → SharedNodeStore wiring itself.

    Leaving this open for review per convention — not closing automatically.

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