fix: Stale lock recovery and MCP init retry

Fixes #47 — "database is locked" after crash and MCP "not initialized"
when project IS initialized.

- FileLock: treat locks older than 10 minutes as stale regardless of PID
  status, covering cases where PID was reused or kill signal check fails
- MCP server: log errors from tryInitializeDefault() to stderr instead of
  silently swallowing, so transient open failures are diagnosable
- MCP server: retryInitIfNeeded() properly cleans up failed instances
  before retrying, preventing resource leaks
- CLI: add 'codegraph unlock' command for manual lock file removal
This commit is contained in:
Colby McHenry
2026-03-18 14:50:25 -05:00
parent bdbe59b457
commit b964d5909a
3 changed files with 51 additions and 4 deletions
+31
View File
@@ -1036,6 +1036,37 @@ program
process.exit(0);
});
/**
* codegraph unlock [path]
*/
program
.command('unlock [path]')
.description('Remove a stale lock file that is blocking indexing')
.action(async (pathArg: string | undefined) => {
const projectPath = resolveProjectPath(pathArg);
try {
if (!isInitialized(projectPath)) {
error(`CodeGraph not initialized in ${projectPath}`);
return;
}
const lockPath = path.join(getCodeGraphDir(projectPath), 'codegraph.lock');
if (!fs.existsSync(lockPath)) {
info('No lock file found — nothing to do');
return;
}
fs.unlinkSync(lockPath);
success('Removed lock file. You can now run indexing again.');
} catch (err) {
captureException(err);
error(`Failed to remove lock: ${err instanceof Error ? err.message : String(err)}`);
process.exit(1);
}
});
/**
* codegraph install
*/