fix(artifacts): own DMG readers before attachment

Make inventory reader construction synchronous and DMG attachment lazy, so the inventory registers its reader before native operations start. Share initialization, reject work after close, wait for an in-flight attach before cleanup, and preserve retryable close state.

Cover failed-attach inventory cleanup, concurrent initialization, close-before-use, and close-during-attach alongside existing ownership, cancellation, and integrity cases. Session-level retention after inventory cleanup failure remains a separate concern.
This commit is contained in:
morluto
2026-10-11 04:15:26 +08:00
parent 5a77a962ec
commit bbd01923e9
6 changed files with 325 additions and 108 deletions
+34 -35
View File
@@ -111,7 +111,9 @@ const createSystemHost = (
/** Read-only macOS DMG adapter that owns attachment and reverse-order detach. */
export class NativeDmgArtifactReader implements ArtifactReader {
readonly format = "file" as const;
private readonly host: NativeDmgHost;
readonly #provenance: ArtifactCommand[] = [];
#initialization: Promise<void> | undefined;
#directory: DirectoryArtifactReader | undefined;
#devices: string[] = [];
#inventoryConfirmedDevices = new Set<string>();
@@ -121,33 +123,24 @@ export class NativeDmgArtifactReader implements ArtifactReader {
#attachOutputReturned = false;
#attachMayHaveMounted = false;
#ownershipUncertainty: string | undefined;
#closed = false;
#closePromise: Promise<void> | undefined;
private constructor(
constructor(
private readonly path: string,
private readonly host: NativeDmgHost,
) {}
/** Verify and attach one image beneath an exclusively owned temporary root. */
static async create(
path: string,
environment: Readonly<NodeJS.ProcessEnv>,
signal?: AbortSignal,
host?: NativeDmgHost,
): Promise<NativeDmgArtifactReader> {
) {
if (process.platform !== "darwin" && host === undefined)
throw new ArtifactReaderFailure(
"unavailable",
"Native DMG traversal is available only on macOS",
);
const reader = new NativeDmgArtifactReader(
path,
host ?? createSystemHost(environment),
);
await reader.attach(signal);
return reader;
this.host = host ?? createSystemHost(environment);
}
async *entries(signal?: AbortSignal): AsyncIterable<ArtifactEntry> {
await this.#ensureAttached(signal);
if (this.#directory === undefined)
throw new ArtifactReaderFailure("unavailable", "DMG is not attached");
const prefix = basename(this.path);
@@ -155,11 +148,10 @@ export class NativeDmgArtifactReader implements ArtifactReader {
yield { ...entry, path: `${prefix}/${entry.path}` };
}
open(entry: ArtifactEntry, signal?: AbortSignal): Promise<Readable> {
async open(entry: ArtifactEntry, signal?: AbortSignal): Promise<Readable> {
await this.#ensureAttached(signal);
if (this.#directory === undefined)
return Promise.reject(
new ArtifactReaderFailure("unavailable", "DMG is not attached"),
);
throw new ArtifactReaderFailure("unavailable", "DMG is not attached");
return this.#directory.open(entry, signal);
}
@@ -167,7 +159,19 @@ export class NativeDmgArtifactReader implements ArtifactReader {
return structuredClone(this.#provenance);
}
async close(): Promise<void> {
close(): Promise<void> {
this.#closed = true;
this.#closePromise ??= this.#closeOwnedResources().catch(
(cause: unknown) => {
this.#closePromise = undefined;
throw cause;
},
);
return this.#closePromise;
}
async #closeOwnedResources(): Promise<void> {
await this.#initialization?.catch(() => undefined);
let detachFailure = await this.#reconcileAttachState();
let remainingDevices: string[] = [];
for (const device of [...this.#devices].reverse()) {
@@ -281,7 +285,16 @@ export class NativeDmgArtifactReader implements ArtifactReader {
}
}
async attach(signal?: AbortSignal): Promise<void> {
async #ensureAttached(signal?: AbortSignal): Promise<void> {
if (this.#closed)
throw new ArtifactReaderFailure("unavailable", "DMG reader is closed");
this.#initialization ??= this.#attach(signal);
await this.#initialization;
if (this.#closed)
throw new ArtifactReaderFailure("unavailable", "DMG reader is closed");
}
async #attach(signal?: AbortSignal): Promise<void> {
await runChecked(this.host, ["verify", this.path], signal);
this.#provenance.push(command(["verify", this.path], ["read"]));
const baseline = await this.#readInfo();
@@ -361,20 +374,6 @@ export class NativeDmgArtifactReader implements ArtifactReader {
this.#attachMayHaveMounted = false;
this.#ownershipUncertainty = undefined;
}
let cleanupFailure: unknown;
try {
await this.close();
} catch (cleanupCause: unknown) {
cleanupFailure = cleanupCause;
}
if (cleanupFailure !== undefined)
throw ArtifactReaderFailure.withCleanup(
cause,
ArtifactReaderFailure.cleanupObservation(
cleanupFailure,
`DMG mount root ${this.#mountRoot ?? this.path}`,
),
);
throw cause;
}
}
+3 -4
View File
@@ -8,13 +8,12 @@ import type { ArtifactOccurrence } from "../../domain/artifactGraph.js";
import { ZIP_NON_ENTRY_TAIL_LIMITATION } from "../../domain/zipPackageFormat.js";
import type { StableRegularFileDescriptor } from "../../filesystem/RegularFile.js";
export const createReader = async (
export const createReader = (
path: string,
format: ArtifactOccurrence["artifact_format"],
environment: Readonly<NodeJS.ProcessEnv>,
signal?: AbortSignal,
rootSource?: StableRegularFileDescriptor,
): Promise<ArtifactReader | undefined> => {
): ArtifactReader | undefined => {
switch (format) {
case "directory":
return new DirectoryArtifactReader(path);
@@ -32,7 +31,7 @@ export const createReader = async (
: undefined;
case "dmg":
if (process.platform !== "darwin") return undefined;
return NativeDmgArtifactReader.create(path, environment, signal);
return new NativeDmgArtifactReader(path, environment);
default:
return undefined;
}
+1 -2
View File
@@ -68,11 +68,10 @@ export const scanCanonicalArtifactInventory = async (
let reader: ArtifactReader | undefined;
let readerCreationFailure: { readonly cause: unknown } | undefined;
try {
reader = await readerFactory(
reader = readerFactory(
path,
rootFormat,
options.environment ?? {},
options.signal,
rootSource,
);
} catch (cause: unknown) {