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.
This commit is contained in:
@@ -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<Uint8Array>({
|
||||
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),
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user