Reclaim self-update .old backups left beside the binary
Windows cannot rename-replace a running executable, so the win32 update path renames the live image to `<binary>.old.<timestamp>` and never deleted it: one full binary (tens of MB) leaked per update, forever. The same file cannot be removed during the update that created it either — it is the executing image — so the sweep runs at the start of every binary-mode invocation instead, which includes the SWR background `update check --bg-save` that runs on its own. Orphaned `.tmp-update-<ts>` staging dirs from an interrupted run are swept by the same pass, gated on a one-hour mtime so a staging dir an update is actively downloading into is never pulled out from under it.
This commit is contained in:
1 parent
d10aebe5ab
commit
a55015ca26
2 files changed
+119
-3
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { chmodSync, mkdirSync, readdirSync, realpathSync, renameSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
|
||||
import { chmodSync, mkdirSync, readdirSync, realpathSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
||||
import { basename, dirname, isAbsolute, join, relative } from "node:path";
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { isCompiledBinary } from "../../core/runtime.js";
|
||||
@@ -177,6 +177,12 @@ export async function handleUpdateCommand(runtime: CliRuntime, options: UpdateOp
|
||||
const platform = detectPlatform();
|
||||
const arch = detectArch();
|
||||
|
||||
// Every binary-mode invocation sweeps here, not just real updates: the SWR
|
||||
// background check (`update check --bg-save`) is the only command many users
|
||||
// ever run after their last update, so it is what reclaims that update's
|
||||
// `.old.<timestamp>` backup (undeletable while its image was executing).
|
||||
pruneUpdateLeftovers(process.execPath);
|
||||
|
||||
const releases = await fetchGitHubReleases(repo, 30);
|
||||
const release = targetVersion
|
||||
? (releases.find((r) => r.version === targetVersion || r.tagName === targetVersion || r.tagName === `v${targetVersion}`) ??
|
||||
@@ -350,6 +356,61 @@ export async function handleUpdateCommand(runtime: CliRuntime, options: UpdateOp
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the debris earlier self-updates left next to the installed binary.
|
||||
*
|
||||
* Windows cannot `renameSync`-replace a running executable, so the win32 branch
|
||||
* first renames the live image to `<binary>.old.<timestamp>` and never removes
|
||||
* it — one full binary (tens of MB) leaked per update. The same file is also
|
||||
* undeletable *during* that update (it is the executing image), so cleanup runs
|
||||
* at the start of the next update instead, when nothing holds it open. Orphaned
|
||||
* `.tmp-update-<timestamp>` staging directories from an interrupted run are
|
||||
* swept for the same reason.
|
||||
*
|
||||
* Best-effort: a locked or already-gone file is skipped, never fatal. Returns
|
||||
* the entries removed so tests can assert the sweep.
|
||||
*/
|
||||
export function pruneUpdateLeftovers(binaryPath: string): string[] {
|
||||
const dir = dirname(binaryPath);
|
||||
const base = basename(binaryPath);
|
||||
const removed: string[] = [];
|
||||
|
||||
let entries: string[];
|
||||
try {
|
||||
entries = readdirSync(dir);
|
||||
} catch {
|
||||
return removed;
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
for (const entry of entries) {
|
||||
// `<binary>.old.<anything>` (e.g. seal-use.exe.old.1727...) and the
|
||||
// `.tmp-update-<ts>` staging dirs this command creates. Never the live
|
||||
// binary itself: its name has no `.old.` / `tmp-update` marker.
|
||||
const isStaleBackup = entry.startsWith(`${base}.old.`);
|
||||
const isOrphanStaging = entry.startsWith(".tmp-update-");
|
||||
if (!isStaleBackup && !isOrphanStaging) continue;
|
||||
|
||||
const fullPath = join(dir, entry);
|
||||
try {
|
||||
if (isOrphanStaging) {
|
||||
// A staging dir younger than an hour may belong to an update running
|
||||
// right now; deleting it out from under that download would break it.
|
||||
// The `.old.` case needs no such gate: on Windows a live image cannot
|
||||
// be deleted (the rm throws and is swallowed), and on POSIX no `.old.`
|
||||
// file is ever written.
|
||||
if (now - statSync(fullPath).mtimeMs < 60 * 60 * 1000) continue;
|
||||
}
|
||||
rmSync(fullPath, { recursive: true, force: true });
|
||||
removed.push(entry);
|
||||
} catch {
|
||||
// Still locked (its owning process is alive); the next update retries.
|
||||
}
|
||||
}
|
||||
|
||||
return removed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Installs the Agent Skill by running the newly installed binary as a child process, then reads
|
||||
* the file back to prove which version actually landed.
|
||||
|
||||
Reference in new issue
Block a user