Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(media-server): cancel and wait for the other init download when one fails

Richie McIlroyb3d418c76152
diff --git a/apps/media-server/src/__tests__/lib/media-routes-real-world.integration.test.ts b/apps/media-server/src/__tests__/lib/media-routes-real-world.integration.test.ts
index f59eb5b58..fbfb58fb8 100644
--- a/apps/media-server/src/__tests__/lib/media-routes-real-world.integration.test.ts
+++ b/apps/media-server/src/__tests__/lib/media-routes-real-world.integration.test.ts
@@ -1572,6 +1572,34 @@ describe("media routes real-world integration tests", () => {
}
}, 90000);
+ test("cancels the other init download when one fails, before the job reports failure", async () => {
+ const response = await app.fetch(
+ mediaPostRequest("/video/mux-segments", {
+ videoId: "failed-init-mux-video",
+ userId: "failed-init-mux-user",
+ outputPresignedUrl: uploadUrl("failed-init-output.mp4"),
+ videoInitUrl: `${baseUrl}/fixtures/slow-segment.m4s`,
+ videoSegmentUrls: [fixtureUrl("test-no-audio.mp4")],
+ audioInitUrl: `${baseUrl}/fixtures/permanent-unavailable.m4s`,
+ audioSegmentUrls: [fixtureUrl("test-no-audio.mp4")],
+ }),
+ );
+
+ expect(response.status).toBe(200);
+ const data = (await response.json()) as { jobId: string };
+ const job = await waitForTerminalJob(data.jobId);
+ try {
+ expect(job.phase).toBe("error");
+ expect(job.error).toContain("503");
+ expect(slowFixtureCancellations).toBe(1);
+ expect(uploadedArtifacts.has("/uploads/failed-init-output.mp4")).toBe(
+ false,
+ );
+ } finally {
+ deleteJob(data.jobId);
+ }
+ }, 90000);
+
test("edits and uploads a real video job through the async route", async () => {
const response = await app.fetch(
mediaPostRequest("/video/edit", {
diff --git a/apps/media-server/src/routes/video.ts b/apps/media-server/src/routes/video.ts
index 9c72ac68b..74e19efed 100644
--- a/apps/media-server/src/routes/video.ts
+++ b/apps/media-server/src/routes/video.ts
@@ -3030,22 +3030,39 @@ async function muxSegmentsAsync(
audioSegmentUrls.length > 0
? { initUrl: audioInitUrl, segmentUrls: audioSegmentUrls }
: null;
- await Promise.all([
+ // Both init segments download together; a failure cancels the other and waits for it, so nothing
+ // keeps writing into the work directory after the job has failed.
+ const initAbort = new AbortController();
+ const initSignal = AbortSignal.any([
+ abortController.signal,
+ initAbort.signal,
+ ]);
+ const initDownloads = [
downloadUrlToFile(
videoInitUrl,
join(videoDir, "init.mp4"),
- abortController.signal,
+ initSignal,
sources?.get(videoInitUrl),
),
- audioInput
- ? downloadUrlToFile(
- audioInput.initUrl,
- join(audioDir, "init.mp4"),
- abortController.signal,
- sources?.get(audioInput.initUrl),
- )
- : undefined,
- ]);
+ ...(audioInput
+ ? [
+ downloadUrlToFile(
+ audioInput.initUrl,
+ join(audioDir, "init.mp4"),
+ initSignal,
+ sources?.get(audioInput.initUrl),
+ ),
+ ]
+ : []),
+ ];
+ let initFailure: { error: unknown } | undefined;
+ for (const download of initDownloads)
+ download.catch((error: unknown) => {
+ initFailure ??= { error };
+ initAbort.abort();
+ });
+ await Promise.allSettled(initDownloads);
+ if (initFailure) throw initFailure.error;
updateJob(jobId, { phase: "downloading", progress: 5 });
sendCurrentJobWebhook(jobId);