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[],