From 00e2346c2c6c80f1dde003375708722287c6d51c Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 07:40:46 -0500 Subject: [PATCH] fix(reference): retain cancellation during policy file cleanup (#12323) fix(reference): preserve cancellation during policy file cleanup --- .../ReferenceSourceImportPolicy.ts | 30 +++++------ .../referenceSourceMetadataCleanup.test.ts | 53 +++++++++++++++++++ 2 files changed, 65 insertions(+), 18 deletions(-) diff --git a/src/application/ReferenceSourceImportPolicy.ts b/src/application/ReferenceSourceImportPolicy.ts index 3557f2148d..ad162eadb9 100644 --- a/src/application/ReferenceSourceImportPolicy.ts +++ b/src/application/ReferenceSourceImportPolicy.ts @@ -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); diff --git a/tests/boundary/filesystem/referenceSourceMetadataCleanup.test.ts b/tests/boundary/filesystem/referenceSourceMetadataCleanup.test.ts index 34375f756d..0420b3509b 100644 --- a/tests/boundary/filesystem/referenceSourceMetadataCleanup.test.ts +++ b/tests/boundary/filesystem/referenceSourceMetadataCleanup.test.ts @@ -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) => {