Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Set XHR upload timeout so stalled uploads fail into recovery (#306)
jjsmackay926fdbfbcc19
diff --git a/static/js/modules/composables/upload.js b/static/js/modules/composables/upload.jsindex 88e480f8..55dd4c2d 100644--- a/static/js/modules/composables/upload.js+++ b/static/js/modules/composables/upload.js@@ -7,6 +7,7 @@ import * as FailedUploads from '../db/failed-uploads.js';import * as IncognitoStorage from '../db/incognito-storage.js';import * as RecordingDB from '../db/recording-persistence.js';import { getUploadCsrfToken, isCsrfRejection } from '../csrf.js';+import { computeUploadTimeout } from '../utils/upload-timeout.js';// Parse error message and return friendly error infofunction getFriendlyError(errorMessage, t) {@@ -608,6 +609,13 @@ export function useUpload(state, utils) {xhr.onerror = () => reject(new Error('Network error during upload'));xhr.ontimeout = () => reject(new Error('Upload timed out'));+ // Without an explicit timeout, a stalled connection (no FIN/RST,+ // just no progress) hangs forever and ontimeout never fires — so+ // the upload never reaches the catch block that persists the file+ // for retry / triggers the local-download fallback. Set a+ // size-scaled ceiling so a stalled upload eventually fails over.+ xhr.timeout = computeUploadTimeout(fileItem.file?.size || 0);+// Store abort controller on item for cancellationfileItem._xhr = xhr;diff --git a/static/js/modules/utils/upload-timeout.js b/static/js/modules/utils/upload-timeout.jsnew file mode 100644index 00000000..1e1fe1a2--- /dev/null+++ b/static/js/modules/utils/upload-timeout.js@@ -0,0 +1,42 @@+/**+ * Upload timeout calculation.+ *+ * XMLHttpRequest has no timeout by default (xhr.timeout === 0), so a TCP+ * connection that stalls mid-stream — no FIN, no RST, just no progress — sits+ * open indefinitely and neither onerror nor ontimeout ever fires. The upload+ * then never reaches the catch block that persists the file to IndexedDB /+ * triggers the local-download fallback, so a stalled upload is a silent+ * data-loss path. We set an explicit, size-scaled ceiling so a stalled upload+ * eventually fails over into that recovery path instead of hanging forever.+ */++const MINUTE_MS = 60 * 1000;++// Floor for any upload, regardless of size.+const BASE_TIMEOUT_MS = 10 * MINUTE_MS;++// Additional allowance per 10 MB of payload, so large recordings that simply+// take a long time to transfer over a slow link are not killed prematurely.+const MS_PER_10MB = 1 * MINUTE_MS;++const BYTES_PER_10MB = 10 * 1024 * 1024;++/**+ * Compute the XHR timeout (in milliseconds) for an upload of the given size.+ * @param {number} fileSizeBytes - size of the file being uploaded, in bytes.+ * @returns {number} timeout in milliseconds (always >= BASE_TIMEOUT_MS).+ */+export function computeUploadTimeout(fileSizeBytes) {+ const size = Number(fileSizeBytes);+ if (!Number.isFinite(size) || size <= 0) {+ return BASE_TIMEOUT_MS;+ }+ return Math.round(BASE_TIMEOUT_MS + (size / BYTES_PER_10MB) * MS_PER_10MB);+}++export const UPLOAD_TIMEOUT_CONSTANTS = Object.freeze({+ MINUTE_MS,+ BASE_TIMEOUT_MS,+ MS_PER_10MB,+ BYTES_PER_10MB,+});diff --git a/static/js/modules/utils/upload-timeout.test.js b/static/js/modules/utils/upload-timeout.test.jsnew file mode 100644index 00000000..5238fe20--- /dev/null+++ b/static/js/modules/utils/upload-timeout.test.js@@ -0,0 +1,37 @@+import { describe, it, expect } from 'vitest';+import { computeUploadTimeout, UPLOAD_TIMEOUT_CONSTANTS } from './upload-timeout.js';++const { BASE_TIMEOUT_MS, MINUTE_MS, BYTES_PER_10MB } = UPLOAD_TIMEOUT_CONSTANTS;++describe('computeUploadTimeout', () => {+ it('returns the base floor for an empty or zero-size upload', () => {+ expect(computeUploadTimeout(0)).toBe(BASE_TIMEOUT_MS);+ });++ it('falls back to the base floor for invalid sizes', () => {+ expect(computeUploadTimeout(undefined)).toBe(BASE_TIMEOUT_MS);+ expect(computeUploadTimeout(null)).toBe(BASE_TIMEOUT_MS);+ expect(computeUploadTimeout(NaN)).toBe(BASE_TIMEOUT_MS);+ expect(computeUploadTimeout(-5)).toBe(BASE_TIMEOUT_MS);+ });++ it('adds one minute per 10 MB on top of the base floor', () => {+ // 10 MB -> base + 1 minute+ expect(computeUploadTimeout(BYTES_PER_10MB)).toBe(BASE_TIMEOUT_MS + MINUTE_MS);+ // 100 MB -> base + 10 minutes+ expect(computeUploadTimeout(10 * BYTES_PER_10MB)).toBe(BASE_TIMEOUT_MS + 10 * MINUTE_MS);+ });++ it('scales monotonically with size and never drops below the floor', () => {+ const small = computeUploadTimeout(1 * 1024 * 1024); // 1 MB+ const large = computeUploadTimeout(500 * 1024 * 1024); // 500 MB+ expect(small).toBeGreaterThanOrEqual(BASE_TIMEOUT_MS);+ expect(large).toBeGreaterThan(small);+ });++ it('gives a multi-hour recording a sane (not infinite) ceiling', () => {+ // ~300 MB recording: base 10 min + 30 min = 40 min, well under an hour.+ const fortyMinutes = 40 * MINUTE_MS;+ expect(computeUploadTimeout(300 * 1024 * 1024)).toBe(fortyMinutes);+ });+});