From 9d4c517b848ae3ff860ec61a9149bf8afde17809 Mon Sep 17 00:00:00 2001 From: 64579525 <17373302683@163.com> Date: Tue, 13 Oct 2026 12:05:26 +0800 Subject: [PATCH] fix(target): preserve typed cancellation during descriptor admission --- src/application/BinaryTargetResolver.ts | 7 +++ .../filesystem/inesCancellation.test.ts | 61 +++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/src/application/BinaryTargetResolver.ts b/src/application/BinaryTargetResolver.ts index 7bd3e3684..5d0aa2ff3 100644 --- a/src/application/BinaryTargetResolver.ts +++ b/src/application/BinaryTargetResolver.ts @@ -210,6 +210,13 @@ export const parseBinaryTarget = async ( await handle.close(); } } catch (cause: unknown) { + // The filesystem admission helper throws the caller's abort reason after + // confirming descriptor release. Cleanup failures carry a different owner + // error and must remain visible rather than being reduced to cancellation. + if (signal?.aborted === true && cause === signal.reason) + return err( + new AnalysisCancelledError("resolve_binary_target", { cause }), + ); if (cause instanceof AnalysisCancelledError) return err(cause); if (cause instanceof BinaryTargetError) return err(cause); if (!(cause instanceof Error)) throw cause; diff --git a/tests/boundary/filesystem/inesCancellation.test.ts b/tests/boundary/filesystem/inesCancellation.test.ts index d979c7f95..9017dc964 100644 --- a/tests/boundary/filesystem/inesCancellation.test.ts +++ b/tests/boundary/filesystem/inesCancellation.test.ts @@ -8,6 +8,7 @@ import { hashStableRootArtifactHandle } from "../../../src/artifacts/inventory/h import { inventoryArtifact } from "../../fixtures/artifactInventory.js"; import { inesFixture } from "../../fixtures/ines.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { RegularFileAdmissionFailure } from "../../../src/filesystem/RegularFile.js"; // Complete the real I/O, then cancel while its caller is still awaiting it. const cancelAfterNextStat = ( @@ -37,6 +38,66 @@ const malformed = (format: string) => { return bytes.subarray(0, 20); }; +it.each(["default", "caller-error", "failed-close"])( + "preserves cancellation and descriptor ownership during %s admission", + async (reasonKind) => { + const root = await createTestTempDirectory("rea-ines-cancel-admission-"); + const path = join(root, "source.nes"); + await writeFile(path, inesFixture()); + const sample = await open(path, "r"); + const prototype = Object.getPrototypeOf(sample) as Pick; + await sample.close(); + const original = prototype.stat; + const controller = new AbortController(); + let admitted: FileHandle | undefined; + const cleanupFailure = new Error("descriptor close failed"); + let restoreClose = () => {}; + const stat = vi + .spyOn(prototype, "stat") + .mockImplementationOnce(async function (this: FileHandle, ...args) { + admitted = this; + const observed = await original.call(this, ...args); + if (reasonKind === "failed-close") { + const close = vi + .spyOn(this, "close") + .mockRejectedValueOnce(cleanupFailure); + restoreClose = () => close.mockRestore(); + } + controller.abort( + reasonKind === "caller-error" + ? new Error("caller stopped admission") + : undefined, + ); + return observed; + }); + try { + const result = parseBinaryTarget(path, { signal: controller.signal }); + if (reasonKind === "failed-close") { + const failure = await result.catch((cause: unknown) => cause); + expect(failure).toBeInstanceOf(RegularFileAdmissionFailure); + if (!(failure instanceof RegularFileAdmissionFailure)) throw failure; + expect(failure.cause).toBe(controller.signal.reason); + expect(failure.cleanupCause).toBe(cleanupFailure); + await failure.owner.close(); + } else { + await expect(result).resolves.toMatchObject({ + ok: false, + error: { + _tag: "AnalysisCancelledError", + operation: "resolve_binary_target", + }, + }); + } + if (admitted === undefined) + throw new Error("Admission descriptor was not observed"); + await expect(admitted.stat()).rejects.toMatchObject({ code: "EBADF" }); + } finally { + restoreClose(); + stat.mockRestore(); + } + }, +); + it.each([ ["truncated", "read"], ["NES 2.0", "read"],