Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

fix: treat an edge timeout on a sliced upload as inconclusive, not fatal

François Catuheb90000bf1ca9
diff --git a/docs/admin-guide/recording-sessions.md b/docs/admin-guide/recording-sessions.md
index 3f78b1ba..67a28e44 100644
--- a/docs/admin-guide/recording-sessions.md
+++ b/docs/admin-guide/recording-sessions.md
@@ -82,6 +82,23 @@ A dropped connection or a timeout leaves the session alone, including
when it happens waiting for `finalize-upload`: the server may be
ingesting at that moment, and the retry gets that recording back.
+!!! note "Behind Cloudflare, mind the 125-second Proxy Read Timeout"
+
+ `finalize-upload` ingests in-request, exactly as `POST /upload`
+ does: hash, ffprobe, and audio extraction or codec conversion. For
+ audio that is a sub-second call, but extracting the audio from a
+ long video can outlast Cloudflare's 125-second Proxy Read Timeout
+ (not adjustable below Enterprise). The edge then answers 524 while
+ the origin finishes ingesting anyway.
+
+ Speakr treats 524 and the rest of Cloudflare's 52x range as
+ inconclusive rather than fatal, so the session survives and the
+ user's retry replays the recording that landed instead of sending
+ the file again. Time your worst realistic upload before putting the
+ edge in front of it: if it breaches the budget, the fix is deferring
+ conversion to the job queue, which `VIDEO_RETENTION=true` already
+ does for videos whose original you want to keep.
+
Operationally this means in-progress sliced uploads sit in
`UPLOAD_FOLDER/_sessions/` for up to `RECORDING_SESSION_TTL_HOURS`
waiting for a resume that may never come, and count against
diff --git a/static/js/modules/db/sliced-file-upload.js b/static/js/modules/db/sliced-file-upload.js
index f289d517..8e60f0f2 100644
--- a/static/js/modules/db/sliced-file-upload.js
+++ b/static/js/modules/db/sliced-file-upload.js
@@ -43,7 +43,9 @@ const MAX_STALLED_RESYNCS = 2;
const BASE_RETRY_MS = 1000;
const MAX_RETRY_MS = 30000;
-const RETRYABLE_STATUSES = new Set([408, 425, 429, 500, 502, 503, 504]);
+const RETRYABLE_STATUSES = new Set([408, 425, 429, 500, 502, 503, 504, 520, 521, 522, 523, 524, 525, 526, 527]);
+
+const INCONCLUSIVE_STATUSES = new Set([408, 429, 502, 503, 504, 520, 521, 522, 523, 524, 525, 526, 527]);
const RESUME_KEY = 'speakr.slicedUploads';
const RESUME_TTL_MS = 24 * 60 * 60 * 1000;
@@ -278,13 +280,30 @@ function sliceRejectionMessage(index, sliceSize, result, body) {
return body?.error || `Slice ${index} rejected (HTTP ${result.status})`;
}
+/**
+ * True when the session has to outlive a failed finalize.
+ *
+ * Covers the statuses that leave the outcome unknown, and the 409 that
+ * says another request holds the finalize claim. Cloudflare answers 524
+ * at 125 seconds while the origin carries on ingesting, which is the
+ * case that matters: the retry is then the replay that hands back the
+ * recording rather than a second upload of the same file.
+ *
+ * Guessing wrong in this direction is cheap. The resume check reads the
+ * session's real state first and only continues one the server still
+ * has, so a session that did fail costs one request to discover it.
+ */
+function finalizeCouldStillLand(status) {
+ return INCONCLUSIVE_STATUSES.has(status) || status === 409;
+}
+
/**
* Turn the uploaded slices into a recording.
*
- * A transport failure here is marked resumable: the server may be
- * ingesting at that moment, so the slices have to survive, and a retry
- * gets back the recording that call produced rather than sending the
- * file a second time.
+ * A transport failure or an inconclusive status here is marked resumable:
+ * the server may be ingesting at that moment, so the slices have to
+ * survive, and a retry gets back the recording that call produced rather
+ * than sending the file a second time.
*/
async function finalize(sessionId, formData, tokenRef, options) {
const url = `${SESSION_BASE}/${encodeURIComponent(sessionId)}/finalize-upload`;
@@ -306,8 +325,11 @@ async function finalize(sessionId, formData, tokenRef, options) {
tokenRef.token = await getUploadCsrfToken();
continue;
}
- throw new Error(body?.error
+ const error = new Error(body?.error
|| `Upload could not be finalized (HTTP ${result.status})`);
+ error.status = result.status;
+ error.resumable = finalizeCouldStillLand(result.status);
+ throw error;
}
}
diff --git a/static/js/modules/db/sliced-file-upload.test.js b/static/js/modules/db/sliced-file-upload.test.js
index be506ca1..0f29b8d9 100644
--- a/static/js/modules/db/sliced-file-upload.test.js
+++ b/static/js/modules/db/sliced-file-upload.test.js
@@ -264,10 +264,10 @@ describe('uploadFileInSlices', () => {
});
it('surfaces the finalize error and deletes the session', async () => {
- finalizeResponse = { status: 409, text: JSON.stringify({ error: 'Slices on disk do not match the session' }) };
+ finalizeResponse = { status: 400, text: JSON.stringify({ error: 'No file provided' }) };
await expect(uploadFileInSlices(fakeFile(20 * MB), new Map(), {}))
- .rejects.toThrow('Slices on disk do not match the session');
+ .rejects.toThrow('No file provided');
expect(fetchCalls[fetchCalls.length - 1].options.method).toBe('DELETE');
});
@@ -314,6 +314,48 @@ describe('resuming an interrupted upload', () => {
});
});
+ // Cloudflare answers 524 at 125s while the origin is still ingesting, and the finalize it timed out on may well have landed.
+ it('keeps the session when the edge times the finalize out', async () => {
+ finalizeResponse = { status: 524, text: '<html><title>524: A timeout occurred</title></html>' };
+ const file = fakeFile(40 * MB);
+
+ await expect(uploadFileInSlices(file, new Map(), {}))
+ .rejects.toThrow('HTTP 524');
+
+ expect(deleteCalls()).toHaveLength(0);
+ expect(rememberedSessions()[`talk.mp4|${40 * MB}|0`]).toMatchObject({ sessionId: 'sess-1' });
+ });
+
+ it('keeps the session when another request already holds the finalize claim', async () => {
+ finalizeResponse = { status: 409, text: JSON.stringify({ error: 'Session is already being finalized' }) };
+
+ await expect(uploadFileInSlices(fakeFile(40 * MB), new Map(), {}))
+ .rejects.toThrow('already being finalized');
+
+ expect(deleteCalls()).toHaveLength(0);
+ expect(Object.keys(rememberedSessions())).toHaveLength(1);
+ });
+
+ it('retries a slice the edge timed out instead of abandoning the upload', async () => {
+ vi.useFakeTimers();
+ sliceResponses = [
+ { status: 204, text: '' },
+ { status: 524, text: '<html><title>524: A timeout occurred</title></html>' },
+ ];
+
+ const upload = uploadFileInSlices(fakeFile(40 * MB), new Map(), {});
+ await vi.advanceTimersByTimeAsync(5000);
+ await upload;
+
+ expect(slicePosts().map((call) => [call.url, call.body.start])).toEqual([
+ ['/upload/session/sess-1/chunks/1', 0],
+ ['/upload/session/sess-1/chunks/2', SLICE_BYTES],
+ ['/upload/session/sess-1/chunks/2', SLICE_BYTES],
+ ['/upload/session/sess-1/chunks/3', 2 * SLICE_BYTES],
+ ]);
+ expect(deleteCalls()).toHaveLength(0);
+ });
+
it('keeps the session when a gateway error outlives the retries', async () => {
vi.useFakeTimers();
sliceResponses = [{ status: 204, text: '' }];