Skip to content

Dev + production daemons can now run concurrently against the same shared SQLite DB with no busy_timeout/retry #1483

Closed

Description

@malibio

Overview

#1469 scoped the daemon's socket and launchd label by build variant (dev/release × community/Pro) specifically so multiple daemon instances no longer collide/kill each other. But the SQLite database path was intentionally left unscoped — all variants share ~/.nodespace/database/nodespace.db — so a dev daemon and a production daemon can now run concurrently, each with their own exclusive-lock assumption, against the same file.

This directly contradicts the premise of ADR-031, which states the daemon-client architecture exists specifically because "the embedded SQLite (libsql) database requires a writer lock" and only one process should hold it. #1469 makes it possible for two nodespaced processes to both believe they hold that lock simultaneously.

What's actually in place today

  • packages/core/src/db/schema.sql sets PRAGMA journal_mode = WAL — this is good and necessary (WAL supports multiple readers + one writer across separate OS processes on the same file, unlike rollback-journal mode).
  • No busy_timeout is set, and there's no explicit application-level lock (flock, PID file, etc.) coordinating writes between two daemon processes.
  • WAL mode alone does not make concurrent writers from separate processes safe by itself — SQLite still serializes actual write transactions at the file level, but without a busy_timeout, a write from one process while another holds the write lock will fail immediately with SQLITE_BUSY rather than waiting/retrying.
  • Confirmed via grep that no SQLITE_BUSY handling or retry logic exists anywhere in packages/core/src/db/sqlite_store.rs today — a busy-lock failure would propagate as a hard error, not retry silently.

Why this is a real (if narrow) risk

Before #1469: a dev and production daemon could never coexist — kill_stale_daemon_sync/the shared socket meant one would always be killed on the other's startup. Multi-writer contention was structurally impossible.

After #1469: both can run at once (confirmed live — this is how a duplicate-tray-icon situation was diagnosed today, two separate launchd jobs app.nodespace.daemon and app.nodespace.daemon.dev both holding the daemon role simultaneously). If both happen to write to the shared nodespace.db at overlapping moments:

  • Best case: the write retries transparently (requires the fix below — doesn't happen today).
  • Today's actual behavior: a write from one process while the other holds the write lock fails immediately with SQLITE_BUSY, surfacing as a hard error to whichever caller lost the race. WAL mode's design makes actual data corruption unlikely — SQLite's file-format-level guarantees hold regardless — but a failed write is still a real, user-visible bug (e.g. an edit that silently doesn't save).

Proposed fix

Add a busy_timeout PRAGMA to packages/core/src/db/schema.sql, alongside the existing PRAGMAs:

PRAGMA journal_mode = WAL;
PRAGMA foreign_keys = ON;
PRAGMA synchronous = NORMAL;
PRAGMA busy_timeout = 5000;  -- ms; retry instead of failing immediately on SQLITE_BUSY

Note busy_timeout is a per-connection setting (unlike journal_mode, which is persisted in the DB file itself) — it resets to 0 for every new connection, so it must be applied every time a connection opens, not just once at DB creation. schema.sql is already run inside SqliteStore::new() (packages/core/src/db/sqlite_store.rs, around line 158) on every connection open, so adding it there is the correct place — no separate one-time-migration handling needed.

5000ms is a starting suggestion, not a hard requirement — pick a value that comfortably covers a single write transaction's duration without making a genuinely stuck lock hang the caller for too long.

To investigate / decide

  • Confirmed: no busy_timeout or SQLITE_BUSY retry logic exists today.
  • Decide: is running dev + production daemons concurrently against the same DB an intentional, accepted scenario (e.g. "I want my dev build and prod app to see the same live data"), or should the two be prevented from running at the same time via some other mechanism (e.g. dev daemon refuses to start / warns if it detects a production daemon already holds the DB, or vice versa)? The busy_timeout fix is a good safety net either way, but doesn't replace this product decision if genuinely-simultaneous heavy writes from two daemons turns out to be common rather than a rare edge case.
  • Add the busy_timeout PRAGMA above to schema.sql.
  • Add a regression test if practical (e.g. two SqliteStore instances opened against the same file, one holding a write transaction while the other attempts a write, confirming the second retries/succeeds within the timeout rather than erroring immediately).

Related

Activity

  1. malibio commented on Jul 2, 2026

    @malibio
    CollaboratorAuthor

    Frontend baseline: 147 test files passed, 3941 tests passed.

  2. self-assigned this
    on Jul 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions