Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: verify transcode dispatch and stored output before completion

Richie McIlroy0fd3a02d7b19
diff --git a/apps/render-farm/src/coordinator.test.ts b/apps/render-farm/src/coordinator.test.ts
index 774b883bf..1461d00c2 100644
--- a/apps/render-farm/src/coordinator.test.ts
+++ b/apps/render-farm/src/coordinator.test.ts
@@ -22,10 +22,12 @@ function harness() {
const timers: (() => void)[] = [];
const watchdogs: (() => void)[] = [];
let putGate: Promise<void> | undefined;
+ let headGate: Promise<void> | undefined;
let failures = 0;
const callbacks: { url: string; init: RequestInit }[] = [];
const s3 = {
async head(key: string) {
+ await headGate;
const value = objects.get(key);
return value ? { size: value.byteLength } : null;
},
@@ -155,6 +157,9 @@ function harness() {
gate: (gate?: Promise<void>) => {
putGate = gate;
},
+ gateHead: (gate?: Promise<void>) => {
+ headGate = gate;
+ },
fail: (count = 1) => {
failures = count;
},
@@ -615,6 +620,7 @@ describe("transcodes", () => {
})
).json()) as { cancel: string[] };
expect(heartbeat.cancel).toContain(work.task.taskId);
+ h.objects.set(request.output, new Uint8Array(1234));
await call(h, `/transcodes/${created.id}/done`, {
worker: "gpu-a",
attempt: 1,
@@ -625,6 +631,64 @@ describe("transcodes", () => {
).toMatchObject({ status: "ready", size: 1234 });
});
+ test("completion requires the active dispatch and matching stored bytes", async () => {
+ const h = harness();
+ const created = (await (await call(h, "/transcodes", request)).json()) as {
+ id: string;
+ };
+ const path = `/transcodes/${created.id}/done`;
+ const report = { worker: "gpu-a", attempt: 1, size: 42 };
+ expect((await call(h, path, report)).status).toBe(409);
+ await call(h, "/work", { worker: "gpu-a", slots: 1, cpus: 8 });
+ for (const invalid of [
+ { ...report, worker: "gpu-b" },
+ { ...report, attempt: 0 },
+ { ...report, attempt: undefined },
+ ]) {
+ expect((await call(h, path, invalid)).status).toBe(409);
+ }
+ expect((await call(h, path, { ...report, size: 1.5 })).status).toBe(400);
+ expect((await call(h, path, report)).status).toBe(409);
+ h.objects.set(request.output, new Uint8Array(41));
+ expect((await call(h, path, report)).status).toBe(409);
+ expect(
+ await (await call(h, `/transcodes/${created.id}`)).json(),
+ ).toMatchObject({
+ status: "running",
+ });
+ h.objects.set(request.output, new Uint8Array(42));
+ expect((await call(h, path, report)).status).toBe(200);
+ expect((await call(h, path, report)).status).toBe(200);
+ expect(
+ await (await call(h, `/transcodes/${created.id}`)).json(),
+ ).toMatchObject({
+ status: "ready",
+ size: 42,
+ });
+ });
+
+ test("a completion cannot settle a dispatch retired during object verification", async () => {
+ const h = harness();
+ const created = (await (await call(h, "/transcodes", request)).json()) as {
+ id: string;
+ };
+ await call(h, "/work", { worker: "gpu-a", slots: 1, cpus: 8 });
+ h.objects.set(request.output, new Uint8Array(42));
+ const gate = Promise.withResolvers<void>();
+ h.gateHead(gate.promise);
+ const report = { worker: "gpu-a", attempt: 1, size: 42 };
+ const completing = call(h, `/transcodes/${created.id}/done`, report);
+ for (let i = 0; i < 20; i++) await Promise.resolve();
+ await call(h, `/transcodes/${created.id}/fail`, report);
+ gate.resolve();
+ expect((await completing).status).toBe(409);
+ expect(
+ await (await call(h, `/transcodes/${created.id}`)).json(),
+ ).toMatchObject({
+ status: "queued",
+ });
+ });
+
test("reuse an output already in the bucket", async () => {
const h = harness();
h.objects.set(request.output, new Uint8Array(42));
diff --git a/apps/render-farm/src/coordinator.ts b/apps/render-farm/src/coordinator.ts
index 375f7efd5..df630b2f9 100644
--- a/apps/render-farm/src/coordinator.ts
+++ b/apps/render-farm/src/coordinator.ts
@@ -2588,18 +2588,30 @@ Bun.serve({
size?: number;
error?: string;
};
- // Outputs are written with If-None-Match, so any attempt that
- // finished produced the one object there is; failures count only
- // for the current attempt.
if (transcodeMatch[2] === "done") {
- if (
- transcode.state !== "ready" &&
- typeof report.size === "number" &&
- report.size > 0
- ) {
- settleTranscode(transcode, "ready", report.size);
- dispatch();
+ if (transcode.state === "ready") return Response.json({ ok: true });
+ const current = () =>
+ transcodes.get(transcode.task.output) === transcode &&
+ transcode.state === "running" &&
+ transcode.worker === report.worker &&
+ transcode.attempts === report.attempt;
+ if (!current()) {
+ return new Response("stale transcode report", { status: 409 });
+ }
+ if (!Number.isSafeInteger(report.size) || !(Number(report.size) > 0)) {
+ return new Response("invalid transcode size", { status: 400 });
+ }
+ const object = await s3.head(transcode.task.output);
+ if (!current()) {
+ return new Response("stale transcode report", { status: 409 });
}
+ if (!object || object.size !== report.size) {
+ return new Response("transcode output does not match report", {
+ status: 409,
+ });
+ }
+ settleTranscode(transcode, "ready", object.size);
+ dispatch();
} else if (
transcode.state === "running" &&
transcode.worker === report.worker &&