fix(artifacts): retain failed DMG cleanup state and diagnostics (#1103)

This commit is contained in:
Ysopwnable
2026-10-09 01:28:42 +08:00
committed by GitHub
parent c789af04b7
commit 4bb056bbb5
2 changed files with 101 additions and 10 deletions
+17 -6
View File
@@ -128,6 +128,7 @@ export class NativeDmgArtifactReader implements ArtifactReader {
async close(): Promise<void> {
let detachFailure: unknown;
const remainingDevices: string[] = [];
for (const device of [...this.#devices].reverse()) {
try {
await runChecked(this.host, ["detach", device], undefined, {
@@ -138,12 +139,19 @@ export class NativeDmgArtifactReader implements ArtifactReader {
// Detaching a synthesized APFS container also ejects the image that
// backs it, so a later whole disk of the same image may already be
// gone. Only a device that is still attached is a cleanup failure.
if (await this.#isAttached(device)) detachFailure ??= cause;
if (await this.#isAttached(device)) {
remainingDevices.push(device);
detachFailure ??= cause;
}
}
}
this.#devices = [];
if (this.#mountRoot !== undefined)
await rm(this.#mountRoot, { recursive: true, force: true }).catch(
this.#devices = remainingDevices.reverse();
if (this.#devices.length === 0 && this.#mountRoot !== undefined)
await rm(this.#mountRoot, { recursive: true, force: true }).then(
() => {
this.#mountRoot = undefined;
this.#directory = undefined;
},
(cause: unknown) => {
detachFailure ??= cause;
},
@@ -151,7 +159,7 @@ export class NativeDmgArtifactReader implements ArtifactReader {
if (detachFailure !== undefined)
throw new ArtifactReaderFailure(
"unavailable",
"DMG detach or mount-root cleanup failed",
`DMG detach or mount-root cleanup failed for ${JSON.stringify(this.path)}: ${failureMessage(detachFailure)}`,
{ cause: detachFailure },
);
}
@@ -251,7 +259,7 @@ export class NativeDmgArtifactReader implements ArtifactReader {
if (cleanupFailure !== undefined)
throw new ArtifactReaderFailure(
"unavailable",
"DMG attach failed and cleanup could not detach every device",
`DMG attach failed and cleanup could not detach every device: ${failureMessage(cause)}; cleanup: ${failureMessage(cleanupFailure)}`,
{ cause: new AggregateError([cause, cleanupFailure]) },
);
throw cause;
@@ -259,6 +267,9 @@ export class NativeDmgArtifactReader implements ArtifactReader {
}
}
const failureMessage = (cause: unknown): string =>
cause instanceof Error ? cause.message : String(cause);
const runChecked = async (
host: NativeDmgHost,
arguments_: readonly string[],
@@ -1,8 +1,15 @@
import { chmod, mkdir, readFile, realpath, writeFile } from "node:fs/promises";
import {
chmod,
mkdir,
readFile,
realpath,
rm,
writeFile,
} from "node:fs/promises";
import { join } from "node:path";
import { build } from "plist";
import { describe, expect, it } from "vitest";
import { describe, expect, it, onTestFinished } from "vitest";
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
import { execFileOutput } from "../../../../src/process/ExecFileOutput.js";
@@ -456,8 +463,81 @@ describe("APFS disk image detach", () => {
undefined,
host,
);
await expect(reader.close()).rejects.toThrow(
"DMG detach or mount-root cleanup failed",
await expect(reader.close()).rejects.toMatchObject({
reason: "unavailable",
message: expect.stringContaining("No such file or directory"),
});
});
it("retains a failed attachment and its mount root until a later close detaches it", async () => {
let mountRoot: string | undefined;
let busy = true;
let attached = true;
onTestFinished(async () => {
if (mountRoot !== undefined)
await rm(mountRoot, { recursive: true, force: true });
});
const host: NativeDmgHost = {
async run(args) {
if (args[0] === "attach") {
mountRoot = args[args.indexOf("-mountroot") + 1];
if (mountRoot === undefined) throw new Error("missing mount root");
const mountPoint = join(mountRoot, "Fixture");
await mkdir(mountPoint);
await writeFile(join(mountPoint, "hello.txt"), "owned observation");
return {
stdout: build({
"system-entities": [
{ "dev-entry": "/dev/disk7", "mount-point": mountPoint },
],
}),
exitCode: 0,
};
}
if (args[0] === "info")
return {
stdout: build({
images: attached
? [{ "system-entities": [{ "dev-entry": "/dev/disk7" }] }]
: [],
}),
exitCode: 0,
};
if (args[0] === "detach") {
if (busy)
return {
stdout: "",
stderr: "hdiutil: detach failed - Resource busy",
exitCode: 16,
};
attached = false;
}
return { stdout: "", exitCode: 0 };
},
};
const reader = await NativeDmgArtifactReader.create(
"/tmp/image.dmg",
undefined,
host,
);
await expect(reader.close()).rejects.toMatchObject({
reason: "unavailable",
});
if (mountRoot === undefined) throw new Error("missing owned mount root");
expect
.soft(
await readFile(join(mountRoot, "Fixture", "hello.txt"), "utf8").catch(
() => undefined,
),
)
.toBe("owned observation");
busy = false;
await reader.close();
expect.soft(attached).toBe(false);
await expect(
readFile(join(mountRoot, "Fixture", "hello.txt")),
).rejects.toMatchObject({
code: "ENOENT",
});
});
});