Repository navigation
fix(devkitd): prevent dual live daemons during autostart (#495) - #503
Open
codeaholicguy wants to merge 2 commits into
Open
codeaholicguy wants to merge 2 commits into
codeaholicguy wants to merge 2 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #495
Root cause
TLA+ counterexamples on
feature-tla-analysismatch production: a client could releasedaemon.sock.spawn.lockafter the 3s socket wait while the spawneddevkitdwas still inDaemon::new()and the firstapply_sweep(). A second client could spawn anotherdevkitd. Both openeddaemon.dband raced throughserve()'s connect-then-unlink-then-bind path, breakingInv_AtMostOneLiveDaemonand sometimes unlinking a live socket (Inv_NeverUnlinkLiveSocket).Fix
Rust (
devkitd serve): Exclusive non-blockingflockon~/.ai-devkit/daemon.lockbeforeDaemon::new()or any SQLite work. The lock is held for the process lifetime; the kernel releases it when the process exits, so a leftoverdaemon.lockfile after a crash does not block a new instance. Bind the Unix socket, then run the one-shot warm-upapply_sweep(), then enter theacceptloop.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 infinallylike before so short-lived CLI processes never block on background timers. Write the spawned devkitd pid into the lock file;acquireSpawnLockreclaims when that pid is dead or the lock is older thanSTALE_LOCK_MS. Twin prevention is primarilydaemon.lock; the spawn lock stays defense in depth for concurrent client starters.No change to
apply_sweepconcurrency inside the discovery loop (that is #493).Tests
instance_lock_allows_one_holderinrust/crates/devkitd/src/server.rsautostart-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:
All passed.
Risks
A second
devkitd serveexits 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 servecreates~/.ai-devkit/daemon.lockand 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.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 emptyagent.list.ensureDaemonreturns; no extended hold or background polling.Overlap with open PRs
No edits to
durable.rs(#489) or theagent.lookup/listed_agent/apply_sweepdoc area in #481.AI DevKit skills
Issue context and TLA traces from
feature-tla-analysis, repositoryAGENTS.mdtest placement, and humanizer for this description.Tooling friction
Pre-commit runs the full Rust workspace test suite on commit.
private release-ai-devkitskill install failure during setup is expected.