MCPcopy
hub / github.com/colbymchenry/codegraph / reopenIfReplaced

Method reopenIfReplaced

src/index.ts:206–221  ·  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

204 * file can't be unlinked there, and st_ino is unreliable).
205 */
206 reopenIfReplaced(): boolean {
207 if (!this.db.isReplacedOnDisk()) return false;
208 const dbPath = this.db.getPath();
209 // Open the live file FIRST — if that throws (e.g. mid-recreate), the old
210 // handle stays in place and the caller retries on the next query, rather
211 // than leaving this instance with no connection at all.
212 const fresh = DatabaseConnection.open(dbPath);
213 const stale = this.db;
214 this.db = fresh;
215 this.queries = new QueryBuilder(fresh.getDb());
216 this.wireLayers();
217 // Releasing the dead handle also frees the leaked db/-wal/-shm fds that were
218 // pinning the unlinked inode (#925).
219 try { stale.close(); } catch { /* the old inode is gone; closing just frees fds */ }
220 return true;
221 }
222
223 // ===========================================================================
224 // 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