MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / reopenIfReplaced

Method reopenIfReplaced

src/index.ts:249–264  ·  view source on GitHub ↗

* Heal a stale database handle in place. If `.codegraph/` was removed and * recreated at the SAME path while this instance held the DB open — a git * worktree removed and re-added, or `rm -rf .codegraph` + `codegraph init` — * our open fd points at the now-unlinked inode and can never see t

()

Source from the content-addressed store, hash-verified

247 * file can't be unlinked there, and st_ino is unreliable).
248 */
249 reopenIfReplaced(): boolean {
250 if (!this.db.isReplacedOnDisk()) return false;
251 const dbPath = this.db.getPath();
252 // Open the live file FIRST — if that throws (e.g. mid-recreate), the old
253 // handle stays in place and the caller retries on the next query, rather
254 // than leaving this instance with no connection at all.
255 const fresh = DatabaseConnection.open(dbPath);
256 const stale = this.db;
257 this.db = fresh;
258 this.queries = new QueryBuilder(fresh.getDb());
259 this.wireLayers();
260 // Releasing the dead handle also frees the leaked db/-wal/-shm fds that were
261 // pinning the unlinked inode (#925).
262 try { stale.close(); } catch { /* the old inode is gone; closing just frees fds */ }
263 return true;
264 }
265
266 // ===========================================================================
267 // Lifecycle Methods

Callers 2

freshenMethod · 0.80

Calls 6

wireLayersMethod · 0.95
isReplacedOnDiskMethod · 0.80
getPathMethod · 0.80
getDbMethod · 0.80
closeMethod · 0.65
openMethod · 0.45

Tested by

no test coverage detected