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,8 +1,8 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, symlinkSync, utimesSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { compareSemVer, getMirrorMode, isHomebrewInstall, isNewerVersion, matchAssetForPlatform } from "./update.js";
|
||||
import { compareSemVer, getMirrorMode, isHomebrewInstall, isNewerVersion, matchAssetForPlatform, pruneUpdateLeftovers } from "./update.js";
|
||||
|
||||
type Asset = { name: string; downloadUrl: string; size: number };
|
||||
|
||||
@@ -72,6 +72,61 @@ describe("update skill install", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("update leftover pruning", () => {
|
||||
// Windows cannot rename-replace a running executable, so the win32 branch
|
||||
// renames the live image to `<binary>.old.<ts>` and — before this helper —
|
||||
// never removed it: one full binary leaked per update, forever. Cleanup can
|
||||
// only happen once that image stops executing, i.e. on a later invocation.
|
||||
test("removes stale .old backups but never the live binary", () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "seal-use-prune-old-"));
|
||||
try {
|
||||
const binary = join(dir, "seal-use");
|
||||
writeFileSync(binary, "current");
|
||||
writeFileSync(join(dir, "seal-use.old.1727000000000"), "previous release");
|
||||
writeFileSync(join(dir, "my-old-seal-use"), "unrelated file with 'old' in the name");
|
||||
|
||||
const removed = pruneUpdateLeftovers(binary);
|
||||
|
||||
expect(removed).toEqual(["seal-use.old.1727000000000"]);
|
||||
expect(readFileSync(binary, "utf8")).toBe("current");
|
||||
expect(readFileSync(join(dir, "my-old-seal-use"), "utf8")).toBe("unrelated file with 'old' in the name");
|
||||
expect(() => statSync(join(dir, "seal-use.old.1727000000000"))).toThrow();
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("sweeps only stale staging dirs, leaving a concurrent update's alone", () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "seal-use-prune-tmp-"));
|
||||
try {
|
||||
const binary = join(dir, "seal-use");
|
||||
writeFileSync(binary, "current");
|
||||
|
||||
const fresh = join(dir, `.tmp-update-${Date.now()}`);
|
||||
const orphan = join(dir, ".tmp-update-1727000000000");
|
||||
mkdirSync(fresh, { recursive: true });
|
||||
mkdirSync(orphan, { recursive: true });
|
||||
writeFileSync(join(orphan, "half-downloaded.bin"), "x");
|
||||
// Backdate the orphan past the one-hour safety window.
|
||||
const old = new Date(Date.now() - 2 * 60 * 60 * 1000);
|
||||
utimesSync(orphan, old, old);
|
||||
|
||||
const removed = pruneUpdateLeftovers(binary);
|
||||
|
||||
expect(removed).toEqual([".tmp-update-1727000000000"]);
|
||||
expect(() => statSync(orphan)).toThrow();
|
||||
// A staging dir this run (or a parallel update) may still be writing into.
|
||||
expect(statSync(fresh).isDirectory()).toBe(true);
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("is a no-op when the binary directory cannot be read", () => {
|
||||
expect(pruneUpdateLeftovers(join(tmpdir(), "does-not-exist", "seal-use"))).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("update version comparator", () => {
|
||||
test("correctly identifies newer semantic versions", () => {
|
||||
expect(isNewerVersion("0.7.0", "0.6.0")).toBe(true);
|
||||
|
||||
@@ -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