fix(artifacts)!: preserve descriptor cleanup authority
Keep rejected closes unresolved after descriptor invalidation and share concurrent close attempts. Preserve primary admission failures and transfer the exact failed handle owner into inventory scopes. BREAKING CHANGE: ArtifactResourceScope file-handle owners require OwnedFileHandle. Root inventory sources retain their single cleanup owner.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { FileHandle } from "node:fs/promises";
|
||||
import type { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js";
|
||||
|
||||
import type { ArtifactReader } from "./ArtifactReader.js";
|
||||
import { ArtifactReaderFailure } from "./ArtifactReader.js";
|
||||
@@ -14,7 +14,7 @@ export type ArtifactResourceOwner =
|
||||
| {
|
||||
readonly kind: "file-handle";
|
||||
readonly resource: string;
|
||||
readonly handle: FileHandle;
|
||||
readonly handle: OwnedFileHandle;
|
||||
}
|
||||
| {
|
||||
readonly kind: "output-tree";
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import { expect, it, vi } from "vitest";
|
||||
|
||||
import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js";
|
||||
|
||||
it("shares one in-flight successful close between concurrent owners", async () => {
|
||||
let resolveClose: (() => void) | undefined;
|
||||
const file = {
|
||||
fd: 17,
|
||||
close: vi.fn(
|
||||
() =>
|
||||
new Promise<void>((resolve) => {
|
||||
file.fd = -1;
|
||||
resolveClose = resolve;
|
||||
}),
|
||||
),
|
||||
};
|
||||
const owner = new OwnedFileHandle(file);
|
||||
|
||||
const first = owner.close();
|
||||
const second = owner.close();
|
||||
await Promise.resolve();
|
||||
expect(file.close).toHaveBeenCalledTimes(1);
|
||||
resolveClose?.();
|
||||
|
||||
await expect(Promise.all([first, second])).resolves.toEqual([
|
||||
undefined,
|
||||
undefined,
|
||||
]);
|
||||
await expect(owner.close()).resolves.toBeUndefined();
|
||||
expect(file.close).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("shares one in-flight failed close and keeps its unconfirmed result sticky", async () => {
|
||||
const failure = new Error("native close failed");
|
||||
let rejectClose: (() => void) | undefined;
|
||||
const file = {
|
||||
fd: 17,
|
||||
close: vi.fn(
|
||||
() =>
|
||||
new Promise<void>((_resolve, reject) => {
|
||||
file.fd = -1;
|
||||
rejectClose = () => reject(failure);
|
||||
}),
|
||||
),
|
||||
};
|
||||
const owner = new OwnedFileHandle(file);
|
||||
|
||||
const first = owner.close();
|
||||
const second = owner.close();
|
||||
await Promise.resolve();
|
||||
expect(file.close).toHaveBeenCalledTimes(1);
|
||||
rejectClose?.();
|
||||
|
||||
await expect(Promise.all([first, second])).rejects.toBe(failure);
|
||||
await expect(owner.close()).rejects.toBe(failure);
|
||||
expect(file.close).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
@@ -0,0 +1,68 @@
|
||||
import type { Stats } from "node:fs";
|
||||
import type { FileHandle } from "node:fs/promises";
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
NonRegularFileReadError,
|
||||
openRegularFile,
|
||||
RegularFileAdmissionFailure,
|
||||
} from "../filesystem/RegularFile.js";
|
||||
|
||||
const { mockOpen } = vi.hoisted(() => ({ mockOpen: vi.fn() }));
|
||||
|
||||
vi.mock("node:fs/promises", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("node:fs/promises")>()),
|
||||
open: mockOpen,
|
||||
}));
|
||||
|
||||
afterEach(() => mockOpen.mockReset());
|
||||
|
||||
it.each(["cancellation after open", "non-regular metadata", "stat failure"])(
|
||||
"retains the exact owner when close fails after %s",
|
||||
async (failureCase) => {
|
||||
const closeFailure = new Error("native close failed");
|
||||
const statFailure = new Error("descriptor stat failed");
|
||||
const cancellation = new Error("cancelled after open");
|
||||
const controller = new AbortController();
|
||||
const file = {
|
||||
fd: 17,
|
||||
stat: vi.fn(async () => {
|
||||
if (failureCase === "cancellation after open") {
|
||||
controller.abort(cancellation);
|
||||
return { isFile: () => true } as Stats;
|
||||
}
|
||||
if (failureCase === "non-regular metadata")
|
||||
return {
|
||||
isFile: () => false,
|
||||
isDirectory: () => false,
|
||||
} as Stats;
|
||||
throw statFailure;
|
||||
}),
|
||||
close: vi.fn(async () => {
|
||||
file.fd = -1;
|
||||
throw closeFailure;
|
||||
}),
|
||||
};
|
||||
mockOpen.mockResolvedValue(file as unknown as FileHandle);
|
||||
|
||||
const cause = await openRegularFile("/selected/input", {
|
||||
symlinks: "reject",
|
||||
signal: controller.signal,
|
||||
}).catch((failure: unknown) => failure);
|
||||
|
||||
expect(cause).toBeInstanceOf(RegularFileAdmissionFailure);
|
||||
if (!(cause instanceof RegularFileAdmissionFailure))
|
||||
throw new Error("Expected an admission failure that retains its owner");
|
||||
expect(cause.owner.handle).toBe(file);
|
||||
expect(cause.cleanupCause).toBe(closeFailure);
|
||||
expect(file.close).toHaveBeenCalledTimes(1);
|
||||
await expect(cause.owner.close()).rejects.toBe(closeFailure);
|
||||
expect(file.close).toHaveBeenCalledTimes(1);
|
||||
|
||||
if (failureCase === "non-regular metadata")
|
||||
expect(cause.cause).toBeInstanceOf(NonRegularFileReadError);
|
||||
else if (failureCase === "stat failure")
|
||||
expect(cause.cause).toBe(statFailure);
|
||||
else expect(cause.cause).toBe(cancellation);
|
||||
},
|
||||
);
|
||||
@@ -1,11 +1,8 @@
|
||||
import {
|
||||
NonRegularFileReadError,
|
||||
openRegularFile,
|
||||
sameRegularFileState,
|
||||
} from "../../filesystem/RegularFile.js";
|
||||
import { sameRegularFileState } from "../../filesystem/RegularFile.js";
|
||||
import type { FileHandle } from "node:fs/promises";
|
||||
import type { Stats } from "node:fs";
|
||||
import type { StableRegularFileDescriptor } from "../../filesystem/RegularFile.js";
|
||||
import type { OwnedFileHandle } from "../../filesystem/OwnedFileHandle.js";
|
||||
|
||||
import { classifyArtifactContent } from "./ArtifactGraphConstruction.js";
|
||||
import { ARTIFACT_CLASSIFICATION_PREFIX_BYTES } from "../ArtifactHash.js";
|
||||
@@ -17,7 +14,10 @@ import {
|
||||
import { ArtifactReaderFailure } from "../ArtifactReader.js";
|
||||
import { type ArtifactResourceOwner } from "../ArtifactResourceScope.js";
|
||||
import type { HashResult } from "../ArtifactHash.js";
|
||||
import { hashStableRootArtifactHandle } from "./hashStableRootArtifact.js";
|
||||
import {
|
||||
hashStableRootArtifactHandle,
|
||||
openRootArtifact,
|
||||
} from "./hashStableRootArtifact.js";
|
||||
import type { ArtifactInventoryOptions } from "./types.js";
|
||||
|
||||
interface RootClassification {
|
||||
@@ -26,9 +26,14 @@ interface RootClassification {
|
||||
}
|
||||
|
||||
type RootInventoryClassification = RootClassification & {
|
||||
readonly rootSource: StableRegularFileDescriptor | undefined;
|
||||
readonly rootSource: RootInventorySource | undefined;
|
||||
};
|
||||
|
||||
/** The admitted descriptor and the single owner that can confirm its close. */
|
||||
export interface RootInventorySource extends StableRegularFileDescriptor {
|
||||
readonly owner: OwnedFileHandle;
|
||||
}
|
||||
|
||||
/** Classify and hash one file root through the same stable open descriptor. */
|
||||
export const classifyAndHashRoot = async (
|
||||
path: string,
|
||||
@@ -47,7 +52,7 @@ export const classifyAndHashRoot = async (
|
||||
if (rootSource !== undefined) {
|
||||
const owner: ArtifactResourceOwner = {
|
||||
kind: "file-handle",
|
||||
handle: rootSource.handle,
|
||||
handle: rootSource.owner,
|
||||
resource: `root artifact descriptor for ${path}`,
|
||||
};
|
||||
const cleanupAttempt = await options.resourceScope.release(owner);
|
||||
@@ -73,10 +78,15 @@ export const classifyAndHashRootForInventory = async (
|
||||
): Promise<RootInventoryClassification> => {
|
||||
if (directory)
|
||||
return { format: "directory", digest: null, rootSource: undefined };
|
||||
const handle = await openRootFile(path, options.signal);
|
||||
const ownedHandle = await openRootArtifact(
|
||||
path,
|
||||
options.resourceScope,
|
||||
options.signal,
|
||||
);
|
||||
const handle = ownedHandle.handle;
|
||||
const owner: ArtifactResourceOwner = {
|
||||
kind: "file-handle",
|
||||
handle,
|
||||
handle: ownedHandle,
|
||||
resource: `root artifact descriptor for ${path}`,
|
||||
};
|
||||
let outcome:
|
||||
@@ -105,7 +115,9 @@ export const classifyAndHashRootForInventory = async (
|
||||
value: {
|
||||
format,
|
||||
digest,
|
||||
rootSource: retainSource ? { handle, initial } : undefined,
|
||||
rootSource: retainSource
|
||||
? { handle, initial, owner: ownedHandle }
|
||||
: undefined,
|
||||
},
|
||||
};
|
||||
} catch (cause: unknown) {
|
||||
@@ -133,23 +145,6 @@ const isZipFormat = (format: ArtifactOccurrence["artifact_format"]): boolean =>
|
||||
format === "msix" ||
|
||||
format === "appx";
|
||||
|
||||
const openRootFile = async (
|
||||
path: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<FileHandle> => {
|
||||
try {
|
||||
return await openRegularFile(path, { symlinks: "reject", signal });
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof NonRegularFileReadError)
|
||||
throw new ArtifactReaderFailure(
|
||||
"format",
|
||||
`Artifact root is not a regular file: ${path}`,
|
||||
{ cause },
|
||||
);
|
||||
throw cause;
|
||||
}
|
||||
};
|
||||
|
||||
const classifyRootFormat = async (
|
||||
path: string,
|
||||
handle: FileHandle,
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { lstat } from "node:fs/promises";
|
||||
import type { Stats } from "node:fs";
|
||||
import type { FileHandle } from "node:fs/promises";
|
||||
import { readFileHandleChunks } from "../../filesystem/readFileHandleChunks.js";
|
||||
import { OwnedFileHandle } from "../../filesystem/OwnedFileHandle.js";
|
||||
|
||||
import { ArtifactReaderFailure } from "../ArtifactReader.js";
|
||||
import {
|
||||
NonRegularFileReadError,
|
||||
RegularFileAdmissionFailure,
|
||||
openRegularFile,
|
||||
sameRegularFileState,
|
||||
} from "../../filesystem/RegularFile.js";
|
||||
@@ -13,33 +16,79 @@ import {
|
||||
hashReadable,
|
||||
type HashResult,
|
||||
} from "../ArtifactHash.js";
|
||||
import type { ArtifactResourceScope } from "../ArtifactResourceScope.js";
|
||||
|
||||
/** Admit a root descriptor without losing ownership when validation cleanup fails. */
|
||||
export const openRootArtifact = async (
|
||||
path: string,
|
||||
resourceScope: ArtifactResourceScope,
|
||||
signal?: AbortSignal,
|
||||
): Promise<OwnedFileHandle> => {
|
||||
try {
|
||||
return new OwnedFileHandle(
|
||||
await openRegularFile(path, { symlinks: "reject", signal }),
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
const primary =
|
||||
cause instanceof RegularFileAdmissionFailure ? cause.cause : cause;
|
||||
const failure =
|
||||
primary instanceof NonRegularFileReadError
|
||||
? new ArtifactReaderFailure(
|
||||
"format",
|
||||
`Artifact root is not a regular file: ${path}`,
|
||||
{ cause: primary },
|
||||
)
|
||||
: primary;
|
||||
if (cause instanceof RegularFileAdmissionFailure) {
|
||||
const resource = `root artifact descriptor for ${path}`;
|
||||
const cleanup = await resourceScope.release({
|
||||
kind: "file-handle",
|
||||
resource,
|
||||
handle: cause.owner,
|
||||
});
|
||||
if (cleanup.kind === "failed")
|
||||
throw ArtifactReaderFailure.withCleanup(
|
||||
failure,
|
||||
ArtifactReaderFailure.cleanupObservation(cleanup.cause, resource),
|
||||
);
|
||||
}
|
||||
throw failure;
|
||||
}
|
||||
};
|
||||
|
||||
/** Hash one stable regular root file from the same descriptor that was checked. */
|
||||
export const hashStableRootArtifact = async (
|
||||
path: string,
|
||||
resourceScope: ArtifactResourceScope,
|
||||
signal?: AbortSignal,
|
||||
): Promise<HashResult> => {
|
||||
let handle: FileHandle;
|
||||
try {
|
||||
handle = await openRegularFile(path, {
|
||||
symlinks: "reject",
|
||||
signal,
|
||||
});
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof NonRegularFileReadError)
|
||||
throw new ArtifactReaderFailure(
|
||||
"format",
|
||||
`Artifact root is not a regular file: ${path}`,
|
||||
{ cause },
|
||||
);
|
||||
throw cause;
|
||||
}
|
||||
const owner = await openRootArtifact(path, resourceScope, signal);
|
||||
const handle = owner.handle;
|
||||
let outcome:
|
||||
| { readonly kind: "completed"; readonly value: HashResult }
|
||||
| { readonly kind: "failed"; readonly cause: unknown };
|
||||
try {
|
||||
const initial = await handle.stat();
|
||||
return await hashStableRootArtifactHandle(path, handle, initial, signal);
|
||||
} finally {
|
||||
await handle.close();
|
||||
outcome = {
|
||||
kind: "completed",
|
||||
value: await hashStableRootArtifactHandle(path, handle, initial, signal),
|
||||
};
|
||||
} catch (cause: unknown) {
|
||||
outcome = { kind: "failed", cause };
|
||||
}
|
||||
const resource = `root artifact descriptor for ${path}`;
|
||||
const cleanup = await resourceScope.release({
|
||||
kind: "file-handle",
|
||||
resource,
|
||||
handle: owner,
|
||||
});
|
||||
if (cleanup.kind === "failed")
|
||||
throw ArtifactReaderFailure.withCleanup(
|
||||
outcome.kind === "failed" ? outcome.cause : cleanup.cause,
|
||||
ArtifactReaderFailure.cleanupObservation(cleanup.cause, resource),
|
||||
);
|
||||
if (outcome.kind === "failed") throw outcome.cause;
|
||||
return outcome.value;
|
||||
};
|
||||
|
||||
/** Hash and revalidate the same open descriptor used for root classification. */
|
||||
@@ -52,7 +101,7 @@ export const hashStableRootArtifactHandle = async (
|
||||
const digest = await hashReadable(
|
||||
// Read at most the admitted extent plus one byte to detect growth without
|
||||
// following an actively appended file indefinitely.
|
||||
handle.createReadStream({ autoClose: false, start: 0, end: initial.size }),
|
||||
readFileHandleChunks(handle, { start: 0, end: initial.size }),
|
||||
signal,
|
||||
);
|
||||
abortIfNeeded(signal);
|
||||
|
||||
@@ -5,7 +5,7 @@ import { AsarArtifactReader } from "../AsarArtifactReader.js";
|
||||
|
||||
import { lstat } from "node:fs/promises";
|
||||
import type { Stats } from "node:fs";
|
||||
import type { StableRegularFileDescriptor } from "../../filesystem/RegularFile.js";
|
||||
import type { RootInventorySource } from "./classify.js";
|
||||
|
||||
import {
|
||||
ArtifactReaderFailure,
|
||||
@@ -107,6 +107,7 @@ export const scanCanonicalArtifactInventoryInScope = async (
|
||||
rootFormat,
|
||||
rootDigest,
|
||||
rootSource,
|
||||
resourceScope: options.resourceScope,
|
||||
reader,
|
||||
signal: options.signal,
|
||||
nodes,
|
||||
@@ -146,7 +147,7 @@ const cleanupInventoryOwners = async ({
|
||||
}: {
|
||||
readonly path: string;
|
||||
readonly options: ArtifactInventoryOptions;
|
||||
readonly rootSource: StableRegularFileDescriptor | undefined;
|
||||
readonly rootSource: RootInventorySource | undefined;
|
||||
readonly ownedReaders: readonly ArtifactReader[];
|
||||
readonly outcome: InventoryScanOutcome;
|
||||
}): Promise<ArtifactReaderFailure | undefined> => {
|
||||
@@ -161,7 +162,7 @@ const cleanupInventoryOwners = async ({
|
||||
if (rootSource !== undefined)
|
||||
owners.push({
|
||||
kind: "file-handle",
|
||||
handle: rootSource.handle,
|
||||
handle: rootSource.owner,
|
||||
resource: `root artifact descriptor for ${path}`,
|
||||
});
|
||||
for (const owner of owners) {
|
||||
@@ -182,11 +183,12 @@ const cleanupInventoryOwners = async ({
|
||||
};
|
||||
|
||||
interface SnapshotBuildInput {
|
||||
readonly resourceScope: ArtifactInventoryOptions["resourceScope"];
|
||||
readonly path: string;
|
||||
readonly metadata: Stats;
|
||||
readonly rootFormat: ArtifactOccurrence["artifact_format"];
|
||||
readonly rootDigest: HashResult | null;
|
||||
readonly rootSource: StableRegularFileDescriptor | undefined;
|
||||
readonly rootSource: RootInventorySource | undefined;
|
||||
readonly reader: ArtifactReader | undefined;
|
||||
readonly signal: AbortSignal | undefined;
|
||||
readonly nodes: Map<string, ArtifactNode>;
|
||||
@@ -243,7 +245,13 @@ const buildInventorySnapshot = async (
|
||||
ordinal,
|
||||
}));
|
||||
|
||||
await verifyRootDigest(path, rootDigest, rootSource, signal);
|
||||
await verifyRootDigest(
|
||||
path,
|
||||
rootDigest,
|
||||
rootSource,
|
||||
input.resourceScope,
|
||||
signal,
|
||||
);
|
||||
|
||||
const graphSha256 = artifactGraphDigest({
|
||||
nodes: orderedNodes,
|
||||
@@ -318,13 +326,14 @@ const buildIntegrityContradictions = (
|
||||
const verifyRootDigest = async (
|
||||
path: string,
|
||||
rootDigest: HashResult | null,
|
||||
rootSource: StableRegularFileDescriptor | undefined,
|
||||
rootSource: RootInventorySource | undefined,
|
||||
resourceScope: ArtifactInventoryOptions["resourceScope"],
|
||||
signal: AbortSignal | undefined,
|
||||
): Promise<void> => {
|
||||
if (rootDigest === null) return;
|
||||
const verified =
|
||||
rootSource === undefined
|
||||
? await hashStableRootArtifact(path, signal)
|
||||
? await hashStableRootArtifact(path, resourceScope, signal)
|
||||
: await hashStableRootArtifactHandle(
|
||||
path,
|
||||
rootSource.handle,
|
||||
|
||||
Reference in New Issue
Block a user