fix(artifacts): recheck failed DMG detaches before reporting cleanup failure (#1155)

* fix(artifacts): recheck failed DMG detaches before reporting cleanup failure

NativeDmgArtifactReader.close() detached each device once and, when a
detach failed, asked `hdiutil info` once whether the device was still
attached. On APFS images, detaching the synthesized container ejects the
image disk too. `hdiutil info` can still list that ejected device
briefly, and a just-read volume can report busy. Either way a real
cleanup succeeded but close() reported "DMG detach or mount-root cleanup
failed" and kept the mount root.

macOS CI hit this intermittently in verify:macos-bundle: `hdiutil
detach /dev/disk23` failed with "No such file or directory" (already
ejected) and the device was still counted as attached. The #1103
review also raised the missing recheck.

After the first pass, recheck the remaining devices up to three times
with 250/500/1000 ms settle delays. Drop any that hdiutil no longer
lists, retry the detach of any still listed, and fail only for devices
that stay attached. The host seam gains an optional delay so tests run
without real waits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(artifacts): share DMG detach fixtures to stay under the duplication gate

The new busy-device and recheck cases copied the fake hdiutil host and
the APFS close sequence from neighbouring tests, which pushed jscpd to
its 2.5% threshold in Static checks. Share one single-device host
between the busy and retained-attachment cases and one APFS close
helper between the ejected-device cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822
2026-10-09 10:01:52 +08:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 089bbf58f3
commit 58a91f03d7
2 changed files with 176 additions and 71 deletions
+47 -14
View File
@@ -15,6 +15,12 @@ import {
import { DirectoryArtifactReader } from "./DirectoryArtifactReader.js";
import { execFileOutput } from "../process/ExecFileOutput.js";
const DETACH_TIMEOUT_MS = 120_000;
/**
* Waits before rechecking devices whose detach failed. A related device's
* later detach can eject one, `hdiutil info` can still list an ejected device
* briefly, and a volume can report busy just after it was read.
*/
const DETACH_SETTLE_DELAYS_MS = [250, 500, 1000] as const;
const attachOutputSchema = z.object({
"system-entities": z.array(
z.object({
@@ -43,9 +49,13 @@ export interface NativeDmgHost {
readonly exitCode: number;
readonly cause?: unknown;
}>;
/** Wait between detach rechecks; tests may resolve immediately. */
delay?(milliseconds: number): Promise<void>;
}
const systemHost: NativeDmgHost = {
delay: (milliseconds) =>
new Promise((resolve) => setTimeout(resolve, milliseconds)),
async run(arguments_, signal, options) {
try {
const { stdout, stderr } = await execFileOutput(
@@ -128,23 +138,33 @@ export class NativeDmgArtifactReader implements ArtifactReader {
async close(): Promise<void> {
let detachFailure: unknown;
const remainingDevices: string[] = [];
let remainingDevices: string[] = [];
for (const device of [...this.#devices].reverse()) {
try {
await runChecked(this.host, ["detach", device], undefined, {
timeoutMs: DETACH_TIMEOUT_MS,
});
this.#provenance.push(command(["detach", device], ["mount"]));
} catch (cause: unknown) {
// 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)) {
remainingDevices.push(device);
detachFailure ??= cause;
}
const failure = await this.#detach(device);
if (failure !== undefined) {
remainingDevices.push(device);
detachFailure ??= failure;
}
}
// Detaching a synthesized APFS container also ejects the image that backs
// it, so a device whose own detach failed may be gone by the end of the
// pass, and hdiutil can list an ejected device or report busy briefly.
// Only a device that stays attached through every recheck is a failure.
for (const delayMs of DETACH_SETTLE_DELAYS_MS) {
if (remainingDevices.length === 0) break;
await (this.host.delay ?? systemHost.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();
if (this.#devices.length === 0 && this.#mountRoot !== undefined)
await rm(this.#mountRoot, { recursive: true, force: true }).then(
@@ -164,6 +184,19 @@ export class NativeDmgArtifactReader implements ArtifactReader {
);
}
/** Detach one device; return the failure only while it remains attached. */
async #detach(device: string): Promise<unknown> {
try {
await runChecked(this.host, ["detach", device], undefined, {
timeoutMs: DETACH_TIMEOUT_MS,
});
this.#provenance.push(command(["detach", device], ["mount"]));
return undefined;
} catch (cause: unknown) {
return (await this.#isAttached(device)) ? cause : undefined;
}
}
/**
* Report whether hdiutil still lists a device; unknown state counts as
* attached. Like detach, this cleanup query runs after the inventory