mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 06:04:03 +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,
|
||||
|
||||
Reference in new issue
Block a user