Skip to content

Commit 706b7bf

Browse files
committed
fix(cli): give each export its own partial file, and remove it when the commit fails
Two exports to one output path shared "<out>.part": the second truncated the first's writes, and the first's rename moved the file the second was still writing. Each run now writes "<out>.<uuid>.part", so the last to commit wins, as before the streamed export. A commit whose close or rename throws now removes its partial file before it rethrows, since the run no longer tracks it. Refs #1385
1 parent 4ff8d7b commit 706b7bf

2 files changed

Lines changed: 74 additions & 18 deletions

File tree

‎electron/cli/runnerHost.test.ts‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EventEmitter } from "node:events";
2-
import { mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises";
2+
import { mkdir, mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises";
33
import os from "node:os";
44
import path from "node:path";
55
import { describe, expect, it } from "vitest";
@@ -149,6 +149,45 @@ describe("createRunnerHost", () => {
149149
await rm(dir, { recursive: true, force: true });
150150
});
151151

152+
it("keeps two exports to the same path apart: the last to commit wins", async () => {
153+
const dir = await mkdtemp(path.join(os.tmpdir(), "runner-host-"));
154+
const outPath = path.join(dir, "same.mp4");
155+
const { ipc, host } = setup();
156+
const first = host.start(exportOf(dir, "mp4", outPath));
157+
const second = host.start(exportOf(dir, "mp4", outPath));
158+
159+
await ipc.call(1, "cli-export-file-write", Buffer.from("AAAA"), 0);
160+
await ipc.call(2, "cli-export-file-write", Buffer.from("BBBBBB"), 0);
161+
await ipc.call(1, "cli-export-file-write", Buffer.from("aa"), 4);
162+
await ipc.call(1, "cli-export-file-commit");
163+
expect(await readFile(outPath, "utf8")).toBe("AAAAaa");
164+
await ipc.call(2, "cli-export-file-write", Buffer.from("bb"), 6);
165+
await ipc.call(2, "cli-export-file-commit");
166+
expect(await readFile(outPath, "utf8")).toBe("BBBBBBbb");
167+
168+
ipc.call(1, "cli-done", { success: true });
169+
ipc.call(2, "cli-done", { success: true });
170+
await Promise.all([first.done, second.done]);
171+
expect(await readdir(dir)).toEqual(["same.mp4"]);
172+
await rm(dir, { recursive: true, force: true });
173+
});
174+
175+
it("leaves no partial file when the commit cannot rename it into place", async () => {
176+
const dir = await mkdtemp(path.join(os.tmpdir(), "runner-host-"));
177+
// A folder where the output goes: the rename fails on every system.
178+
const outPath = path.join(dir, "taken.wav");
179+
await mkdir(path.join(outPath, "inside"), { recursive: true });
180+
const { ipc, host } = setup();
181+
const run = host.start(exportOf(dir, "wav", outPath));
182+
183+
await ipc.call(1, "cli-export-file-write", Buffer.from("RIFF"), 0);
184+
await expect(ipc.call(1, "cli-export-file-commit")).rejects.toThrow();
185+
expect(await readdir(dir)).toEqual(["taken.wav"]);
186+
ipc.call(1, "cli-done", { success: false, error: "commit failed" });
187+
await run.done;
188+
await rm(dir, { recursive: true, force: true });
189+
});
190+
152191
it("refuses a file that is not the calling window's own export, and a chunk out of bounds", async () => {
153192
const dir = await mkdtemp(path.join(os.tmpdir(), "runner-host-"));
154193
await writeFile(path.join(dir, "p.mp4"), "mp4");

‎electron/cli/runnerHost.ts‎

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// No `electron` import: the IPC surface and the window factory are handed in,
66
// so the routing stays unit-testable.
77

8+
import { randomUUID } from "node:crypto";
89
import { type FileHandle, mkdir, open, rename, rm } from "node:fs/promises";
910
import path from "node:path";
1011
import type { IpcMain } from "electron";
@@ -69,8 +70,8 @@ interface PendingRun {
6970
request: CliRequest;
7071
handlers: RunnerHandlers;
7172
resolve(result: CliDoneResult): void;
72-
/** The export's output while its runner writes it, open on `partialPath`. */
73-
partial?: Promise<FileHandle>;
73+
/** The export's output while its runner writes it: a file of this run's own beside the output. */
74+
partial?: { path: string; handle: Promise<FileHandle> };
7475
}
7576

7677
/** Largest read or write a runner makes in one call. */
@@ -81,9 +82,13 @@ function exportOutPath(request: CliExportRequest): string {
8182
return request.outPath ?? defaultExportPath(request.projectPath, request.format ?? "mp4");
8283
}
8384

84-
/** The output is written beside itself and renamed once whole: a mix reads the file it replaces. */
85-
function partialPath(request: CliExportRequest): string {
86-
return `${exportOutPath(request)}.part`;
85+
/**
86+
* The output is written beside itself and renamed once whole: a mix reads the file it replaces.
87+
* Each run has its own, so two exports to one path never write into each other: the last to
88+
* commit wins.
89+
*/
90+
function newPartialPath(request: CliExportRequest): string {
91+
return `${exportOutPath(request)}.${randomUUID()}.part`;
8792
}
8893

8994
/** The file of its own export a runner names by role. It never passes a path. */
@@ -97,11 +102,11 @@ function exportFilePath(request: CliExportRequest, file: unknown): string {
97102

98103
/** Drops an output the runner did not commit. */
99104
async function discardPartial(run: PendingRun): Promise<void> {
100-
if (!run.partial || run.request.kind !== "export") return;
101-
const opening = run.partial;
105+
const partial = run.partial;
106+
if (!partial) return;
102107
run.partial = undefined;
103-
await opening.then((handle) => handle.close()).catch(() => undefined);
104-
await rm(partialPath(run.request), { force: true }).catch(() => undefined);
108+
await partial.handle.then((handle) => handle.close()).catch(() => undefined);
109+
await rm(partial.path, { force: true }).catch(() => undefined);
105110
}
106111

107112
export const RUNNER_WINDOW_TYPES = {
@@ -186,19 +191,31 @@ export function createRunnerHost<W extends RunnerWindow>(
186191
if (!Number.isSafeInteger(position) || (position as number) < 0) {
187192
throw new Error("Invalid chunk position");
188193
}
189-
run.partial ??= mkdir(path.dirname(partialPath(request)), { recursive: true }).then(() =>
190-
open(partialPath(request), "w"),
191-
);
192-
const handle = await run.partial;
194+
if (!run.partial) {
195+
const partialPath = newPartialPath(request);
196+
run.partial = {
197+
path: partialPath,
198+
handle: mkdir(path.dirname(partialPath), { recursive: true }).then(() =>
199+
open(partialPath, "wx"),
200+
),
201+
};
202+
}
203+
const handle = await run.partial.handle;
193204
await handle.write(bytes, 0, bytes.byteLength, position as number);
194205
});
195206
ipc.handle("cli-export-file-commit", async (event) => {
196207
const { run, request } = exportRunFor(event);
197-
if (!run.partial) throw new Error("This export wrote no output");
198-
const opening = run.partial;
208+
const partial = run.partial;
209+
if (!partial) throw new Error("This export wrote no output");
199210
run.partial = undefined;
200-
await (await opening).close();
201-
await rename(partialPath(request), exportOutPath(request));
211+
try {
212+
await (await partial.handle).close();
213+
await rename(partial.path, exportOutPath(request));
214+
} catch (error) {
215+
// A commit that fails leaves no partial file behind: it can be as large as the export.
216+
await rm(partial.path, { force: true }).catch(() => undefined);
217+
throw error;
218+
}
202219
});
203220

204221
return {

0 commit comments

Comments
 (0)