fix(cli): a received-files write waits out a held lock instead of failing on Windows (#8367)

* test(cli): a received-files write waits while Windows still holds the lock file

* fix(cli): a received-files write waits out a held lock instead of failing on Windows

* test(cli): the held-lock test matches the lock by its ending, as Windows spells temp short
This commit is contained in:
Miguel Ángel
2026-11-21 11:33:20 +00:00
committed by GitHub
parent b0d9f36159
commit f74e1545c0
2 changed files with 28 additions and 1 deletions
@@ -29,6 +29,7 @@ import {
const zeroFileIds = vi.hoisted(() => ({ on: false }));
const opens = vi.hoisted(() => ({ path: "", seen: 0, replacing: 0 }));
const refusals = vi.hoisted(() => ({ ending: "", left: 0 }));
vi.mock("node:fs", async (importOriginal) => {
const actual = await importOriginal<typeof import("node:fs")>();
@@ -37,6 +38,12 @@ vi.mock("node:fs", async (importOriginal) => {
return zeroFileIds.on && stat ? Object.assign(stat, { dev: 0n, ino: 0n }) : stat;
}) as typeof actual.lstatSync;
const openSync = ((...args: Parameters<typeof actual.openSync>) => {
const opened = String(args[0]).replaceAll("\\", "/");
if (refusals.left > 0 && refusals.ending && opened.endsWith(refusals.ending)) {
refusals.left -= 1;
const code = process.platform === "win32" ? "EPERM" : "EBUSY";
throw Object.assign(new Error(`${code}: open '${opened}'`), { code });
}
const fd = actual.openSync(...args);
if (args[0] !== opens.path) return fd;
opens.seen += 1;
@@ -55,6 +62,7 @@ let dir: string | undefined;
afterEach(() => {
zeroFileIds.on = false;
Object.assign(opens, { path: "", seen: 0, replacing: 0 });
Object.assign(refusals, { ending: "", left: 0 });
vi.unstubAllEnvs();
if (dir) rmSync(dir, { recursive: true, force: true });
dir = undefined;
@@ -551,6 +559,17 @@ describe("recordReceivedFiles", () => {
expect(listed(root)).toEqual(["hyperframes.json", "mine.html", "theirs.html"]);
});
// fails without the fix: Windows refuses a new lock with EPERM while the last writer's is still being deleted, and
// that failed the write instead of waiting (CI: a Windows run of the test above).
it("waits while the lock file is held (EPERM on Windows, EBUSY elsewhere), then takes it", () => {
const root = project();
// Matched by its ending: Windows may spell the temp folder short (RUNNER~1) where the code resolves it long.
Object.assign(refusals, { ending: "/.hyperframes/received-files.json.lock", left: 2 });
expect(recordReceivedFiles(root, ["mine.html"])).toEqual(["mine.html"]);
expect(refusals.left).toBe(0);
expect(listed(root)).toEqual(["hyperframes.json", "mine.html"]);
});
it("takes a project folder however its path is spelled", () => {
const root = join(project(), "Project");
mkdirSync(root);
+9 -1
View File
@@ -26,6 +26,7 @@ import {
} from "@hyperframes/core/safe-path";
import { registryRoot } from "../registry/publication.js";
import { readPlainTail, recordFolder, writeRecord } from "./projectRecords.js";
import { isHeldError } from "./renderLockOwners.js";
import { walkBudget } from "./walkBudget.js";
import {
contentHash,
@@ -283,13 +284,20 @@ function sleepSync(ms: number): void {
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
function tryRecordLock(lock: string, deadline: number): "taken" | "wait" {
function openRecordLock(lock: string, deadline: number): "taken" | "held" | "exists" {
try {
closeSync(openSync(lock, "wx"));
return "taken";
} catch (error) {
if (isHeldError(error) && Date.now() < deadline) return "held";
if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
return "exists";
}
}
function tryRecordLock(lock: string, deadline: number): "taken" | "wait" {
const opened = openRecordLock(lock, deadline);
if (opened !== "exists") return opened === "taken" ? "taken" : "wait";
const heldFor = Date.now() - (statSync(lock, { throwIfNoEntry: false })?.mtimeMs ?? Date.now());
if (heldFor > LOCK_STALE_MS || heldFor < -LOCK_CLOCK_SLACK_MS) rmSync(lock, { force: true });
else if (Date.now() >= deadline)