A platform for community discussion. Free, open, simple.
FIX: Handle rate limited uploads instead of failing the batch
Régis Hanol0fa63771e91f
diff --git a/frontend/discourse/app/lib/uploads.js b/frontend/discourse/app/lib/uploads.jsindex 4fb38a0beb8..4e0b127e74c 100644--- a/frontend/discourse/app/lib/uploads.js+++ b/frontend/discourse/app/lib/uploads.js@@ -1,9 +1,16 @@+import {+ isRateLimitError,+ MAX_RATE_LIMIT_RETRY_SECONDS,+ rateLimitWaitSeconds,+} from "discourse/lib/ajax-error";import deprecated from "discourse/lib/deprecated";import { getOwnerWithFallback } from "discourse/lib/get-owner";import { humanizeList } from "discourse/lib/text";import { capabilities } from "discourse/services/capabilities";import I18n, { i18n } from "discourse-i18n";+const RATE_LIMIT_RETRIES = 1;+function isGUID(value) {return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value@@ -317,7 +324,26 @@ export function getUploadMarkdown(upload) {}}+export function displayUploadErrors(errors, siteSettings) {+ if (errors.length === 0) {+ return;+ }++ if (errors.length === 1) {+ displayErrorForUpload(errors[0].data, siteSettings, errors[0].fileName);+ return;+ }++ displayErrorForBulkUpload(errors);+}+export function displayErrorForBulkUpload(errors) {+ const rateLimited = errors.find(({ data }) => isRateLimitError(data));+ if (rateLimited) {+ displayRateLimitError(rateLimited.data);+ return;+ }+const fileNames = humanizeList(errors.map((item) => item.fileName));dialog.alert(i18n("post.errors.upload", { file_name: fileNames }));@@ -332,6 +358,11 @@ export function displayErrorForUpload(data, siteSettings, fileName) {fileName = data.files[0].name;}+ if (isRateLimitError(data)) {+ displayRateLimitError(data);+ return;+ }+if (data.jqXHR) {const didError = displayErrorByResponseStatus(data.jqXHR.status,@@ -404,6 +435,52 @@ function displayErrorByResponseStatus(status, body, fileName, siteSettings) {return;}+function displayRateLimitError(error) {+ dialog.alert(+ i18n("too_many_requests", { count: rateLimitWaitSeconds(error) })+ );+}++function isRetryableRateLimit(error) {+ return (+ isRateLimitError(error) &&+ rateLimitWaitSeconds(error) <= MAX_RATE_LIMIT_RETRY_SECONDS+ );+}++function waitOutRateLimit(error) {+ return new Promise((resolve) =>+ setTimeout(resolve, rateLimitWaitSeconds(error) * 1000)+ );+}++export const rateLimitRetryOptions = {+ retries: RATE_LIMIT_RETRIES,++ shouldRetry: isRetryableRateLimit,++ async onAfterResponse(xhr, retryCount) {+ if (retryCount >= RATE_LIMIT_RETRIES || !isRetryableRateLimit(xhr)) {+ return;+ }++ await waitOutRateLimit(xhr);+ },+};++export async function withRateLimitRetry(request) {+ try {+ return await request();+ } catch (error) {+ if (!isRetryableRateLimit(error)) {+ throw error;+ }++ await waitOutRateLimit(error);+ return request();+ }+}+export function bindFileInputChangeListener(element, fileCallbackFn) {function changeListener(event) {const files = Array.from(event.target.files);diff --git a/frontend/discourse/app/lib/uppy/composer-upload.js b/frontend/discourse/app/lib/uppy/composer-upload.jsindex 4c4c058542d..fe8822cfb63 100644--- a/frontend/discourse/app/lib/uppy/composer-upload.js+++ b/frontend/discourse/app/lib/uppy/composer-upload.js@@ -13,10 +13,10 @@ import { bind } from "discourse/lib/decorators";import getURL from "discourse/lib/get-url";import {bindFileInputChangeListener,- displayErrorForBulkUpload,- displayErrorForUpload,+ displayUploadErrors,getUploadMarkdown,isImage,+ rateLimitRetryOptions,validateUploadedFile,} from "discourse/lib/uploads";import UppyS3Multipart from "discourse/lib/uppy/s3-multipart";@@ -388,7 +388,10 @@ export default class UppyComposerUpload {`${this.composerEventPrefix}:all-uploads-complete`);- this.#displayBufferedErrors();+ displayUploadErrors(+ this.#bufferedUploadErrors,+ this.siteSettings+ );this.#reset();}}@@ -434,20 +437,6 @@ export default class UppyComposerUpload {);}- #displayBufferedErrors() {- if (this.#bufferedUploadErrors.length === 0) {- return;- } else if (this.#bufferedUploadErrors.length === 1) {- displayErrorForUpload(- this.#bufferedUploadErrors[0].data,- this.siteSettings,- this.#bufferedUploadErrors[0].fileName- );- } else {- displayErrorForBulkUpload(this.#bufferedUploadErrors);- }- }-#bufferUploadError(data, fileName) {this.#bufferedUploadErrors.push({ data, fileName });}@@ -506,7 +495,7 @@ export default class UppyComposerUpload {#useXHRUploads() {this.uppyWrapper.uppyInstance.use(XHRUpload, {endpoint: getURL(`/uploads.json?client_id=${this.messageBus.clientId}`),- shouldRetry: () => false,+ ...rateLimitRetryOptions,headers: () => ({"X-CSRF-Token": this.session.csrfToken,}),@@ -584,7 +573,7 @@ export default class UppyComposerUpload {this.appEvents.trigger(`${this.composerEventPrefix}:upload-error`, file);}if (this.#inProgressUploads.length === 0) {- this.#displayBufferedErrors();+ displayUploadErrors(this.#bufferedUploadErrors, this.siteSettings);this.#reset();}}diff --git a/frontend/discourse/app/lib/uppy/s3-multipart.js b/frontend/discourse/app/lib/uppy/s3-multipart.jsindex 777386efabe..5b8a3154853 100644--- a/frontend/discourse/app/lib/uppy/s3-multipart.js+++ b/frontend/discourse/app/lib/uppy/s3-multipart.js@@ -4,6 +4,8 @@ import { service } from "@ember/service";import AwsS3 from "@uppy/aws-s3";import { Promise } from "rsvp";import { ajax } from "discourse/lib/ajax";+import { isRateLimitError } from "discourse/lib/ajax-error";+import { withRateLimitRetry } from "discourse/lib/uploads";const RETRY_DELAYS = [0, 1000, 3000, 5000];const MB = 1024 * 1024;@@ -79,11 +81,12 @@ export default class UppyS3Multipart {data.metadata = { "sha1-checksum": file.meta.sha1_checksum };}- return ajax(`${this.uploadRootPath}/create-multipart.json`, {- type: "POST",- data,- // uppy is inconsistent, an error here fires the upload-error event- }).then((responseData) => {+ return withRateLimitRetry(() =>+ ajax(`${this.uploadRootPath}/create-multipart.json`, {+ type: "POST",+ data,+ })+ ).then((responseData) => {this.uppyInstance.emit("create-multipart-success", file.id);file.meta.unique_identifier = responseData.unique_identifier;@@ -122,6 +125,10 @@ export default class UppyS3Multipart {`[uppy] requesting signature for part ${partData.partNumber} (attempt ${thisPartAttempts})`);+ if (fileMeta.rateLimitError) {+ throw fileMeta.rateLimitError;+ }+try {const url = await fileMeta.batchSigner.signedUrlFor(partData);this.uppyWrapper.debug.log(@@ -131,15 +138,18 @@ export default class UppyS3Multipart {} catch (err) {// Uppy doesn't properly bubble errors from failed #signPart, so we call// the error handler ourselves after the last failed attempt+ if (isRateLimitError(err)) {+ fileMeta.rateLimitError = err;+ }+if (!fileMeta.signingErrorRaised &&- thisPartAttempts >= RETRY_DELAYS.length+ (fileMeta.rateLimitError || thisPartAttempts >= RETRY_DELAYS.length)) {this.uppyWrapper.debug.log(`[uppy] Fetching a signed part URL for ${file.id} failed too many times, raising error.`);- // uppy is inconsistent, an error here does not fire the upload-error event- this.handleUploadError(file, err);+ this.errorHandler(file, err);fileMeta.signingErrorRaised = true;}throw err;@@ -155,19 +165,20 @@ export default class UppyS3Multipart {const parts = data.parts.map((part) => {return { part_number: part.PartNumber, etag: part.ETag };});- return ajax(`${this.uploadRootPath}/complete-multipart.json`, {- type: "POST",- contentType: "application/json",- data: JSON.stringify({- parts,- unique_identifier: file.meta.unique_identifier,- pasted: file.meta.pasted,- for_private_message: file.meta.for_private_message,- for_site_setting: file.meta.for_site_setting,- site_setting_name: file.meta.site_setting_name,- }),- // uppy is inconsistent, an error here fires the upload-error event- }).then((responseData) => {+ return withRateLimitRetry(() =>+ ajax(`${this.uploadRootPath}/complete-multipart.json`, {+ type: "POST",+ contentType: "application/json",+ data: JSON.stringify({+ parts,+ unique_identifier: file.meta.unique_identifier,+ pasted: file.meta.pasted,+ for_private_message: file.meta.for_private_message,+ for_site_setting: file.meta.for_site_setting,+ site_setting_name: file.meta.site_setting_name,+ }),+ })+ ).then((responseData) => {this.uppyInstance.emit("complete-multipart-success", file.id);return responseData;});@@ -196,7 +207,6 @@ export default class UppyS3Multipart {data: {external_upload_identifier: uploadId,},- // uppy is inconsistent, an error here does not fire the upload-error event}).catch((err) => {this.errorHandler(file, err);});@@ -254,9 +264,8 @@ class BatchSigner {this.pendingRequests = [];try {- const result = await ajax(- `${this.uploadRootPath}/batch-presign-multipart-parts.json`,- {+ const result = await withRateLimitRetry(() =>+ ajax(`${this.uploadRootPath}/batch-presign-multipart-parts.json`, {type: "POST",data: {part_numbers: requests.map(@@ -264,14 +273,12 @@ class BatchSigner {),unique_identifier: this.file.meta.unique_identifier,},- }+ }));requests.forEach(({ partData, resolve }) => {resolve(result.presigned_urls[partData.partNumber.toString()]);});} catch (err) {- // eslint-disable-next-line no-console- console.error("[uppy] failed to get part signatures", err);requests.forEach(({ reject }) => reject(err));return;}diff --git a/frontend/discourse/app/lib/uppy/uppy-upload.js b/frontend/discourse/app/lib/uppy/uppy-upload.jsindex 67538ca69d0..abda3ce6d8d 100644--- a/frontend/discourse/app/lib/uppy/uppy-upload.js+++ b/frontend/discourse/app/lib/uppy/uppy-upload.js@@ -16,6 +16,8 @@ import { deepMerge } from "discourse/lib/object";import {bindFileInputChangeListener,displayErrorForUpload,+ displayUploadErrors,+ rateLimitRetryOptions,validateUploadedFile,} from "discourse/lib/uploads";import UppyS3Multipart from "discourse/lib/uppy/s3-multipart";@@ -102,6 +104,8 @@ export default class UppyUpload {uppyWrapper;+ #bufferedUploadErrors = [];+#fileInputEventListener;#usingS3Uploads;@@ -280,8 +284,10 @@ export default class UppyUpload {}}).catch((errResponse) => {- displayErrorForUpload(errResponse, this.siteSettings, file.name);- this.#triggerInProgressUploadsEvent();+ this.#removeInProgressUpload(file.id);+ this.#bufferUploadError(errResponse, file.name);++ this.#finishBatch();});} else {this.#removeInProgressUpload(file.id);@@ -303,12 +309,10 @@ export default class UppyUpload {this.uppyWrapper.uppyInstance.on("upload-error",(file, error, response) => {- if (response?.aborted) {- return; // User cancelled the upload- }this.#removeInProgressUpload(file.id);- displayErrorForUpload(response || error, this.siteSettings, file.name);- this.#reset();+ this.#bufferUploadError(response || error, file.name);++ this.#finishBatch();});@@ -414,6 +418,7 @@ export default class UppyUpload {cancelSingleUpload(data) {this.uppyWrapper.uppyInstance.removeFile(data.fileId);this.#removeInProgressUpload(data.fileId);+ this.#finishBatch();}@bind@@ -421,6 +426,7 @@ export default class UppyUpload {this.uppyWrapper.uppyInstance?.cancelAll();this.inProgressUploads.length = 0;this.#triggerInProgressUploadsEvent();+ this.#finishBatch();}@bind@@ -461,7 +467,7 @@ export default class UppyUpload {#useXHRUploads() {this.uppyWrapper.uppyInstance.use(XHRUpload, {endpoint: this.#xhrUploadUrl(),- shouldRetry: () => false,+ ...rateLimitRetryOptions,headers: () => ({"X-CSRF-Token": this.session.csrfToken,}),@@ -544,6 +550,19 @@ export default class UppyUpload {});}+ #bufferUploadError(data, fileName) {+ this.#bufferedUploadErrors.push({ data, fileName });+ }++ #finishBatch() {+ if (this.inProgressUploads.length > 0) {+ return;+ }++ displayUploadErrors(this.#bufferedUploadErrors, this.siteSettings);+ this.#reset();+ }+#reset() {this.uppyWrapper.uppyInstance?.cancelAll();Object.assign(this, {@@ -553,6 +572,7 @@ export default class UppyUpload {uploadProgress: 0,filesAwaitingUpload: false,});+ this.#bufferedUploadErrors = [];if (this._fileInputEl) {this._fileInputEl.value = "";}@@ -575,6 +595,7 @@ export default class UppyUpload {this.appEvents.trigger(`upload-mixin:${this.config.id}:all-uploads-complete`);- this.#reset();++ this.#finishBatch();}}diff --git a/frontend/discourse/tests/unit/lib/uploads-test.js b/frontend/discourse/tests/unit/lib/uploads-test.jsindex 3cf477d29ad..17873cf6bdc 100644--- a/frontend/discourse/tests/unit/lib/uploads-test.js+++ b/frontend/discourse/tests/unit/lib/uploads-test.js@@ -7,13 +7,47 @@ import {allowsImages,authorizedExtensions,dialog,+ displayErrorForBulkUpload,displayErrorForUpload,getUploadMarkdown,isImage,+ rateLimitRetryOptions,validateUploadedFiles,} from "discourse/lib/uploads";+import { TOO_MANY_REQUESTS } from "discourse/tests/helpers/create-pretender";import I18n, { i18n } from "discourse-i18n";+const RETRY_AFTER_SECONDS = 1;+const TOO_LONG_TO_WAIT_SECONDS = 600;++function middlewareRateLimit(retryAfter = RETRY_AFTER_SECONDS) {+ const headers = {+ "Retry-After": String(retryAfter),+ "Content-Type": "text/plain",+ };++ return {+ status: TOO_MANY_REQUESTS,+ responseText:+ "Slow down, you're making too many requests.\n" ++ `Please retry again in ${retryAfter} seconds.\n`,+ getResponseHeader: (name) => headers[name],+ };+}++function controllerRateLimit(waitSeconds = RETRY_AFTER_SECONDS) {+ return {+ jqXHR: {+ status: TOO_MANY_REQUESTS,+ responseJSON: {+ errors: ["rate limited"],+ error_type: "rate_limit",+ extras: { wait_seconds: waitSeconds },+ },+ },+ };+}+module("Unit | Utility | uploads", function (hooks) {setupTest(hooks);@@ -470,4 +504,95 @@ module("Unit | Utility | uploads", function (hooks) {"the alert is called");});++ test("displayErrorForUpload - rate limits report the advertised wait", function (assert) {+ sinon.stub(dialog, "alert");++ const chunked = new Error("Non 2xx");+ chunked.source = middlewareRateLimit();++ displayErrorForUpload(middlewareRateLimit(), {}, "test.png");+ displayErrorForUpload(chunked, {}, "backup.tar.gz");+ displayErrorForUpload(controllerRateLimit(30), {}, "test.png");++ assert.deepEqual(+ dialog.alert.args.flat(),+ [+ i18n("too_many_requests", { count: RETRY_AFTER_SECONDS }),+ i18n("too_many_requests", { count: RETRY_AFTER_SECONDS }),+ i18n("too_many_requests", { count: 30 }),+ ],+ "the wait comes from Retry-After, error.source, then extras.wait_seconds"+ );+ });++ test("displayErrorForBulkUpload - one rate limited file speaks for the batch", function (assert) {+ sinon.stub(dialog, "alert");+ displayErrorForBulkUpload([+ { data: { status: 422 }, fileName: "a.png" },+ { data: middlewareRateLimit(), fileName: "b.png" },+ ]);+ assert.true(+ dialog.alert.calledWith(+ i18n("too_many_requests", { count: RETRY_AFTER_SECONDS })+ ),+ "the actionable rate limit wins over the list of file names"+ );+ });++ test("displayErrorForBulkUpload - falls back to the file names", function (assert) {+ sinon.stub(dialog, "alert");+ displayErrorForBulkUpload([+ { data: { status: 422 }, fileName: "a.png" },+ { data: { status: 422 }, fileName: "b.png" },+ ]);+ assert.true(+ dialog.alert.calledWith(+ i18n("post.errors.upload", { file_name: "a.png and b.png" })+ ),+ "the alert is called"+ );+ });++ test("rateLimitRetryOptions - only retries a rate limit worth waiting for", function (assert) {+ assert.true(+ rateLimitRetryOptions.shouldRetry(middlewareRateLimit()),+ "a 429 is retried"+ );+ assert.false(+ rateLimitRetryOptions.shouldRetry({ status: 422 }),+ "a 422 is not retried"+ );+ assert.false(+ rateLimitRetryOptions.shouldRetry(+ middlewareRateLimit(TOO_LONG_TO_WAIT_SECONDS)+ ),+ "a 429 that asks for a very long wait fails fast instead"+ );+ });++ test("rateLimitRetryOptions - waits out Retry-After before retrying", async function (assert) {+ const startedAt = Date.now();+ await rateLimitRetryOptions.onAfterResponse(middlewareRateLimit(), 0);++ assert.true(+ Date.now() - startedAt >= RETRY_AFTER_SECONDS * 1000,+ "the retry is delayed by Retry-After, not by uppy's fixed sub-second ladder"+ );+ });++ test("rateLimitRetryOptions - never waits when there is nothing left to wait for", async function (assert) {+ const startedAt = Date.now();++ await rateLimitRetryOptions.onAfterResponse(+ middlewareRateLimit(),+ rateLimitRetryOptions.retries+ );+ await rateLimitRetryOptions.onAfterResponse({ status: 200 }, 0);++ assert.true(+ Date.now() - startedAt < RETRY_AFTER_SECONDS * 1000,+ "an exhausted retry budget and a successful response both report immediately"+ );+ });});diff --git a/frontend/discourse/tests/unit/lib/uppy/s3-multipart-test.js b/frontend/discourse/tests/unit/lib/uppy/s3-multipart-test.jsnew file mode 100644index 00000000000..59e72498a36--- /dev/null+++ b/frontend/discourse/tests/unit/lib/uppy/s3-multipart-test.js@@ -0,0 +1,113 @@+import { getOwner } from "@ember/owner";+import { setupTest } from "ember-qunit";+import { module, test } from "qunit";+import sinon from "sinon";+import UppyS3Multipart from "discourse/lib/uppy/s3-multipart";+import pretender, {+ middlewareRateLimit,+} from "discourse/tests/helpers/create-pretender";++const UPLOAD_ROOT_PATH = "/uploads";+const BATCH_PRESIGN_URL = "/uploads/batch-presign-multipart-parts.json";+const COMPLETE_URL = "/uploads/complete-multipart.json";+const RETRY_AFTER_SECONDS = 1;+const PART_NUMBER = 1;+const SIGNING_ATTEMPTS_BEFORE_RAISING = 4;+const UPPY_SIGNING_ATTEMPTS = 5;+const LONG_RETRY_AFTER_SECONDS = 120;++const OK = [200, { "Content-Type": "application/json" }, "{}"];+const SERVER_ERROR = [500, {}, ""];++function buildFile() {+ return { id: "file-1", name: "big.mp4", meta: { unique_identifier: "abc" } };+}++function awsS3Options(context, errorHandler) {+ const multipart = new UppyS3Multipart(getOwner(context), {+ uploadRootPath: UPLOAD_ROOT_PATH,+ uppyWrapper: { debug: { log: () => {} } },+ errorHandler,+ });++ let options;+ multipart.apply({+ use: (_plugin, opts) => (options = opts),+ emit: () => {},+ });++ return options;+}++module("Unit | Lib | uppy/s3-multipart", function (hooks) {+ setupTest(hooks);++ test("a signing failure reaches the configured error handler", async function (assert) {+ const errorHandler = sinon.spy();+ const { signPart } = awsS3Options(this, errorHandler);++ pretender.post(BATCH_PRESIGN_URL, () => SERVER_ERROR);++ const file = buildFile();+ await Promise.allSettled(+ Array.from({ length: SIGNING_ATTEMPTS_BEFORE_RAISING }, () =>+ signPart(file, { partNumber: PART_NUMBER })+ )+ );++ assert.true(+ errorHandler.calledOnce,+ "the error handler is called once, rather than throwing on a method that does not exist"+ );+ assert.strictEqual(+ errorHandler.firstCall.args[0],+ file,+ "the error handler receives the file"+ );+ });++ test("a rate limited signing stops uppy's own retries from hitting the server", async function (assert) {+ const errorHandler = sinon.spy();+ const { signPart } = awsS3Options(this, errorHandler);++ let requests = 0;+ pretender.post(BATCH_PRESIGN_URL, () => {+ requests++;+ return middlewareRateLimit(LONG_RETRY_AFTER_SECONDS);+ });++ const file = buildFile();+ for (let attempt = 0; attempt < UPPY_SIGNING_ATTEMPTS; attempt++) {+ await signPart(file, { partNumber: PART_NUMBER }).catch(() => {});+ }++ assert.strictEqual(requests, 1, "the limiter is not hit again");+ assert.true(errorHandler.calledOnce, "the error is reported once");+ });++ test("a rate limited complete-multipart is retried after Retry-After", async function (assert) {+ const { completeMultipartUpload } = awsS3Options(this, sinon.spy());++ const requests = [];+ pretender.post(COMPLETE_URL, () => {+ requests.push(Date.now());+ return requests.length === 1+ ? middlewareRateLimit(RETRY_AFTER_SECONDS)+ : OK;+ });++ await completeMultipartUpload(buildFile(), {+ parts: [{ PartNumber: PART_NUMBER, ETag: "etag" }],+ });++ assert.strictEqual(+ requests.length,+ 2,+ "the completion is retried rather than discarding a fully uploaded file"+ );+ assert.true(+ requests[1] - requests[0] >= RETRY_AFTER_SECONDS * 1000,+ "the retry waits out the window the server advertised"+ );+ });+});diff --git a/frontend/discourse/tests/unit/lib/uppy/uppy-upload-test.js b/frontend/discourse/tests/unit/lib/uppy/uppy-upload-test.jsnew file mode 100644index 00000000000..e3b6e6a5f0e--- /dev/null+++ b/frontend/discourse/tests/unit/lib/uppy/uppy-upload-test.js@@ -0,0 +1,144 @@+import { getOwner } from "@ember/owner";+import { waitUntil } from "@ember/test-helpers";+import { setupTest } from "ember-qunit";+import { module, test } from "qunit";+import sinon from "sinon";+import { dialog } from "discourse/lib/uploads";+import UppyUpload from "discourse/lib/uppy/uppy-upload";+import pretender from "discourse/tests/helpers/create-pretender";+import { createFile } from "discourse/tests/helpers/qunit-helpers";++const UPLOAD_URL = "/uploads.json";+const REJECTED_FILE = "rejected.png";+const SIBLING_FILE = "sibling.png";+const JSON_HEADERS = { "Content-Type": "application/json" };++const UNPROCESSABLE = [+ 422,+ JSON_HEADERS,+ JSON.stringify({ message: "upload failed" }),+];++function uploadResponse(fileName) {+ return [+ 200,+ JSON_HEADERS,+ JSON.stringify({+ id: 1,+ url: `/uploads/${fileName}`,+ short_url: `upload://${fileName}`,+ original_filename: fileName,+ }),+ ];+}++module("Unit | Lib | uppy/uppy-upload", function (hooks) {+ setupTest(hooks);++ hooks.beforeEach(function () {+ this.fileInput = document.createElement("input");+ this.fileInput.type = "file";+ this.fileInput.multiple = true;+ document.body.appendChild(this.fileInput);+ });++ hooks.afterEach(function () {+ this.fileInput.remove();+ sinon.restore();+ });++ test("a rejected upload does not cancel the rest of the batch", async function (assert) {+ const alert = sinon.stub(dialog, "alert");+ const completed = [];++ let rejectionDelivered;+ const siblingReleasedByRejection = new Promise(+ (resolve) => (rejectionDelivered = resolve)+ );++ pretender.post(UPLOAD_URL, async (request) => {+ const fileName = request.requestBody.get("file").name;++ if (fileName === REJECTED_FILE) {+ return UNPROCESSABLE;+ }++ await siblingReleasedByRejection;+ return uploadResponse(fileName);+ });++ const upload = new UppyUpload(getOwner(this), {+ id: "uppy-upload-test",+ type: "composer",+ uploadDone: ({ file_name }) => completed.push(file_name),+ });++ upload.setup(this.fileInput);+ upload.uppyWrapper.uppyInstance.on("upload-error", () =>+ rejectionDelivered()+ );++ await upload.addFiles([+ createFile(REJECTED_FILE),+ createFile(SIBLING_FILE),+ ]);++ await waitUntil(() => !upload.uploading && completed.length > 0, {+ timeout: 5000,+ });++ assert.deepEqual(+ completed,+ [SIBLING_FILE],+ "the sibling upload finishes instead of being aborted"+ );+ assert.true(+ alert.calledOnceWith("upload failed"),+ "the rejection is reported once, with the server's own message"+ );++ upload.teardown();+ });++ test("a batch drained by a cancel still reports the failure", async function (assert) {+ const alert = sinon.stub(dialog, "alert");++ pretender.post(UPLOAD_URL, async (request) => {+ const fileName = request.requestBody.get("file").name;++ if (fileName === REJECTED_FILE) {+ return UNPROCESSABLE;+ }++ await new Promise(() => {});+ });++ const upload = new UppyUpload(getOwner(this), {+ id: "uppy-upload-cancel-test",+ type: "composer",+ uploadDone: () => {},+ });++ upload.setup(this.fileInput);+ upload.uppyWrapper.uppyInstance.on("upload-error", () => {+ const sibling = upload.inProgressUploads.find(+ (inProgress) => inProgress.fileName === SIBLING_FILE+ );+ upload.cancelSingleUpload({ fileId: sibling.id });+ });++ await upload.addFiles([+ createFile(REJECTED_FILE),+ createFile(SIBLING_FILE),+ ]);++ await waitUntil(() => !upload.uploading, { timeout: 5000 });++ assert.true(+ alert.calledOnceWith("upload failed"),+ "the buffered failure is reported once the cancel drains the batch"+ );++ upload.teardown();+ });+});diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-composer-uploads.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-composer-uploads.gjsindex d179d3537f0..e315ca2c077 100644--- a/plugins/chat/assets/javascripts/discourse/components/chat-composer-uploads.gjs+++ b/plugins/chat/assets/javascripts/discourse/components/chat-composer-uploads.gjs@@ -83,7 +83,7 @@ export default class ChatComposerUploads extends Component {didReceiveAttrs() {super.didReceiveAttrs(...arguments);if (this.inProgressUploads?.length > 0) {- this.uppyUpload.uppyWrapper.uppyInstance?.cancelAll();+ this.uppyUpload.cancelAllUploads();}this.uploads = this.existingUploads ? cloneJSON(this.existingUploads) : [];