fix(artifacts): close mounted directory resources before DMG detach
Wait for directory operations, close owned streams and traversal handles, and retain the mounted reader when cleanup fails so close can retry before detach. Consolidate stable-artifact filesystem regressions into the boundary suite. Validation: 35 artifact tests and temporary-directory hygiene, including real hdiutil diagnostics. Directory-close and traversal races use injected faults.
This commit is contained in:
@@ -125,6 +125,7 @@ export class NativeDmgArtifactReader implements ArtifactReader {
|
||||
#ownershipUncertainty: string | undefined;
|
||||
#closed = false;
|
||||
#closePromise: Promise<void> | undefined;
|
||||
readonly #activeDirectoryOperations = new Set<Promise<unknown>>();
|
||||
|
||||
constructor(
|
||||
private readonly path: string,
|
||||
@@ -141,18 +142,30 @@ export class NativeDmgArtifactReader implements ArtifactReader {
|
||||
|
||||
async *entries(signal?: AbortSignal): AsyncIterable<ArtifactEntry> {
|
||||
await this.#ensureAttached(signal);
|
||||
if (this.#directory === undefined)
|
||||
const directory = this.#directory;
|
||||
if (directory === undefined)
|
||||
throw new ArtifactReaderFailure("unavailable", "DMG is not attached");
|
||||
const prefix = basename(this.path);
|
||||
for await (const entry of this.#directory.entries(signal))
|
||||
yield { ...entry, path: `${prefix}/${entry.path}` };
|
||||
const entries = directory.entries(signal)[Symbol.asyncIterator]();
|
||||
try {
|
||||
for (;;) {
|
||||
if (this.#closed) return;
|
||||
const next = await this.#trackDirectoryOperation(() => entries.next());
|
||||
if (this.#closed) return;
|
||||
if (next.done) return;
|
||||
yield { ...next.value, path: `${prefix}/${next.value.path}` };
|
||||
}
|
||||
} finally {
|
||||
await entries.return?.();
|
||||
}
|
||||
}
|
||||
|
||||
async open(entry: ArtifactEntry, signal?: AbortSignal): Promise<Readable> {
|
||||
await this.#ensureAttached(signal);
|
||||
if (this.#directory === undefined)
|
||||
const directory = this.#directory;
|
||||
if (directory === undefined)
|
||||
throw new ArtifactReaderFailure("unavailable", "DMG is not attached");
|
||||
return this.#directory.open(entry, signal);
|
||||
return this.#trackDirectoryOperation(() => directory.open(entry, signal));
|
||||
}
|
||||
|
||||
provenance(): readonly ArtifactCommand[] {
|
||||
@@ -172,32 +185,9 @@ export class NativeDmgArtifactReader implements ArtifactReader {
|
||||
|
||||
async #closeOwnedResources(): Promise<void> {
|
||||
await this.#initialization?.catch(() => undefined);
|
||||
let detachFailure = await this.#reconcileAttachState();
|
||||
let remainingDevices: string[] = [];
|
||||
for (const device of [...this.#devices].reverse()) {
|
||||
const failure = await this.#detach(device);
|
||||
if (failure !== undefined) {
|
||||
remainingDevices.push(device);
|
||||
detachFailure ??= failure;
|
||||
}
|
||||
}
|
||||
// APFS container detach may briefly leave its backing image listed.
|
||||
for (const delayMs of DETACH_SETTLE_DELAYS_MS) {
|
||||
if (remainingDevices.length === 0) break;
|
||||
await (this.host.delay ?? delay)(delayMs);
|
||||
const stillAttached: string[] = [];
|
||||
for (const device of remainingDevices) {
|
||||
if (!(await this.#isAttached(device))) continue;
|
||||
const failure = await this.#detach(device);
|
||||
if (failure !== undefined) {
|
||||
stillAttached.push(device);
|
||||
detachFailure = failure;
|
||||
}
|
||||
}
|
||||
remainingDevices = stillAttached;
|
||||
}
|
||||
if (remainingDevices.length === 0) detachFailure = undefined;
|
||||
this.#devices = remainingDevices.reverse();
|
||||
await Promise.allSettled(this.#activeDirectoryOperations);
|
||||
await this.#closeMountedDirectory();
|
||||
let detachFailure = await this.#detachOwnedDevices();
|
||||
if (
|
||||
this.#devices.length === 0 &&
|
||||
this.#mountRoot !== undefined &&
|
||||
@@ -242,6 +232,55 @@ export class NativeDmgArtifactReader implements ArtifactReader {
|
||||
);
|
||||
}
|
||||
|
||||
async #detachOwnedDevices(): Promise<unknown> {
|
||||
let detachFailure = await this.#reconcileAttachState();
|
||||
let remainingDevices: string[] = [];
|
||||
for (const device of [...this.#devices].reverse()) {
|
||||
const failure = await this.#detach(device);
|
||||
if (failure !== undefined) {
|
||||
remainingDevices.push(device);
|
||||
detachFailure ??= failure;
|
||||
}
|
||||
}
|
||||
// APFS container detach may briefly leave its backing image listed.
|
||||
for (const delayMs of DETACH_SETTLE_DELAYS_MS) {
|
||||
if (remainingDevices.length === 0) break;
|
||||
await (this.host.delay ?? delay)(delayMs);
|
||||
const stillAttached: string[] = [];
|
||||
for (const device of remainingDevices) {
|
||||
if (!(await this.#isAttached(device))) continue;
|
||||
const failure = await this.#detach(device);
|
||||
if (failure !== undefined) {
|
||||
stillAttached.push(device);
|
||||
detachFailure = failure;
|
||||
}
|
||||
}
|
||||
remainingDevices = stillAttached;
|
||||
}
|
||||
if (remainingDevices.length === 0) detachFailure = undefined;
|
||||
this.#devices = remainingDevices.reverse();
|
||||
return detachFailure;
|
||||
}
|
||||
|
||||
async #closeMountedDirectory(): Promise<void> {
|
||||
if (this.#directory !== undefined) {
|
||||
try {
|
||||
await this.#directory.close();
|
||||
this.#directory = undefined;
|
||||
} catch (cause: unknown) {
|
||||
const mountRoot = this.#mountRoot ?? this.path;
|
||||
throw ArtifactReaderFailure.withCleanup(cause, {
|
||||
reason: `DMG mounted directory remains owned: ${failureMessage(cause)}`,
|
||||
resources: [
|
||||
`DMG mounted directory at ${mountRoot}`,
|
||||
`DMG mount root ${mountRoot}`,
|
||||
...this.#devices.map((device) => `DMG device ${device}`),
|
||||
],
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Detach one device; return the failure only while it remains attached. */
|
||||
async #detach(device: string): Promise<unknown> {
|
||||
try {
|
||||
@@ -294,6 +333,20 @@ export class NativeDmgArtifactReader implements ArtifactReader {
|
||||
throw new ArtifactReaderFailure("unavailable", "DMG reader is closed");
|
||||
}
|
||||
|
||||
async #trackDirectoryOperation<Result>(
|
||||
operation: () => Promise<Result>,
|
||||
): Promise<Result> {
|
||||
if (this.#closed)
|
||||
throw new ArtifactReaderFailure("unavailable", "DMG reader is closed");
|
||||
const running = Promise.resolve().then(operation);
|
||||
this.#activeDirectoryOperations.add(running);
|
||||
try {
|
||||
return await running;
|
||||
} finally {
|
||||
this.#activeDirectoryOperations.delete(running);
|
||||
}
|
||||
}
|
||||
|
||||
async #attach(signal?: AbortSignal): Promise<void> {
|
||||
await runChecked(this.host, ["verify", this.path], signal);
|
||||
this.#provenance.push(command(["verify", this.path], ["read"]));
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
import {
|
||||
lstat,
|
||||
mkdtemp,
|
||||
open,
|
||||
rm,
|
||||
symlink,
|
||||
unlink,
|
||||
writeFile,
|
||||
} from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { expect, it } from "vitest";
|
||||
import { readStableArtifact } from "./readStableArtifact.js";
|
||||
|
||||
const checkChange = async (
|
||||
stage: "before-open" | "after-read" | "symlink-replacement",
|
||||
): Promise<void> => {
|
||||
const root = await mkdtemp(join(tmpdir(), "rea-stable-read-"));
|
||||
const path = join(root, "selected");
|
||||
await writeFile(path, "original evidence");
|
||||
let inspected = 0;
|
||||
try {
|
||||
const reading = readStableArtifact(path, 1024, undefined, {
|
||||
async lstat(selected) {
|
||||
inspected++;
|
||||
if (stage === "after-read" && inspected === 2) await unlink(selected);
|
||||
const stat = await lstat(selected, { bigint: true });
|
||||
if (inspected === 1 && stage !== "after-read") {
|
||||
await unlink(selected);
|
||||
if (stage === "symlink-replacement")
|
||||
await symlink("missing", selected);
|
||||
}
|
||||
return stat;
|
||||
},
|
||||
open,
|
||||
});
|
||||
await expect(reading).rejects.toMatchObject({ reason: "integrity" });
|
||||
} finally {
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
};
|
||||
|
||||
it.each(["before-open", "after-read"] as const)(
|
||||
"retains an actual post-validation %s change as integrity failure",
|
||||
checkChange,
|
||||
);
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"retains a post-validation symlink replacement as an integrity failure",
|
||||
() => checkChange("symlink-replacement"),
|
||||
);
|
||||
|
||||
it("keeps initially missing inputs distinguishable from later changes", async () => {
|
||||
const root = await mkdtemp(join(tmpdir(), "rea-stable-missing-"));
|
||||
try {
|
||||
await expect(
|
||||
readStableArtifact(join(root, "missing"), 1024),
|
||||
).rejects.toMatchObject({ code: "ENOENT" });
|
||||
} finally {
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user