From 50f7e81432001239ca8b5de3a9cfe8f5980d2711 Mon Sep 17 00:00:00 2001 From: Rudy Mizrahi Celekli <47457359+rudycelekli@users.noreply.github.com> Date: Mon, 5 Oct 2026 13:51:26 -0400 Subject: [PATCH] fix(artifacts): interrupt ZIP extraction under backpressure (#594) Cancel ZIP extraction promptly when an unread consumer applies backpressure, releasing the producer instead of leaving it parked on a drain that can never arrive. --- src/artifacts/ZipArtifactReader.ts | 22 ++++++++- .../zipBackpressureCancellation.test.ts | 46 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/boundary/providers/native/zipBackpressureCancellation.test.ts diff --git a/src/artifacts/ZipArtifactReader.ts b/src/artifacts/ZipArtifactReader.ts index ae1bb7710..4f80c087b 100644 --- a/src/artifacts/ZipArtifactReader.ts +++ b/src/artifacts/ZipArtifactReader.ts @@ -106,10 +106,29 @@ export class ZipArtifactReader implements ArtifactReader { const extractStream = (entry: FileEntry, signal?: AbortSignal): Readable => { const output = new PassThrough(); + const controller = new AbortController(); + const onAbort = (): void => { + controller.abort(signal?.reason); + // AbortSignal listeners dispatch synchronously, so this destroy wins the race + // against `getData` rejecting with its own AbortError. That ordering is what + // keeps the caller-visible failure typed as `cancelled`. + output.destroy( + new ArtifactReaderFailure("cancelled", "ZIP operation cancelled"), + ); + }; + signal?.addEventListener("abort", onAbort, { once: true }); + // `close` covers both a consumer walking away mid-entry and normal completion + // after `end()`; aborting then is harmless because `getData` has already + // resolved, and it releases the producer in the abandoned-consumer case. + output.once("close", () => { + signal?.removeEventListener("abort", onAbort); + controller.abort(); + }); const writable = new WritableStream({ write: async (chunk) => { abortIfNeeded(signal); - if (!output.write(Buffer.from(chunk))) await once(output, "drain"); + if (!output.write(Buffer.from(chunk))) + await once(output, "drain", { signal: controller.signal }); }, close: () => { output.end(); @@ -120,6 +139,7 @@ const extractStream = (entry: FileEntry, signal?: AbortSignal): Readable => { }); void entry .getData(writable, { + signal: controller.signal, checkSignature: true, checkOverlappingEntry: true, onprogress: () => abortIfNeeded(signal), diff --git a/tests/boundary/providers/native/zipBackpressureCancellation.test.ts b/tests/boundary/providers/native/zipBackpressureCancellation.test.ts new file mode 100644 index 000000000..fbdf0a469 --- /dev/null +++ b/tests/boundary/providers/native/zipBackpressureCancellation.test.ts @@ -0,0 +1,46 @@ +import { once } from "node:events"; +import { writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { TextReader, Uint8ArrayWriter, ZipWriter } from "@zip.js/zip.js"; +import { expect, it } from "vitest"; +import { ZipArtifactReader } from "../../../../src/artifacts/ZipArtifactReader.js"; +import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js"; + +it("cancels a ZIP read while its consumer is applying backpressure", async () => { + const root = await createTestTempDirectory("rea-zip-backpressure-"); + const path = join(root, "fixture.zip"); + const writer = new ZipWriter(new Uint8ArrayWriter()); + await writer.add("large.txt", new TextReader("x".repeat(2 * 1024 * 1024)), { + level: 0, + }); + await writeFile(path, await writer.close()); + const reader = new ZipArtifactReader(path, "zip"); + try { + const entries = []; + for await (const entry of reader.entries()) entries.push(entry); + const entry = entries[0]; + expect(entry).toBeDefined(); + if (entry === undefined) return; + const controller = new AbortController(); + const stream = await reader.open(entry, controller.signal); + stream.on("error", () => undefined); + try { + await once(stream, "readable"); + expect(stream.readableLength).toBeGreaterThan(0); + const errorEvent = once(stream, "error"); + controller.abort(); + expect(stream.destroyed).toBe(true); + const [failure] = await errorEvent; + expect(failure).toMatchObject({ + name: "ArtifactReaderFailure", + reason: "cancelled", + }); + } finally { + // Resume an old adapter's stalled producer so a failing regression owns cleanup too. + stream.resume(); + stream.destroy(); + } + } finally { + await reader.close(); + } +});