From 336abaa80f405717b82111656f204d48bbde53b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 7 Nov 2026 11:28:13 +0000 Subject: [PATCH] fix(cli): stop waiting for the cloud's file check when the caller aborts (#7361) --- packages/cli/src/cloud/upload.ts | 8 +++++++- packages/cli/src/cloud/uploadComplete.test.ts | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/cloud/upload.ts b/packages/cli/src/cloud/upload.ts index 7963b5fa2e..b322d7db1b 100644 --- a/packages/cli/src/cloud/upload.ts +++ b/packages/cli/src/cloud/upload.ts @@ -399,6 +399,7 @@ export async function completeWithRetry( asset_id: string, checksum_sha256: string, sizeBytes: number, + signal?: AbortSignal, ): Promise<{ asset_id: string; url: string }> { const budget = completeBudgetMs(sizeBytes); const startedAt = Date.now(); @@ -415,7 +416,11 @@ export async function completeWithRetry( { cause: err }, ); } - await sleep(Math.min(poll, left)); + await sleep(Math.min(poll, left), signal); + if (signal?.aborted) + throw new Error("Stopped waiting for the cloud to finish checking the uploaded file.", { + cause: signal.reason, + }); poll = Math.min(poll * 2, completeWait.maxPollMs); } } @@ -472,6 +477,7 @@ export async function uploadAssetViaDirectUpload( initialize.asset_id, checksum_sha256, size_bytes, + opts.signal, ).catch((err: unknown) => { throw nameRefusedFile(err, filename, opts.contentType); }); diff --git a/packages/cli/src/cloud/uploadComplete.test.ts b/packages/cli/src/cloud/uploadComplete.test.ts index a89c8fca4b..edf492fc6c 100644 --- a/packages/cli/src/cloud/uploadComplete.test.ts +++ b/packages/cli/src/cloud/uploadComplete.test.ts @@ -33,7 +33,7 @@ describe("completeWithRetry waits for the check as long as the file's size allow expect(completeBudgetMs(100_000 * MIB)).toBe(600_000); }); - it("keeps polling through two minutes of 409s on a big file, then returns the asset", async () => { + it("keeps polling through more than a minute of 409s on a big file, then returns the asset", async () => { const { client, complete } = clientAnswering( ...Array.from({ length: 20 }, () => "409" as const), "ok", @@ -69,4 +69,18 @@ describe("completeWithRetry waits for the check as long as the file's size allow await expect(completeWithRetry(client, "a1", "sha", 1 * MIB)).rejects.toBe(refused); expect(complete).toHaveBeenCalledOnce(); }); + + it("stops waiting when the caller aborts", async () => { + const { client, complete } = clientAnswering(); + const controller = new AbortController(); + + const done = completeWithRetry(client, "a1", "sha", 600 * MIB, controller.signal); + const aborted = expect(done).rejects.toThrow(/Stopped waiting for the cloud/); + await vi.advanceTimersByTimeAsync(1_000); + controller.abort(new Error("cancelled")); + await vi.advanceTimersByTimeAsync(1); + + await aborted; + expect(complete.mock.calls.length).toBeLessThan(6); + }); });