mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 13:12:26 +00:00
fix(target): preserve physical admission failures through cancellation (#12339)
* fix(target): preserve physical admission failures through cancellation * fix(target): preserve cancellation facts after descriptor retry
This commit is contained in:
1 parent
66bb3e932e
commit
29b2f96f74
2 files changed
+108
-27
No files matched your search
@@ -26,6 +26,7 @@ import {
|
||||
ArtifactReaderFailure,
|
||||
} from "../resources/index.js";
|
||||
import { jsonValueSchema } from "../domain/jsonValue.js";
|
||||
import { analysisErrorWithCancellation } from "../domain/analysisErrorCancellation.js";
|
||||
import { childFailureDiagnostics } from "../domain/analysisErrorProjection.js";
|
||||
import { constants, type Stats } from "node:fs";
|
||||
import { access, realpath, stat, type FileHandle } from "node:fs/promises";
|
||||
@@ -39,6 +40,7 @@ import {
|
||||
import {
|
||||
assertStableRegularFileIdentity,
|
||||
RegularFileChangedError,
|
||||
RegularFileOperationCancelled,
|
||||
type StableRegularFileDescriptor,
|
||||
} from "../filesystem/RegularFile.js";
|
||||
import { hashStableRegularFileDescriptor } from "../filesystem/StableFileHash.js";
|
||||
@@ -233,9 +235,6 @@ export const parseBinaryTarget = async (
|
||||
: options.resources.run(resolve));
|
||||
return finalizedTargetResolution(resolved, signal);
|
||||
} catch (cause: unknown) {
|
||||
// A caller-selected abort value never transfers file ownership.
|
||||
if (signal?.aborted === true && cause === signal.reason)
|
||||
return err(cancelledBy("resolve_binary_target", carriedReason(cause)));
|
||||
if (cause instanceof RegularFileCleanupFailure) {
|
||||
const fileFailure = cause as RegularFileCleanupFailure<TargetResolution>;
|
||||
if (options.resources === undefined)
|
||||
@@ -244,6 +243,7 @@ export const parseBinaryTarget = async (
|
||||
new RetainedRegularFileCleanupFailure(fileFailure),
|
||||
candidate,
|
||||
stage,
|
||||
signal,
|
||||
),
|
||||
);
|
||||
const retried = await retryRegularFileCleanup(
|
||||
@@ -252,15 +252,20 @@ export const parseBinaryTarget = async (
|
||||
signal,
|
||||
);
|
||||
if (retried.failure !== undefined)
|
||||
return err(targetFileCleanupError(retried.failure, candidate, stage));
|
||||
if (retried.outcome.kind === "failed")
|
||||
return err(
|
||||
targetFileCleanupError(retried.failure, candidate, stage, signal),
|
||||
);
|
||||
// Retry settles ownership. Use the file owner's original outcome so
|
||||
// the retry helper's raw cancellation reason cannot be mistaken for I/O.
|
||||
const { operationFailure, outcome } = fileFailure;
|
||||
if (outcome.kind === "failed")
|
||||
return targetResolutionFailure(
|
||||
fileFailure.operationFailure ?? retried.outcome.cause,
|
||||
operationFailure ?? outcome.cause,
|
||||
candidate,
|
||||
stage,
|
||||
signal,
|
||||
);
|
||||
return finalizedTargetResolution(retried.outcome.value, signal);
|
||||
return finalizedTargetResolution(outcome.value, signal);
|
||||
}
|
||||
if (cause instanceof RegularFileCancelled)
|
||||
return finalizedTargetResolution(
|
||||
@@ -560,12 +565,28 @@ const readOpenedBinaryTarget = async (
|
||||
});
|
||||
};
|
||||
|
||||
/** Retain a later interruption without changing the selected admission failure. */
|
||||
const retainTargetCancellation = <
|
||||
Failure extends BinaryTargetError | AnalysisCancelledError,
|
||||
>(
|
||||
failure: Failure,
|
||||
signal: AbortSignal | undefined,
|
||||
): Failure => {
|
||||
if (signal?.aborted === true)
|
||||
analysisErrorWithCancellation(
|
||||
failure,
|
||||
cancelledBy("resolve_binary_target", signal),
|
||||
);
|
||||
return failure;
|
||||
};
|
||||
|
||||
/** Preserve an earlier admission refusal over an interruption during final close. */
|
||||
const finalizedTargetResolution = (
|
||||
result: TargetResolution,
|
||||
signal?: AbortSignal,
|
||||
): TargetResolution => {
|
||||
if (!result.ok || signal?.aborted !== true) return result;
|
||||
if (!result.ok) return err(retainTargetCancellation(result.error, signal));
|
||||
if (signal?.aborted !== true) return result;
|
||||
return err(
|
||||
cancelledBy("resolve_binary_target", signal, {
|
||||
diagnostics: { completed_target: jsonValueSchema.parse(result.value) },
|
||||
@@ -578,6 +599,7 @@ const targetFileCleanupError = (
|
||||
failure: RetainedRegularFileCleanupFailure<TargetResolution>,
|
||||
candidate: string,
|
||||
stage: TargetFilesystemStage,
|
||||
signal: AbortSignal | undefined,
|
||||
): BinaryTargetError => {
|
||||
const cancellation = regularFileCancellation(failure);
|
||||
const completed =
|
||||
@@ -611,6 +633,8 @@ const targetFileCleanupError = (
|
||||
? {}
|
||||
: { diagnostics: { completed_target: completedTarget } },
|
||||
);
|
||||
if (primary instanceof BinaryTargetError)
|
||||
retainTargetCancellation(primary, signal);
|
||||
const error = new BinaryTargetError(
|
||||
failure.path,
|
||||
"The target file handle could not be closed",
|
||||
@@ -648,7 +672,7 @@ const targetResolutionFailure = (
|
||||
physical instanceof AnalysisCancelledError ||
|
||||
physical instanceof BinaryTargetError
|
||||
)
|
||||
return err(physical);
|
||||
return err(retainTargetCancellation(physical, signal));
|
||||
if (!(cause instanceof OwnedRegularFileOperationFailure)) {
|
||||
const cancellation = regularFileCancellation(cause);
|
||||
if (cancellation !== undefined)
|
||||
@@ -659,11 +683,9 @@ const targetResolutionFailure = (
|
||||
),
|
||||
);
|
||||
}
|
||||
if (signal?.aborted === true && physical === signal.reason)
|
||||
return err(cancelledBy("resolve_binary_target", carriedReason(physical)));
|
||||
const failure = classifyTargetFilesystemFailure(candidate, physical, stage);
|
||||
if (failure === undefined) throw physical;
|
||||
return err(failure);
|
||||
return err(retainTargetCancellation(failure, signal));
|
||||
};
|
||||
|
||||
const throwIfTargetResolutionCancelled = (signal?: AbortSignal): void => {
|
||||
@@ -892,7 +914,8 @@ const sha256Handle = async (
|
||||
})
|
||||
).sha256;
|
||||
} catch (cause: unknown) {
|
||||
throwIfTargetResolutionCancelled(signal);
|
||||
if (cause instanceof RegularFileOperationCancelled)
|
||||
throw cancelledBy("resolve_binary_target", carriedReason(cause.reason));
|
||||
if (cause instanceof RegularFileChangedError)
|
||||
throw new BinaryTargetError(path, "target changed while being read", {
|
||||
cause,
|
||||
|
||||
@@ -136,22 +136,55 @@ it("preserves unsupported-format diagnostics when admission is not cancelled", a
|
||||
expect(admission.handles[0]?.fd).toBe(-1);
|
||||
});
|
||||
|
||||
it("does not replace an unrelated stat failure with cancellation", async () => {
|
||||
const { path, controller } = await fixture(1);
|
||||
const failure = Object.assign(new Error("disk read failed"), { code: "EIO" });
|
||||
admission.statFailure = failure;
|
||||
expect(
|
||||
await parseBinaryTarget(path, { signal: controller.signal }),
|
||||
).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
it.each([
|
||||
{ checkpoint: 1, sameReason: false, closeFails: false },
|
||||
{ checkpoint: 1, sameReason: true, closeFails: false },
|
||||
{ checkpoint: 2, sameReason: false, closeFails: false },
|
||||
{ checkpoint: 2, sameReason: false, closeFails: true },
|
||||
])(
|
||||
"keeps physical stat $checkpoint failure through cancellation (same reason: $sameReason, close fails: $closeFails)",
|
||||
async ({ checkpoint, sameReason, closeFails }) => {
|
||||
const physical = Object.assign(new Error("disk read failed"), {
|
||||
code: "EIO",
|
||||
});
|
||||
const reason = sameReason
|
||||
? physical
|
||||
: { caller: "stop after stat failure" };
|
||||
const { path, controller } = await fixture(checkpoint, reason);
|
||||
admission.statFailure = physical;
|
||||
if (closeFails)
|
||||
admission.closeFailure = new Error("descriptor close failed");
|
||||
const resolved = await parseBinaryTarget(path, {
|
||||
signal: controller.signal,
|
||||
formatHint: "raw-binary",
|
||||
architecture: "x86",
|
||||
entryPoint: "0x1000",
|
||||
});
|
||||
if (resolved.ok) throw new Error("Expected the physical stat failure");
|
||||
const primary = resolved.error.cleanupPrimary ?? resolved.error;
|
||||
expect(primary).toMatchObject({
|
||||
_tag: "BinaryTargetError",
|
||||
cause: failure,
|
||||
reason: expect.stringContaining("(EIO)"),
|
||||
},
|
||||
});
|
||||
expect(admission.handles[0]?.fd).toBe(-1);
|
||||
});
|
||||
});
|
||||
expect(primary.cause).toBe(physical);
|
||||
expect(primary.secondaryFailures).toHaveLength(1);
|
||||
expect(primary.secondaryFailures[0]?._tag).toBe("AnalysisCancelledError");
|
||||
expect(primary.secondaryFailures[0]?.cause).toBe(reason);
|
||||
expect(resolved.error.cleanupIncomplete).toBe(closeFails);
|
||||
if (closeFails) {
|
||||
const failure = resolved.error.cause;
|
||||
if (!(failure instanceof RetainedRegularFileCleanupFailure))
|
||||
throw failure;
|
||||
expect(failure.owner.handle).toBe(admission.handles[0]);
|
||||
expect(failure.owner.handle.fd).toBeGreaterThanOrEqual(0);
|
||||
const resources = new ArtifactResourceScope();
|
||||
await retryRegularFileCleanup(failure, resources);
|
||||
await resources.close();
|
||||
expect(resolved.error.cleanupIncomplete).toBe(false);
|
||||
}
|
||||
expect(admission.handles[0]?.fd).toBe(-1);
|
||||
},
|
||||
);
|
||||
|
||||
it("retains the cleanup failure and its file owner when cancelled admission cannot close", async () => {
|
||||
const reason = new Error("caller cancelled");
|
||||
@@ -183,6 +216,31 @@ it("retains the cleanup failure and its file owner when cancelled admission cann
|
||||
expect(result.error.cleanupIncomplete).toBe(false);
|
||||
});
|
||||
|
||||
it.each([
|
||||
"stop before admission",
|
||||
Object.assign(new Error("caller-selected reason"), { code: "EIO" }),
|
||||
])(
|
||||
"preserves typed cancellation after a successful descriptor close retry",
|
||||
async (reason) => {
|
||||
const { path, controller } = await fixture(1, reason);
|
||||
admission.closeFailure = new Error("first descriptor close failed");
|
||||
const resources = new ArtifactResourceScope();
|
||||
try {
|
||||
const resolved = await parseBinaryTarget(path, {
|
||||
signal: controller.signal,
|
||||
resources,
|
||||
});
|
||||
if (resolved.ok) throw new Error("cancelled target was admitted");
|
||||
expect(resolved.error._tag).toBe("AnalysisCancelledError");
|
||||
expect(resolved.error.cause).toBe(reason);
|
||||
expect(resolved.error.cleanupIncomplete).toBe(false);
|
||||
expect(admission.handles[0]?.fd).toBe(-1);
|
||||
} finally {
|
||||
await resources.close();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
it("settles SDK cancellation at the admission boundary and reuses the same MCP connection", async () => {
|
||||
const { path, controller } = await fixture(0);
|
||||
const createClient = vi.fn(() => {
|
||||
|
||||
Reference in new issue
Block a user