fix(producer): a drive that refuses frame links gets a copy, whatever code it refuses with (#7163)

* fix(producer): a drive that refuses frame links gets a copy, whatever code it refuses with

* fix(producer): an entry already where the frames go still gets cleared and relinked

* test(producer): the relinked frame dir is read through its new link
This commit is contained in:
Miguel Ángel
2026-11-04 15:46:32 +00:00
committed by GitHub
parent ac1948a88b
commit 845bd638a7
2 changed files with 69 additions and 16 deletions
+11 -16
View File
@@ -581,6 +581,11 @@ export function createMemorySampler(intervalMs: number = 250): MemorySampler {
// One-time guard for the symlink→copy fallback notice below.
let warnedSymlinkFallback = false;
const copyCannotFix = (error: unknown): boolean =>
["ENOSPC", "EDQUOT", "EEXIST", "ENOENT"].includes(
(error as NodeJS.ErrnoException | undefined)?.code ?? "",
);
// Junctions avoid Windows symlink privileges. POSIX keeps its copy fallback.
function tryWindowsFrameJunction(
fileSystem: MaterializeFileSystem,
@@ -592,30 +597,20 @@ function tryWindowsFrameJunction(
fileSystem.symlinkSync(src, dest, "junction");
return true;
} catch (error) {
const code =
error instanceof Error && "code" in error && typeof error.code === "string"
? error.code
: undefined;
if (
["EPERM", "EACCES", "UNKNOWN", "EINVAL", "ENOSYS", "EOPNOTSUPP", "ENOTSUP"].includes(
code ?? "",
)
) {
return false;
}
throw error;
if (copyCannotFix(error)) throw error;
return false;
}
}
// Create the symlink, degrading to a copy on Windows' no-symlink-privilege
// errors (EPERM/EACCES, plus UNKNOWN), trying a Windows junction first.
// Other symlink errors and non-capability junction errors propagate.
// Create the symlink; a drive that refuses it, with any code, gets a Windows junction, then a copy.
// A full disk, a missing source or an entry already at dest (stageExtractedFrameDir clears a stale one)
// propagate: copying would not fix them.
function linkOrCopyFrameDir(fileSystem: MaterializeFileSystem, src: string, dest: string): void {
try {
fileSystem.symlinkSync(src, dest, "dir");
} catch (err) {
if (copyCannotFix(err)) throw err;
const code = (err as NodeJS.ErrnoException | undefined)?.code;
if (code !== "EPERM" && code !== "EACCES" && code !== "UNKNOWN") throw err;
if (tryWindowsFrameJunction(fileSystem, src, dest)) return;
// Copying is measurably slower than symlinking, so surface the degrade once
// — it explains a render that suddenly got heavier and saves a support
@@ -127,6 +127,19 @@ it("distributed materialization stays physically self-contained after cache remo
expect(readFileSync(extracted.framePaths.get(0)!, "utf8")).toBe("frame bytes");
});
it.skipIf(process.platform === "win32")(
"relinks over a dangling frame link left where the frames go",
() => {
const { root, compiled, extracted } = fixture();
const dest = join(compiled, "__hyperframes_video_frames", "clip");
mkdirSync(join(compiled, "__hyperframes_video_frames"), { recursive: true });
symlinkSync(join(root, "collected-cache"), dest, "dir");
materializeExtractedFramesForCompiledDir([extracted], compiled);
expect(lstatSync(dest).isSymbolicLink()).toBe(true);
expect(readFileSync(join(dest, "frame_000001.jpg"), "utf8")).toBe("frame bytes");
},
);
describe("Windows link fallbacks", () => {
const platform = Object.getOwnPropertyDescriptor(process, "platform")!;
beforeEach(() => Object.defineProperty(process, "platform", { value: "win32" }));
@@ -157,6 +170,51 @@ describe("Windows link fallbacks", () => {
},
);
it.each(["EISDIR", "EIO", "EINVAL"])(
"copies after the drive refuses the symlink with %s and the junction too",
(code) => {
const { compiled, extracted } = fixture();
const attempts: Array<string | undefined> = [];
materializeExtractedFramesForCompiledDir([extracted], compiled, {
fileSystem: {
existsSync,
mkdirSync,
cpSync,
rmSync,
symlinkSync: (_target, _path, type) => {
attempts.push(type);
throw Object.assign(new Error("link refused"), { code });
},
},
});
expect(attempts).toEqual(["dir", "junction"]);
expect(lstatSync(extracted.outputDir).isSymbolicLink()).toBe(false);
expect(readFileSync(extracted.framePaths.get(0)!, "utf8")).toBe("frame bytes");
},
);
it("does not copy onto a disk the symlink found full", () => {
const { compiled, extracted } = fixture();
const copy = vi.fn();
const attempts: Array<string | undefined> = [];
expect(() =>
materializeExtractedFramesForCompiledDir([extracted], compiled, {
fileSystem: {
existsSync,
mkdirSync,
rmSync,
cpSync: copy,
symlinkSync: (_target, _path, type) => {
attempts.push(type);
throw Object.assign(new Error("disk full"), { code: "ENOSPC" });
},
},
}),
).toThrow("disk full");
expect(attempts).toEqual(["dir"]);
expect(copy).not.toHaveBeenCalled();
});
it("does not hide a disk failure from the junction attempt", () => {
const { compiled, extracted } = fixture();
const copy = vi.fn();