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
+28
-3
@@ -820,7 +820,7 @@ export class ToolHandler {
|
||||
"and don't call codegraph again this session — the user can run 'codegraph init' to enable it."
|
||||
);
|
||||
}
|
||||
return this.cg;
|
||||
return this.freshen(this.cg);
|
||||
}
|
||||
|
||||
// Reject sensitive system directories before opening. Only validate a
|
||||
@@ -863,20 +863,45 @@ export class ToolHandler {
|
||||
// "database is locked" on concurrent tool calls. See issue #238. The
|
||||
// default instance is owned/closed by the server, so it's never cached.
|
||||
if (this.cg && this.cg.getProjectRoot() === resolvedRoot) {
|
||||
return this.cg;
|
||||
return this.freshen(this.cg);
|
||||
}
|
||||
|
||||
// Cache the open DB connection by RESOLVED ROOT only — never by the input
|
||||
// path. One key per instance means closeAll() closes each exactly once, and
|
||||
// a changed resolution maps to a different entry instead of a stale hit.
|
||||
const cached = this.projectCache.get(resolvedRoot);
|
||||
if (cached) return cached;
|
||||
if (cached) return this.freshen(cached);
|
||||
|
||||
const cg = loadCodeGraph().openSync(resolvedRoot);
|
||||
this.projectCache.set(resolvedRoot, cg);
|
||||
return cg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Heal a long-lived connection whose `.codegraph/` was removed and recreated
|
||||
* at the same path (a worktree recreated, or `rm -rf .codegraph` + re-init)
|
||||
* before handing it to a tool. Otherwise the daemon keeps serving the
|
||||
* pre-removal snapshot from its now-unlinked file handle until restart — and
|
||||
* because the daemon registry is keyed by path, a same-path recreate routes
|
||||
* new clients straight back to this same stale daemon (#925). The check is one
|
||||
* stat() and a no-op unless the inode actually changed; it never throws into a
|
||||
* tool call.
|
||||
*/
|
||||
private freshen(cg: CodeGraph): CodeGraph {
|
||||
try {
|
||||
if (cg.reopenIfReplaced()) {
|
||||
process.stderr.write(
|
||||
'[CodeGraph MCP] The index was replaced on disk (e.g. a git worktree ' +
|
||||
'recreated at the same path); reopened the live database in place.\n'
|
||||
);
|
||||
}
|
||||
} catch {
|
||||
// Best-effort self-heal — a failed reopen must never break the tool call;
|
||||
// the (still stale) handle keeps serving and the next call retries.
|
||||
}
|
||||
return cg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close all cached project connections
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user