fix(reference): retain cancellation during policy file cleanup (#12323)

fix(reference): preserve cancellation during policy file cleanup
This commit is contained in:
morluto authored and GitHub committed 2026-11-19 12:40:46 +00:00
1 parent c5709b8697
commit 00e2346c2c
2 files changed
+65 -18

No files matched your search

+12 -18
View File
@@ -162,42 +162,36 @@ const readPolicyFile = async (
new IgnorePolicyReadCancelled(reason),
});
} catch (cause: unknown) {
if (cause instanceof IgnorePolicyReadCancelled) throw cause;
if (!(cause instanceof RegularFileCleanupFailure)) throw cause;
const cancellation = regularFileCancellation(cause);
const retry = await retryRegularFileCleanup(cause, resources, signal);
const operationCancellation =
cause.outcome.kind === "failed"
? regularFileCancellation(cause)
: undefined;
const cancellationFailure =
cancellation === undefined
? undefined
: new IgnorePolicyReadCancelled(cancellation.reason);
if (retry.cleanup !== undefined) {
const primary =
retry.outcome.kind === "failed"
cancellationFailure ??
(retry.outcome.kind === "failed"
? retry.outcome.cause
: cause.cleanupCause;
: cause.cleanupCause);
const primaryCause = ignorePolicyCause(primary);
return err({
...failure(
isIgnorePolicyCancellation(primary) ||
operationCancellation !== undefined
? "cancelled"
: "io",
isIgnorePolicyCancellation(primary) ? "cancelled" : "io",
`Reference source ignore policy could not be read at ${policyPath}: ${errorMessage(primaryCause)}`,
),
cleanup: retry.cleanup,
cause: primaryCause,
...(signal !== undefined &&
isAborted(signal) &&
!isIgnorePolicyCancellation(primary) &&
operationCancellation === undefined
!isIgnorePolicyCancellation(primary)
? { secondaryCancellation: { cause: signal.reason } }
: {}),
});
}
if (retry.outcome.kind === "failed") {
if (operationCancellation !== undefined)
throw new IgnorePolicyReadCancelled(operationCancellation.reason);
throw retry.outcome.cause;
}
if (cancellationFailure !== undefined) throw cancellationFailure;
if (retry.outcome.kind === "failed") throw retry.outcome.cause;
text = retry.outcome.value;
}
return ok(text);
@@ -491,6 +491,59 @@ it.each([
},
);
it("preserves cancellation during final policy close and retains the descriptor for retry", async () => {
const root = await createTestTempDirectory("rea-reference-policy-cancel-");
const policyPath = join(root, ".gitignore");
await writeFile(policyPath, "ignored\n");
const resources = new ArtifactResourceScope();
const controller = new AbortController();
const reason = { source: "caller" };
const closeFailure = new Error("policy close failed");
let policyHandle: FileHandle | undefined;
let closeAttempts = 0;
let allowClose = false;
openMock.mockImplementation(async (...args) => {
const handle = await actual.open(...args);
if (resolve(String(args[0])) !== resolve(policyPath)) return handle;
policyHandle = handle;
const close = handle.close.bind(handle);
vi.spyOn(handle, "close").mockImplementation(async () => {
closeAttempts += 1;
controller.abort(reason);
if (!allowClose) throw closeFailure;
await close();
});
return handle;
});
try {
const result = await importReferenceSource(
{
root,
caller: "test",
policy: { secretPatterns: [] },
signal: controller.signal,
},
resources,
);
expect(result).toMatchObject({
ok: false,
error: { code: "cancelled", cleanup: { resources: [policyPath] } },
});
if (result.ok) throw new Error("Expected policy cancellation");
expect(result.error.cause).toBe(reason);
expect(result.error.secondaryCancellation).toBeUndefined();
expect(result.error.cleanup?.reason).toContain(closeFailure.message);
expect(closeAttempts).toBe(2);
expect(policyHandle?.fd).toBeGreaterThanOrEqual(0);
expect(resources.cleanupResources).toEqual([policyPath]);
} finally {
allowClose = true;
await resources.close();
}
expect(policyHandle?.fd).toBe(-1);
expect(resources.hasPending).toBe(false);
});
it.each([false, true])(
"preserves a policy read failure and later cancellation (close fails: %s)",
async (closeFails) => {