Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(media-server): stop and wait for the output hash when a mux job fails

Richie McIlroy3add530ab47b
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 49a3d070c..9ab3c9835 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
@@ -858,6 +858,48 @@ describe("media routes real-world integration tests", () => {
}
}, 30_000);
+ test("stops the output hash before cleanup when the decode check fails first", async () => {
+ let hashAborted = false;
+ let hashSettled = false;
+ const hashing = spyOn(
+ recordingVerification,
+ "hashRecordingFile",
+ ).mockImplementation(
+ (_path, signal) =>
+ new Promise<string>((_resolve, reject) => {
+ signal?.addEventListener("abort", () => {
+ hashAborted = true;
+ setTimeout(() => {
+ hashSettled = true;
+ reject(new Error("Hash stopped"));
+ }, 50);
+ });
+ }),
+ );
+ const verifying = spyOn(
+ recordingVerification,
+ "verifyRemuxedRecording",
+ ).mockRejectedValue(new Error("Recording decoder failed to start"));
+ let jobId: string | undefined;
+ try {
+ const response = await app.fetch(
+ mediaPostRequest(
+ "/video/mux-segments",
+ fencedMuxRequest("hash-after-failed-decode"),
+ ),
+ );
+ jobId = ((await response.json()) as { jobId: string }).jobId;
+ const job = await waitForTerminalJob(jobId);
+ expect(job.phase).toBe("error");
+ expect(hashAborted).toBe(true);
+ expect(hashSettled).toBe(true);
+ } finally {
+ hashing.mockRestore();
+ verifying.mockRestore();
+ if (jobId) deleteJob(jobId);
+ }
+ }, 30_000);
+
// Hashing now overlaps the decode check, so whichever check reads the changed bytes first refuses it.
test("refuses a local output changed while it is decoded and hashed", async () => {
const originalHash = recordingVerification.hashRecordingFile;
diff --git a/apps/media-server/src/routes/video.ts b/apps/media-server/src/routes/video.ts
index bbcd6af24..23d6ba168 100644
--- a/apps/media-server/src/routes/video.ts
+++ b/apps/media-server/src/routes/video.ts
@@ -2967,6 +2967,9 @@ async function muxSegmentsAsync(
updateJob(jobId, { abortController });
let outputUploadStarted = false;
let pendingOutputUpload: PendingStorageUpload | undefined;
+ let pendingOutputHash:
+ | { promise: Promise<string>; abort: AbortController }
+ | undefined;
let processingTimeout: ReturnType<typeof setTimeout> | undefined;
let errorCode: RecordingErrorCode = "processing-unavailable";
const sources = context.sourceObjects
@@ -3127,8 +3130,13 @@ async function muxSegmentsAsync(
"video/mp4",
abortController.signal,
);
- const outputHash = hashRecordingFile(resultPath, abortController.signal);
+ const hashAbort = new AbortController();
+ const outputHash = hashRecordingFile(
+ resultPath,
+ AbortSignal.any([abortController.signal, hashAbort.signal]),
+ );
outputHash.catch(() => {});
+ pendingOutputHash = { promise: outputHash, abort: hashAbort };
updateJob(jobId, {
progress: 70,
message: "Checking the processed recording...",
@@ -3409,6 +3417,9 @@ async function muxSegmentsAsync(
error: logReason(error, MUX_FAILURE_LOG_LENGTH),
resources: getSystemResources(),
});
+ // Stop a hash still reading the output before the work directory is removed.
+ pendingOutputHash?.abort.abort();
+ await pendingOutputHash?.promise.catch(() => {});
if (!outputUploadStarted) {
// Parts still in flight could land after the abort, so let them settle first.
await pendingOutputUpload?.cancel();