Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(render-farm): fail a chunk whose stash upload failed before the final wait

Richie McIlroyf43c6a4364d3
diff --git a/apps/render-farm/src/worker.test.ts b/apps/render-farm/src/worker.test.ts
index e197a29ae..a9e9e99c0 100644
--- a/apps/render-farm/src/worker.test.ts
+++ b/apps/render-farm/src/worker.test.ts
@@ -2,12 +2,16 @@ import { describe, expect, test } from "bun:test";
import { randomUUID } from "node:crypto";
import { readFileSync } from "node:fs";
import { join } from "node:path";
-import type { TranscodeTask, WorkItem } from "./protocol";
+import { MIN_PART, type TranscodeTask, type WorkItem } from "./protocol";
import * as stitch from "./stitch";
import * as transcode from "./transcode";
function harness(
- options: { presignGate?: Promise<void>; probeStalls?: boolean } = {},
+ options: {
+ presignGate?: Promise<void>;
+ probeStalls?: boolean;
+ stashFails?: boolean;
+ } = {},
) {
const spawned: string[] = [];
const killed: string[] = [];
@@ -29,6 +33,7 @@ function harness(
mediaS3ConfigFromEnv: () => ({}),
s3ConfigFromEnv: () => ({}),
...stitch,
+ MIN_PART,
S3: class {
async presignFresh() {
await options.presignGate;
@@ -38,6 +43,14 @@ function harness(
uploads++;
return 42;
}
+ put() {
+ return options.stashFails
+ ? Promise.reject(new Error("stash upload failed"))
+ : Promise.resolve();
+ }
+ async uploadPart() {
+ return "etag";
+ }
},
process: {
env: { RF_SLOTS: "0", RF_AUDIO_SLOTS: "0" },
@@ -84,7 +97,7 @@ function harness(
const compiled = new Bun.Transpiler({ loader: "ts" }).transformSync(source);
const worker = new Function(
...Object.keys(deps),
- `${compiled}\nreturn {runTranscode, cancel, busy, progress, transcoders, engineEnv};`,
+ `${compiled}\nreturn {runTranscode, cancel, busy, progress, transcoders, engineEnv, uploadLayout};`,
)(...Object.values(deps)) as {
runTranscode: (task: TranscodeTask, slot: number) => Promise<number>;
cancel: (taskIds: string[]) => void;
@@ -94,6 +107,11 @@ function harness(
engineEnv: (
env: Record<string, string | undefined>,
) => Record<string, string>;
+ uploadLayout: (
+ task: unknown,
+ segments: { bytes: Uint8Array }[],
+ bytes: number,
+ ) => Promise<unknown>;
};
return {
...worker,
@@ -176,3 +194,36 @@ describe("engine environment", () => {
).toBe("4");
});
});
+
+describe("chunk upload", () => {
+ const upload = {
+ key: "out/job.mp4",
+ uploadId: "upload",
+ firstPart: 2,
+ partLimit: 3,
+ partTarget: MIN_PART,
+ stashKey: "stash/job/0",
+ };
+ const chunk = new Uint8Array(1024);
+
+ test("a stash that failed before the final wait fails the chunk", async () => {
+ const worker = harness({ stashFails: true });
+ await expect(
+ worker.uploadLayout({ upload }, [{ bytes: chunk }], chunk.byteLength),
+ ).rejects.toThrow("stash upload failed");
+ });
+
+ test("a stored stash reports the chunk's bytes", async () => {
+ const worker = harness();
+ expect(
+ await worker.uploadLayout(
+ { upload },
+ [{ bytes: chunk }],
+ chunk.byteLength,
+ ),
+ ).toEqual({
+ parts: [],
+ stash: { key: "stash/job/0", bytes: chunk.byteLength },
+ });
+ });
+});
diff --git a/apps/render-farm/src/worker.ts b/apps/render-farm/src/worker.ts
index e703cf463..4de9dfdbf 100644
--- a/apps/render-farm/src/worker.ts
+++ b/apps/render-farm/src/worker.ts
@@ -401,13 +401,20 @@ async function uploadLayout(
let pendingBytes = 0;
let nextPart = upload.firstPart;
const inflight = new Set<Promise<void>>();
+ let failure: unknown = null;
const track = async (promise: Promise<void>) => {
inflight.add(promise);
// Handle both outcomes here: a bare .finally() re-rejects unhandled and
// kills the whole worker (e.g. a hedge loser hitting NoSuchUpload after
- // the job completed). The failure still surfaces via race/all below.
- const forget = () => inflight.delete(promise);
- promise.then(forget, forget);
+ // the job completed). An upload can settle and leave the set before
+ // anything awaits it, so its failure is kept to be thrown below.
+ promise.then(
+ () => inflight.delete(promise),
+ (error) => {
+ failure ??= error;
+ inflight.delete(promise);
+ },
+ );
if (inflight.size >= 4) await Promise.race(inflight);
};
const send = (body: Uint8Array) => {
@@ -469,6 +476,7 @@ async function uploadLayout(
if (!stashed) throw new Error("chunk ended before its stash filled");
if (pendingBytes > 0) await send(take(pendingBytes));
await Promise.all(inflight);
+ if (failure) throw failure;
pending = [];
if (parts.reduce((sum, part) => sum + part.size, 0) + stashSize !== bytes) {
throw new Error("uploaded byte count mismatch");