Repository navigation
Description
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.sqlsetsPRAGMA 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_timeoutis 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_BUSYrather than waiting/retrying. - Confirmed via
grepthat noSQLITE_BUSYhandling or retry logic exists anywhere inpackages/core/src/db/sqlite_store.rstoday — 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_BUSYNote 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_timeoutorSQLITE_BUSYretry 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_timeoutfix 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_timeoutPRAGMA above toschema.sql. - Add a regression test if practical (e.g. two
SqliteStoreinstances 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
- Daemon lifecycle: dev worktrees, production app, and Pro/community builds collide on shared launchd service + socket #1468 / Daemon lifecycle: dev worktrees, production app, and Pro/community builds collide on shared launchd service + socket #1469 — introduced the socket/label scoping that made this scenario newly possible
- ADR-031 — original single-writer daemon rationale, worth a note or amendment once this is resolved
- Found while investigating duplicate tray icons during Add Ornith 1.0 to model catalog (catalog entry, think-block routing, update_node prompt) #1465 testing
Activity
malibio commentedon Jul 2, 2026
Frontend baseline: 147 test files passed, 3941 tests passed.