* 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
()
| 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 |
no test coverage detected