fix(mcp): reopen the database when it's replaced on disk instead of serving a deleted inode (#925) (#940)
A long-lived `serve --mcp` process opens `.codegraph/codegraph.db` and holds the fd for its whole life. If `.codegraph/` is removed and recreated AT THE SAME PATH while it runs — `git worktree remove <p>` + re-add, or `rm -rf .codegraph` + `codegraph init` — the held fd points at the now-unlinked inode and can never see the new index. The server serves the pre-removal snapshot (renamed/removed symbols still "live", new ones missing); `codegraph sync` can't refresh it and the CLI (a fresh process) diverges. Only a restart fixed it — and because the daemon registry is keyed by path, a same-path recreate routes new clients straight back to the same stale daemon, so the fix has to self-heal inside the running process. - DatabaseConnection records the DB file's (dev, ino) at open and exposes isReplacedOnDisk() — a different inode now at the same path. POSIX-gated: Windows can't unlink an open file and its st_ino is unreliable, so it never fires there. - CodeGraph.reopenIfReplaced() opens the live file first, then swaps the connection + query layers IN PLACE (via the new wireLayers() helper), so every holder of the instance (the daemon's default project, cached projectPath connections) heals without a restart. Closing the dead handle also frees the leaked db/-wal/-shm fds pinning the unlinked inode. - ToolHandler.getCodeGraph calls it (freshen) before serving — one stat() per call, a no-op unless the inode actually changed, never throws into a tool. Tests cover isReplacedOnDisk (unchanged / replaced / absent / Windows-gated) and an end-to-end reopen that heals a held instance after a same-path recreate (asserts the pre-heal staleness too). Validated on macOS with a dist probe of the raw instance and the MCP serving path; full suite green. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
62d5cdde2c
commit
e43ac82cdf
@@ -44,11 +44,21 @@ export class DatabaseConnection {
|
||||
private db: SqliteDatabase;
|
||||
private dbPath: string;
|
||||
private backend: SqliteBackend;
|
||||
/**
|
||||
* `dev:ino` of the DB file at the moment we opened it (or null when the
|
||||
* platform/filesystem reports no usable inode). Lets us notice when the file
|
||||
* we hold open has been unlinked and REPLACED by a new file at the same path
|
||||
* — a git worktree removed and re-added, or `.codegraph/` deleted and
|
||||
* re-`init`ed under a long-lived server — at which point our fd reads a now
|
||||
* dead inode forever (#925). See `isReplacedOnDisk`.
|
||||
*/
|
||||
private openedInode: string | null;
|
||||
|
||||
private constructor(db: SqliteDatabase, dbPath: string, backend: SqliteBackend) {
|
||||
this.db = db;
|
||||
this.dbPath = dbPath;
|
||||
this.backend = backend;
|
||||
this.openedInode = statInode(dbPath);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -230,6 +240,40 @@ export class DatabaseConnection {
|
||||
isOpen(): boolean {
|
||||
return this.db.open;
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the DB file at our path has been REPLACED on disk since we opened
|
||||
* it — a different inode now lives at the same path, so the fd we still hold
|
||||
* points at a now-unlinked inode that can never receive new writes (#925).
|
||||
* The trigger is removing and recreating `.codegraph/` at the same path under
|
||||
* a long-lived process (`git worktree remove` + re-add, or `rm -rf
|
||||
* .codegraph` + `codegraph init`). Returns false when the inode is unchanged,
|
||||
* when the file is momentarily absent (mid-recreate — nothing to reopen onto
|
||||
* yet), or when the platform doesn't report a usable inode (Windows can't
|
||||
* unlink an open file and its st_ino is unreliable, so this never fires there).
|
||||
*/
|
||||
isReplacedOnDisk(): boolean {
|
||||
if (this.openedInode === null) return false;
|
||||
const current = statInode(this.dbPath);
|
||||
return current !== null && current !== this.openedInode;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* `dev:ino` for a path, or null if it can't be stat'd or the platform doesn't
|
||||
* report a usable inode. Windows st_ino is unreliable across handle reopens, so
|
||||
* we deliberately return null there — the deleted-but-open-inode hazard this
|
||||
* guards (#925) is a POSIX file-semantics issue that doesn't arise on Windows
|
||||
* (an open file can't be unlinked).
|
||||
*/
|
||||
function statInode(p: string): string | null {
|
||||
if (process.platform === 'win32') return null;
|
||||
try {
|
||||
const s = fs.statSync(p);
|
||||
return `${s.dev}:${s.ino}`;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user