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 });
|
||||
}
|
||||
});
|
||||
@@ -1,4 +1,11 @@
|
||||
import { rename, symlink, writeFile } from "node:fs/promises";
|
||||
import {
|
||||
lstat,
|
||||
open,
|
||||
rename,
|
||||
symlink,
|
||||
unlink,
|
||||
writeFile,
|
||||
} from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { expect, it } from "vitest";
|
||||
import { readStableArtifact } from "../../../src/artifacts/readStableArtifact.js";
|
||||
@@ -28,3 +35,43 @@ it("reads exact binary and empty bytes and rejects symlink/size/cancellation bou
|
||||
controller.signal.reason,
|
||||
);
|
||||
});
|
||||
|
||||
const checkChange = async (
|
||||
stage: "before-open" | "after-read" | "symlink-replacement",
|
||||
): Promise<void> => {
|
||||
const root = await createTestTempDirectory("rea-stable-read-");
|
||||
const path = join(root, "selected");
|
||||
await writeFile(path, "original evidence");
|
||||
let inspected = 0;
|
||||
const reading = readStableArtifact(path, 1024, undefined, {
|
||||
async lstat(selected) {
|
||||
inspected++;
|
||||
if (stage === "after-read" && inspected === 2) await unlink(selected);
|
||||
const metadata = await lstat(selected, { bigint: true });
|
||||
if (inspected === 1 && stage !== "after-read") {
|
||||
await unlink(selected);
|
||||
if (stage === "symlink-replacement") await symlink("missing", selected);
|
||||
}
|
||||
return metadata;
|
||||
},
|
||||
open,
|
||||
});
|
||||
await expect(reading).rejects.toMatchObject({ reason: "integrity" });
|
||||
};
|
||||
|
||||
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 createTestTempDirectory("rea-stable-missing-");
|
||||
await expect(
|
||||
readStableArtifact(join(root, "missing"), 1024),
|
||||
).rejects.toMatchObject({ code: "ENOENT" });
|
||||
});
|
||||
|
||||
@@ -7,14 +7,19 @@ import {
|
||||
writeFile,
|
||||
} from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import type { Readable } from "node:stream";
|
||||
|
||||
import { build } from "plist";
|
||||
import { describe, expect, it, onTestFinished } from "vitest";
|
||||
import { describe, expect, it, onTestFinished, vi } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
||||
import { execFileOutput } from "../../../../src/process/ExecFileOutput.js";
|
||||
import { ArtifactReaderFailure } from "../../../../src/artifacts/ArtifactReader.js";
|
||||
import {
|
||||
ArtifactReaderFailure,
|
||||
type ArtifactEntry,
|
||||
} from "../../../../src/artifacts/ArtifactReader.js";
|
||||
import { ArtifactResourceScope } from "../../../../src/artifacts/ArtifactResourceScope.js";
|
||||
import { DirectoryArtifactReader } from "../../../../src/artifacts/DirectoryArtifactReader.js";
|
||||
import {
|
||||
NativeDmgArtifactReader,
|
||||
type NativeDmgHost,
|
||||
@@ -376,6 +381,186 @@ describe("native DMG lazy initialization", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("native DMG directory ownership", () => {
|
||||
it("closes unread member streams and suspended traversal handles before detach", async () => {
|
||||
const calls: string[][] = [];
|
||||
const mount = createMountFixture();
|
||||
let attached = false;
|
||||
let mountPoint: string | undefined;
|
||||
let unread: Readable | undefined;
|
||||
let unreadHandleRejectedAtDetach = false;
|
||||
const host: NativeDmgHost = {
|
||||
async run(arguments_) {
|
||||
calls.push([...arguments_]);
|
||||
if (arguments_[0] === "info")
|
||||
return {
|
||||
stdout:
|
||||
!attached || mountPoint === undefined
|
||||
? emptyInfoPlist
|
||||
: infoPlist("/tmp/image.dmg", [
|
||||
{
|
||||
"dev-entry": "/dev/disk-unread",
|
||||
"mount-point": mountPoint,
|
||||
},
|
||||
]),
|
||||
exitCode: 0,
|
||||
};
|
||||
if (arguments_[0] === "detach") {
|
||||
if (unread !== undefined) {
|
||||
try {
|
||||
await unread[Symbol.asyncIterator]().next();
|
||||
} catch {
|
||||
unreadHandleRejectedAtDetach = true;
|
||||
}
|
||||
}
|
||||
attached = false;
|
||||
return { stdout: "", exitCode: 0 };
|
||||
}
|
||||
if (arguments_[0] !== "attach") return { stdout: "", exitCode: 0 };
|
||||
({ mountPoint } = await mount.createVolume(arguments_, "unread bytes"));
|
||||
attached = true;
|
||||
return {
|
||||
stdout: attachPlist([
|
||||
{ "dev-entry": "/dev/disk-unread", "mount-point": mountPoint },
|
||||
]),
|
||||
exitCode: 0,
|
||||
};
|
||||
},
|
||||
};
|
||||
const reader = await createAttachedReader(
|
||||
"/tmp/image.dmg",
|
||||
process.env,
|
||||
undefined,
|
||||
host,
|
||||
);
|
||||
let fileEntry: ArtifactEntry | undefined;
|
||||
for await (const entry of reader.entries())
|
||||
if (entry.path.endsWith("/hello.txt")) fileEntry = entry;
|
||||
if (fileEntry === undefined) throw new Error("expected hello.txt");
|
||||
unread = await reader.open(fileEntry);
|
||||
const iterator = reader.entries()[Symbol.asyncIterator]();
|
||||
const first = await iterator.next();
|
||||
if (first.done) throw new Error("expected a mounted directory entry");
|
||||
await reader.close();
|
||||
expect(calls.filter(([operation]) => operation === "detach")).toEqual([
|
||||
["detach", "/dev/disk-unread"],
|
||||
]);
|
||||
expect(unreadHandleRejectedAtDetach).toBe(true);
|
||||
await expect(realpath(mount.root() ?? "")).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
expect((await iterator.next()).done).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("native DMG traversal shutdown", () => {
|
||||
it("waits for a pending directory advance before detaching", async () => {
|
||||
const { host, state } = createOmittedInventoryHost();
|
||||
state.inventoryMode = "listed";
|
||||
state.detachAttempts = 4;
|
||||
const originalEntries = DirectoryArtifactReader.prototype.entries;
|
||||
let gateAdvance = false;
|
||||
let markAdvanceStarted = (): void => {};
|
||||
const advanceStarted = new Promise<void>((resolve) => {
|
||||
markAdvanceStarted = resolve;
|
||||
});
|
||||
let releaseAdvance = (): void => {};
|
||||
const advanceGate = new Promise<void>((resolve) => {
|
||||
releaseAdvance = resolve;
|
||||
});
|
||||
const entriesSpy = vi
|
||||
.spyOn(DirectoryArtifactReader.prototype, "entries")
|
||||
.mockImplementation(async function* (
|
||||
this: DirectoryArtifactReader,
|
||||
signal?: AbortSignal,
|
||||
descendInto?: (path: string) => boolean,
|
||||
) {
|
||||
const iterator = originalEntries
|
||||
.call(this, signal, descendInto)
|
||||
[Symbol.asyncIterator]();
|
||||
try {
|
||||
for (;;) {
|
||||
const next = await iterator.next();
|
||||
if (next.done) return;
|
||||
if (gateAdvance) {
|
||||
markAdvanceStarted();
|
||||
await advanceGate;
|
||||
}
|
||||
yield next.value;
|
||||
}
|
||||
} finally {
|
||||
await iterator.return?.();
|
||||
}
|
||||
});
|
||||
try {
|
||||
const reader = await createAttachedReader(
|
||||
"/tmp/image.dmg",
|
||||
process.env,
|
||||
undefined,
|
||||
host,
|
||||
);
|
||||
gateAdvance = true;
|
||||
const iterator = reader.entries()[Symbol.asyncIterator]();
|
||||
const advancing = iterator.next();
|
||||
await advanceStarted;
|
||||
const closing = reader.close();
|
||||
await Promise.resolve();
|
||||
expect(state.detachAttempts).toBe(4);
|
||||
releaseAdvance();
|
||||
expect((await advancing).done).toBe(true);
|
||||
await closing;
|
||||
expect(state.detachAttempts).toBe(5);
|
||||
await expect(realpath(state.mountRoot ?? "")).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
} finally {
|
||||
entriesSpy.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("native DMG directory close recovery", () => {
|
||||
it("retains a mounted directory after its close fails and retries before detach", async () => {
|
||||
const { host, state } = createOmittedInventoryHost();
|
||||
state.inventoryMode = "listed";
|
||||
state.detachAttempts = 4;
|
||||
const closeDirectory = vi
|
||||
.spyOn(DirectoryArtifactReader.prototype, "close")
|
||||
.mockImplementationOnce(async () => {
|
||||
throw new Error("injected directory close failure");
|
||||
});
|
||||
try {
|
||||
const reader = await createAttachedReader(
|
||||
"/tmp/image.dmg",
|
||||
process.env,
|
||||
undefined,
|
||||
host,
|
||||
);
|
||||
const failure = await reader.close().catch((cause: unknown) => cause);
|
||||
expect(failure).toMatchObject({
|
||||
reason: "io",
|
||||
cleanup: {
|
||||
resources: expect.arrayContaining([
|
||||
expect.stringMatching(/^DMG mounted directory at /u),
|
||||
expect.stringMatching(/^DMG mount root /u),
|
||||
]),
|
||||
},
|
||||
});
|
||||
expect(state.detachAttempts).toBe(4);
|
||||
expect(await realpath(state.mountRoot ?? "")).toBe(state.mountRoot);
|
||||
|
||||
await reader.close();
|
||||
expect(closeDirectory).toHaveBeenCalledTimes(2);
|
||||
expect(state.detachAttempts).toBe(5);
|
||||
await expect(realpath(state.mountRoot ?? "")).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
} finally {
|
||||
closeDirectory.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("native DMG artifact reader", () => {
|
||||
it("uses plist attachment metadata and detaches returned devices", async () => {
|
||||
const calls: string[][] = [];
|
||||
|
||||
Reference in New Issue
Block a user