From 0fe2c80cda9b551fe43e38f096fb65d32b1adca3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20Tonh=C3=A4user?= Date: Sun, 9 Aug 2026 21:49:34 +0200 Subject: [PATCH] fix: contain immich stream cancel races --- src/modules/immich/client.test.ts | 22 ++++++++++++++++++++++ src/modules/immich/client.ts | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/modules/immich/client.test.ts b/src/modules/immich/client.test.ts index b76e3cc..6183811 100644 --- a/src/modules/immich/client.test.ts +++ b/src/modules/immich/client.test.ts @@ -182,6 +182,28 @@ test("maps a caller abort during a binary stream to IMMICH_ABORTED", async () => await expect(read).rejects.toMatchObject({ code: "IMMICH_ABORTED" }); }); +test("contains an upstream binary cancel rejection after caller abort", async () => { + const caller = new AbortController(); + const streamingClient = createImmichClient({ + baseUrl, + apiKey, + fetch: async () => { + const body = new ReadableStream({ + cancel() { + return Promise.reject(new Error("upstream cancel race")); + }, + }); + return new Response(body, { headers: { "content-type": "image/jpeg" } }); + }, + }); + + const original = await streamingClient.getOriginal("asset-1", caller.signal); + const read = original.body.getReader().read(); + caller.abort(); + + await expect(read).rejects.toMatchObject({ code: "IMMICH_ABORTED" }); +}); + test("preserves abort, timeout, and network failures while reading JSON bodies", async () => { const caller = new AbortController(); const abortedClient = createImmichClient({ diff --git a/src/modules/immich/client.ts b/src/modules/immich/client.ts index 58628c1..0d4025c 100644 --- a/src/modules/immich/client.ts +++ b/src/modules/immich/client.ts @@ -261,7 +261,7 @@ export function createImmichClient({ } const error = failureError(lifecycle); finish(); - void reader.cancel(error); + void reader.cancel(error).catch(() => undefined); controller.error(error); }; lifecycle.signal.addEventListener("abort", abortStream, { once: true });