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);
|
||||
|
||||
Reference in new issue
Block a user