Skip to content

fix(devkitd): prevent dual live daemons during autostart (#495) - #503

Open
codeaholicguy wants to merge 2 commits into
mainfrom
cursor/fix-dual-daemon-autostart-dda9
Open

codeaholicguy wants to merge 2 commits into
mainfrom
cursor/fix-dual-daemon-autostart-dda9

Conversation

@codeaholicguy

@codeaholicguy codeaholicguy commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

Closes #495

Root cause

TLA+ counterexamples on feature-tla-analysis match production: a client could release daemon.sock.spawn.lock after the 3s socket wait while the spawned devkitd was still in Daemon::new() and the first apply_sweep(). A second client could spawn another devkitd. Both opened daemon.db and raced through serve()'s connect-then-unlink-then-bind path, breaking Inv_AtMostOneLiveDaemon and sometimes unlinking a live socket (Inv_NeverUnlinkLiveSocket).

Fix

Rust (devkitd serve): Exclusive non-blocking flock on ~/.ai-devkit/daemon.lock before Daemon::new() or any SQLite work. The lock is held for the process lifetime; the kernel releases it when the process exits, so a leftover daemon.lock file after a crash does not block a new instance. Bind the Unix socket, then run the one-shot warm-up apply_sweep(), then enter the accept loop. serve() keeps a single connect probe before unlinking a stale socket file; singleton enforcement is the flock, not double connect.

TypeScript (ensureDaemon): Release the spawn lock in finally like before so short-lived CLI processes never block on background timers. Write the spawned devkitd pid into the lock file; acquireSpawnLock reclaims when that pid is dead or the lock is older than STALE_LOCK_MS. Twin prevention is primarily daemon.lock; the spawn lock stays defense in depth for concurrent client starters.

No change to apply_sweep concurrency inside the discovery loop (that is #493).

Tests

  • instance_lock_allows_one_holder in rust/crates/devkitd/src/server.rs
  • autostart-spawn-lock.test.ts: lock released on wait timeout, reclaim when recorded pid is dead, subprocess exits within 2s when devkitd binds slowly (no event-loop hang)

Commands run on this branch:

cargo test --workspace
cargo fmt --all
cargo clippy --workspace -- -D warnings
npx vitest run packages/daemon-client/tests
cd packages/daemon-client && npm run lint

All passed.

Risks

A second devkitd serve exits with "devkitd already running" while another instance holds the flock. If two clients autostart at once before the socket exists, both may spawn; the second devkitd exits immediately on flock contention without touching SQLite. The first client may return null if the wait times out while boot is still slow; the CLI degrades as today.

Behavior changes

  • devkitd serve creates ~/.ai-devkit/daemon.lock and refuses to start if another live instance holds the flock. A stale file on disk after crash is harmless: flock is per open fd, not the inode persisting on disk.
  • The socket path appears before the warm-up sweep finishes, but accept() does not run until that sweep completes. Early connect attempts sit in the listen backlog; the first handled RPC sees a warmed cache, not an empty agent.list.
  • Client spawn lock is always released when ensureDaemon returns; no extended hold or background polling.

Overlap with open PRs

No edits to durable.rs (#489) or the agent.lookup / listed_agent / apply_sweep doc area in #481.

AI DevKit skills

Issue context and TLA traces from feature-tla-analysis, repository AGENTS.md test placement, and humanizer for this description.

Tooling friction

Pre-commit runs the full Rust workspace test suite on commit. private release-ai-devkit skill install failure during setup is expected.

Open in Web Open in Cursor 

cursoragent and others added 2 commits November 27, 2026 16:06
Acquire a kernel flock on daemon.lock before SQLite init, bind the Unix
socket before the warm-up sweep, and keep the client spawn lock until
the spawned devkitd is serving or has exited.
Release the client spawn lock in finally (daemon.lock flock is the
singleton guard). Record the spawned devkitd pid in the lock file for
reclaim when the holder dies. Remove the duplicate socket connect probe.

Co-authored-by: Hoang Nguyen <codeaholicguy@users.noreply.github.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(devkitd): daemon autostart can spawn dual live daemons and unlink a live socket (Inv_AtMostOneLiveDaemon / Inv_NeverUnlinkLiveSocket)

3 participants