mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(studio-server): history lock records are replaced, never written through links (#7821)
* fix(studio-server): replace lock start records by rename The owner lock's start records (owner.pid.start, owner.pid.evict.start) and the daily prune stamp are written through replaceFileAtomically: an exclusive temp sibling renamed over the name. A symlink, hard link or other entry already at that name is replaced rather than written through. Lock files (owner.pid, its start record, owner-url.json) are read with O_NOFOLLOW and O_NONBLOCK and only when they are regular files, so a link is not followed and a FIFO is not waited on. * fix(studio-server): clear lock records before replacing them The start records and the daily prune stamp are removed before the rename, so a symlink to a read-only file or a read-only file at that name no longer fails the write. The prune stamp is read without following links or waiting on a FIFO, like the other lock files.
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
// @vitest-environment node
|
||||
import { execFileSync, spawn } from "node:child_process";
|
||||
import {
|
||||
chmodSync,
|
||||
existsSync,
|
||||
linkSync,
|
||||
lstatSync,
|
||||
mkdtempSync,
|
||||
readFileSync,
|
||||
readdirSync,
|
||||
renameSync,
|
||||
rmSync,
|
||||
symlinkSync,
|
||||
utimesSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
@@ -251,7 +254,7 @@ describe.skipIf(process.platform === "win32")(
|
||||
() => {
|
||||
it("gives the lock back instead of keeping a file nobody would release", async () => {
|
||||
vi.mocked(writeFileSync).mockImplementation((...args) => {
|
||||
if (String(args[0]).endsWith(".start")) {
|
||||
if (/\.start(\.hf[0-9a-f]{6}\.tmp)?$/.test(String(args[0]))) {
|
||||
throw Object.assign(new Error("ENOSPC: no space left on device"), { code: "ENOSPC" });
|
||||
}
|
||||
return realWriteFileSync(...args);
|
||||
@@ -265,3 +268,106 @@ describe.skipIf(process.platform === "win32")(
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
// Windows records no start time, and making links there needs extra rights.
|
||||
describe.skipIf(process.platform === "win32" || processStart(process.pid) === null)(
|
||||
"takeHistoryOwnership with a linked entry at a start record's name",
|
||||
() => {
|
||||
const records = ["owner.pid.start", "owner.pid.evict.start"];
|
||||
|
||||
/** A lock home and a sentinel file outside it; a dead owner too, so the evict lock writes its record. */
|
||||
function planted(record: string) {
|
||||
const home = newHome();
|
||||
const outside = newHome();
|
||||
const sentinel = join(outside, "sentinel");
|
||||
writeFileSync(sentinel, "unchanged");
|
||||
if (record === "owner.pid.evict.start") writeFileSync(join(home, "owner.pid"), "2147483646");
|
||||
return { home, outside, sentinel, entry: join(home, record) };
|
||||
}
|
||||
|
||||
async function takeAndRelease(home: string): Promise<void> {
|
||||
const release = await takeHistoryOwnership(home, 1_000);
|
||||
expect(lstatSync(join(home, "owner.pid.start")).isFile()).toBe(true);
|
||||
release();
|
||||
expect(readdirSync(home)).toEqual([]);
|
||||
}
|
||||
|
||||
it.each(records)("replaces a symlink at %s instead of writing through it", async (record) => {
|
||||
const { home, sentinel, entry } = planted(record);
|
||||
symlinkSync(sentinel, entry);
|
||||
await takeAndRelease(home);
|
||||
expect(readFileSync(sentinel, "utf-8")).toBe("unchanged");
|
||||
});
|
||||
|
||||
it.each(records)("creates nothing where a dangling symlink at %s points", async (record) => {
|
||||
const { home, outside, entry } = planted(record);
|
||||
const target = join(outside, "created");
|
||||
symlinkSync(target, entry);
|
||||
await takeAndRelease(home);
|
||||
expect(existsSync(target)).toBe(false);
|
||||
});
|
||||
|
||||
it.each(records)("leaves a file hard-linked at %s as it was", async (record) => {
|
||||
const { home, sentinel, entry } = planted(record);
|
||||
linkSync(sentinel, entry);
|
||||
await takeAndRelease(home);
|
||||
expect(readFileSync(sentinel, "utf-8")).toBe("unchanged");
|
||||
});
|
||||
|
||||
it("reads a FIFO at the start record as no record instead of waiting for a writer", async () => {
|
||||
const home = newHome();
|
||||
writeFileSync(join(home, "owner.pid"), String(process.ppid));
|
||||
const fifo = join(home, "owner.pid.start");
|
||||
execFileSync("mkfifo", [fifo]);
|
||||
const writer = spawn("sh", ["-c", 'sleep 3; while :; do printf x > "$0"; done', fifo], {
|
||||
stdio: "ignore",
|
||||
});
|
||||
try {
|
||||
const began = Date.now();
|
||||
await expect(takeHistoryOwnership(home, 100)).rejects.toThrow(HistoryBusyError);
|
||||
expect(Date.now() - began).toBeLessThan(2_000);
|
||||
} finally {
|
||||
writer.kill("SIGKILL");
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
describe.skipIf(!canRefuseRead || processStart(process.pid) === null)(
|
||||
"takeHistoryOwnership with a read-only entry at the start record's name",
|
||||
() => {
|
||||
it.each(["symlink", "file"])("takes the lock past a read-only %s", async (kind) => {
|
||||
const home = newHome();
|
||||
const sentinel = join(newHome(), "sentinel");
|
||||
writeFileSync(sentinel, "unchanged");
|
||||
chmodSync(sentinel, 0o444);
|
||||
const entry = join(home, "owner.pid.start");
|
||||
if (kind === "symlink") symlinkSync(sentinel, entry);
|
||||
else {
|
||||
writeFileSync(entry, "unchanged");
|
||||
chmodSync(entry, 0o444);
|
||||
}
|
||||
const release = await takeHistoryOwnership(home, 1_000);
|
||||
expect(lstatSync(entry).isFile()).toBe(true);
|
||||
release();
|
||||
expect(readFileSync(sentinel, "utf-8")).toBe("unchanged");
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
describe.skipIf(process.platform === "win32")(
|
||||
"takeHistoryOwnership with a symlink at owner.pid",
|
||||
() => {
|
||||
it("clears the link as a lock without a pid and leaves its target as it was", async () => {
|
||||
const home = newHome();
|
||||
const sentinel = join(newHome(), "sentinel");
|
||||
writeFileSync(sentinel, String(process.ppid));
|
||||
symlinkSync(sentinel, join(home, "owner.pid"));
|
||||
const release = await takeHistoryOwnership(home, 1_000);
|
||||
expect(lstatSync(join(home, "owner.pid")).isFile()).toBe(true);
|
||||
expect(readFileSync(join(home, "owner.pid"), "utf-8")).toBe(String(process.pid));
|
||||
release();
|
||||
expect(readFileSync(sentinel, "utf-8")).toBe(String(process.ppid));
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
@@ -1,6 +1,17 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { linkSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
||||
import {
|
||||
closeSync,
|
||||
constants,
|
||||
fstatSync,
|
||||
linkSync,
|
||||
mkdirSync,
|
||||
openSync,
|
||||
readFileSync,
|
||||
rmSync,
|
||||
statSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { uptime } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { cannotHardLink, replaceFileAtomically } from "@hyperframes/core/atomic-file";
|
||||
@@ -18,6 +29,9 @@ export class HistoryBusyError extends Error {
|
||||
const WINDOWS_BUSY = new Set(["EPERM", "EBUSY", "EACCES"]);
|
||||
const TAKING_MS = 2_000;
|
||||
const BOOT_SLACK_MS = 60_000;
|
||||
const NO_FOLLOW = constants.O_NOFOLLOW ?? 0;
|
||||
const NON_BLOCK = constants.O_NONBLOCK ?? 0;
|
||||
const LINKED = new Set(["ELOOP", "EMLINK"]);
|
||||
|
||||
function busyOnWindows(error: unknown): boolean {
|
||||
return (
|
||||
@@ -53,18 +67,33 @@ export function processStart(pid: number): string | null {
|
||||
}
|
||||
}
|
||||
|
||||
/** The text of `path` when it names a regular file, else null; a link is never followed, a FIFO never waited on. */
|
||||
export function readPlainFile(path: string): string | null {
|
||||
const fd = openSync(path, constants.O_RDONLY | NO_FOLLOW | NON_BLOCK);
|
||||
try {
|
||||
return fstatSync(fd).isFile() ? readFileSync(fd, "utf-8") : null;
|
||||
} finally {
|
||||
closeSync(fd);
|
||||
}
|
||||
}
|
||||
|
||||
function recordedStart(file: string, pid: number): string | null {
|
||||
try {
|
||||
const [owner, ...start] = readFileSync(`${file}.start`, "utf-8").split("\n");
|
||||
const [owner, ...start] = (readPlainFile(`${file}.start`) ?? "").split("\n");
|
||||
return Number(owner) === pid && start.length > 0 ? start.join("\n") : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function replaceOwnFile(path: string, content: string): void {
|
||||
rmSync(path, { force: true });
|
||||
replaceFileAtomically(path, content);
|
||||
}
|
||||
|
||||
function recordStart(file: string): void {
|
||||
const start = processStart(process.pid);
|
||||
if (start) writeFileSync(`${file}.start`, `${process.pid}\n${start}`);
|
||||
if (start) replaceOwnFile(`${file}.start`, `${process.pid}\n${start}`);
|
||||
}
|
||||
|
||||
/** Whether `pid` still runs the process that took `file`: its recorded start matches, else the lock postdates boot. */
|
||||
@@ -82,13 +111,15 @@ function holdsLock(file: string, pid: number): boolean {
|
||||
}
|
||||
}
|
||||
|
||||
/** The pid in `file`; null when there is no file, NaN when it holds no pid (so it reads as dead). */
|
||||
/** The pid in `file`; null when there is no file, NaN when it holds no pid or is no plain file (so it reads as dead). */
|
||||
function ownerOf(file: string): number | null {
|
||||
try {
|
||||
const text = readFileSync(file, "utf-8");
|
||||
return /^\d+$/.test(text) ? Number(text) : Number.NaN;
|
||||
const text = readPlainFile(file);
|
||||
return text !== null && /^\d+$/.test(text) ? Number(text) : Number.NaN;
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
|
||||
const code = (error as NodeJS.ErrnoException).code ?? "";
|
||||
if (code === "ENOENT") return null;
|
||||
if (LINKED.has(code)) return Number.NaN;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -184,7 +215,7 @@ function advertise(urlFile: string, url: string | undefined): void {
|
||||
/** The url `pid` advertised, only while `pid` is alive and still holds `file`. */
|
||||
function servedBy(file: string, urlFile: string, pid: number): string | undefined {
|
||||
try {
|
||||
const said = JSON.parse(readFileSync(urlFile, "utf-8")) as { pid?: unknown; url?: unknown };
|
||||
const said = JSON.parse(readPlainFile(urlFile) ?? "") as { pid?: unknown; url?: unknown };
|
||||
const live = said.pid === pid && holdsLock(file, pid) && ownerOf(file) === pid;
|
||||
return live && typeof said.url === "string" ? said.url : undefined;
|
||||
} catch {
|
||||
|
||||
@@ -3,11 +3,14 @@ import {
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
mkdtempSync,
|
||||
lstatSync,
|
||||
readFileSync,
|
||||
rmSync,
|
||||
symlinkSync,
|
||||
utimesSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { execFileSync, spawn } from "node:child_process";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import { describe, expect, it, onTestFinished, vi } from "vitest";
|
||||
@@ -18,6 +21,7 @@ import {
|
||||
KEEP_GONE_PROJECT_HISTORY_MS,
|
||||
listProjectHistories,
|
||||
pruneGoneProjectHistories,
|
||||
pruneGoneProjectHistoriesDaily,
|
||||
} from "./pruneHistories.js";
|
||||
|
||||
vi.mock("./ownerLock.js", async (importOriginal) => {
|
||||
@@ -327,3 +331,37 @@ describe("listProjectHistories", () => {
|
||||
expect(listProjectHistories(join(tmpdir(), "hf-no-such-history-root"))).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe.skipIf(process.platform === "win32")("pruneGoneProjectHistoriesDaily", () => {
|
||||
it("replaces a symlink at the last-prune stamp instead of writing through it", async () => {
|
||||
const historyRoot = tempDir("hf-prune-root-");
|
||||
const sentinel = join(tempDir("hf-prune-outside-"), "sentinel");
|
||||
writeFileSync(sentinel, "unchanged");
|
||||
const stamp = join(historyRoot, ".last-prune");
|
||||
symlinkSync(sentinel, stamp);
|
||||
pruneGoneProjectHistoriesDaily(historyRoot, Date.now());
|
||||
await vi.waitFor(() => expect(lstatSync(stamp).isFile()).toBe(true));
|
||||
expect(readFileSync(sentinel, "utf-8")).toBe("unchanged");
|
||||
});
|
||||
});
|
||||
|
||||
describe.skipIf(process.platform === "win32")("pruneGoneProjectHistoriesDaily with a FIFO", () => {
|
||||
it("reads a FIFO at the last-prune stamp as no stamp instead of waiting for a writer", async () => {
|
||||
const historyRoot = tempDir("hf-prune-root-");
|
||||
const stamp = join(historyRoot, ".last-prune");
|
||||
execFileSync("mkfifo", [stamp]);
|
||||
const writer = spawn("sh", ["-c", 'sleep 3; while :; do printf x > "$0"; done', stamp], {
|
||||
stdio: "ignore",
|
||||
});
|
||||
let elapsed: number;
|
||||
try {
|
||||
const began = Date.now();
|
||||
pruneGoneProjectHistoriesDaily(historyRoot, Date.now());
|
||||
elapsed = Date.now() - began;
|
||||
} finally {
|
||||
writer.kill("SIGKILL");
|
||||
}
|
||||
expect(elapsed).toBeLessThan(2_000);
|
||||
await vi.waitFor(() => expect(lstatSync(stamp).isFile()).toBe(true));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { readdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
|
||||
import { readdirSync, renameSync, statSync } from "node:fs";
|
||||
import { lstat, readdir, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import { isSafePath } from "../helpers/safePath.js";
|
||||
import { ID_PATH, isHistoryId, readId, readRecord } from "./historyId.js";
|
||||
import { HistoryBusyError, takeHistoryOwnership } from "./ownerLock.js";
|
||||
import {
|
||||
HistoryBusyError,
|
||||
readPlainFile,
|
||||
replaceOwnFile,
|
||||
takeHistoryOwnership,
|
||||
} from "./ownerLock.js";
|
||||
|
||||
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
/** A gone project's history stays this long after its last use, so a moved project reopened in time keeps it. */
|
||||
@@ -200,7 +205,7 @@ async function bytesIn(dir: string): Promise<number> {
|
||||
|
||||
function prunedWithinADay(stamp: string, now: number): boolean {
|
||||
try {
|
||||
const last = Number(readFileSync(stamp, "utf-8"));
|
||||
const last = Number(readPlainFile(stamp) ?? "");
|
||||
return last <= now && now - last < DAY_MS;
|
||||
} catch {
|
||||
return false;
|
||||
@@ -220,7 +225,7 @@ export function pruneGoneProjectHistoriesDaily(
|
||||
pruning.add(historyRoot);
|
||||
setImmediate(() => {
|
||||
pruneWithin(historyRoot, { now, onError, budgetMs, measure: false })
|
||||
.then(({ finished }) => finished && writeFileSync(stamp, String(now)))
|
||||
.then(({ finished }) => finished && replaceOwnFile(stamp, String(now)))
|
||||
.catch((error) => onError?.(error))
|
||||
.finally(() => pruning.delete(historyRoot));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user