fix(artifacts): retain failed DMG cleanup state and diagnostics (#1103)
This commit is contained in:
@@ -128,6 +128,7 @@ export class NativeDmgArtifactReader implements ArtifactReader {
|
|||||||
|
|
||||||
async close(): Promise<void> {
|
async close(): Promise<void> {
|
||||||
let detachFailure: unknown;
|
let detachFailure: unknown;
|
||||||
|
const remainingDevices: string[] = [];
|
||||||
for (const device of [...this.#devices].reverse()) {
|
for (const device of [...this.#devices].reverse()) {
|
||||||
try {
|
try {
|
||||||
await runChecked(this.host, ["detach", device], undefined, {
|
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
|
// Detaching a synthesized APFS container also ejects the image that
|
||||||
// backs it, so a later whole disk of the same image may already be
|
// 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.
|
// 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 = [];
|
this.#devices = remainingDevices.reverse();
|
||||||
if (this.#mountRoot !== undefined)
|
if (this.#devices.length === 0 && this.#mountRoot !== undefined)
|
||||||
await rm(this.#mountRoot, { recursive: true, force: true }).catch(
|
await rm(this.#mountRoot, { recursive: true, force: true }).then(
|
||||||
|
() => {
|
||||||
|
this.#mountRoot = undefined;
|
||||||
|
this.#directory = undefined;
|
||||||
|
},
|
||||||
(cause: unknown) => {
|
(cause: unknown) => {
|
||||||
detachFailure ??= cause;
|
detachFailure ??= cause;
|
||||||
},
|
},
|
||||||
@@ -151,7 +159,7 @@ export class NativeDmgArtifactReader implements ArtifactReader {
|
|||||||
if (detachFailure !== undefined)
|
if (detachFailure !== undefined)
|
||||||
throw new ArtifactReaderFailure(
|
throw new ArtifactReaderFailure(
|
||||||
"unavailable",
|
"unavailable",
|
||||||
"DMG detach or mount-root cleanup failed",
|
`DMG detach or mount-root cleanup failed for ${JSON.stringify(this.path)}: ${failureMessage(detachFailure)}`,
|
||||||
{ cause: detachFailure },
|
{ cause: detachFailure },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -251,7 +259,7 @@ export class NativeDmgArtifactReader implements ArtifactReader {
|
|||||||
if (cleanupFailure !== undefined)
|
if (cleanupFailure !== undefined)
|
||||||
throw new ArtifactReaderFailure(
|
throw new ArtifactReaderFailure(
|
||||||
"unavailable",
|
"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]) },
|
{ cause: new AggregateError([cause, cleanupFailure]) },
|
||||||
);
|
);
|
||||||
throw cause;
|
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 (
|
const runChecked = async (
|
||||||
host: NativeDmgHost,
|
host: NativeDmgHost,
|
||||||
arguments_: readonly string[],
|
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 { join } from "node:path";
|
||||||
|
|
||||||
import { build } from "plist";
|
import { build } from "plist";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it, onTestFinished } from "vitest";
|
||||||
|
|
||||||
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
||||||
import { execFileOutput } from "../../../../src/process/ExecFileOutput.js";
|
import { execFileOutput } from "../../../../src/process/ExecFileOutput.js";
|
||||||
@@ -456,8 +463,81 @@ describe("APFS disk image detach", () => {
|
|||||||
undefined,
|
undefined,
|
||||||
host,
|
host,
|
||||||
);
|
);
|
||||||
await expect(reader.close()).rejects.toThrow(
|
await expect(reader.close()).rejects.toMatchObject({
|
||||||
"DMG detach or mount-root cleanup failed",
|
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",
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user