Skip to content

fix(macos): DMG packaging leaks an attached disk image when hdiutil attach fails on a busy host - #173686

Merged
steipete merged 1 commit into
mainfrom
steipete/dmg-attach-leak
Oct 16, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/dmg-attach-leak

Conversation

@steipete

Copy link
Copy Markdown
Contributor

Related: #172903

What Problem This Solves

Fixes: macOS DMG packaging leaves disk images attached on the build host when hdiutil attach fails on a busy machine, and create-dmg.sh then deletes the image file out from under the still-attached devices.

User Impact

Release and local DMG packaging no longer accumulate orphaned /dev/diskN attachments after a transient attach failure. If an image cannot be detached, packaging fails, keeps the files, and prints the exact hdiutil detach /dev/diskN command to run.

Why This Change Was Made

On a busy host, hdiutil attach ... -mountpoint M IMAGE can fail with Resource temporarily unavailable after it has created the image's devices (for example /dev/disk8 plus the synthesized APFS container /dev/disk11) but before mounting. Both scripts only detached when the attach had succeeded, so this state was never cleaned up:

  • scripts/create-dmg.sh skipped detach and ran rm -rf on the temp root, deleting the RW image while it stayed attached.
  • scripts/package-mac-dist.sh (audit_retained_dmg_async_frames, notarization resume) left the retained release DMG attached.

This is the same leak class #172903 fixes for the Peekaboo snapshot image. The new shared scripts/lib/mac-disk-image.sh uses that approach: find every attachment of the image in hdiutil info -plist by real path, detach its whole-disk device with hdiutil detach -quiet (never -force), re-query to confirm, and fail closed if it is still attached. create-dmg.sh checks before deleting its temp root on every exit and preserves it if the image stays attached; the DMG audit runs the device cleanup when its attach did not complete. mac-swift-build.sh can adopt the helper once #172903 lands.

Release-note context: macOS packaging (scripts/create-dmg.sh, scripts/package-mac-dist.sh --resume-notarization) detaches disk images by device after a failed attach instead of leaking them; maintainer/release tooling only, no app behavior change.

Evidence

Real hdiutil on macOS 27 (hdiutil wrapped so attach runs attach -nomount and then fails with the EAGAIN message, reproducing the leaked state exactly; every other command is the real /usr/bin/hdiutil):

  • create-dmg.sh on main: exit 35, temp root deleted, hdiutil info still lists the deleted image-rw.dmg attached as /dev/disk8, /dev/disk8s1, /dev/disk11, /dev/disk11s1.
  • create-dmg.sh with this change: exit 35, temp root deleted, no attachment left.
  • audit_retained_dmg_async_frames on main: exit 35, release DMG still attached as /dev/disk8 + /dev/disk11. With this change: exit 35, nothing attached.
  • The new helper also detached both leaked main-run images (one whose file was already deleted), so the host was left clean.

Focused tests (pnpm test <file> --maxWorkers=1):

  • test/scripts/create-dmg.test.ts: 16 passed. The new failed-attach cases (detach succeeds / detach fails) fail against main's script.
  • test/scripts/package-mac-dist.test.ts: 29 passed. The new failed-audit-attach cases (detach succeeds / detach fails) fail against main's script.
  • test/scripts/build-and-run-mac.test.ts macOS Bash selection: 137 passed (the helper is pinned to /bin/bash and scanned for Bash 3.2 compatibility).
  • node scripts/run-tsgo-core-test-shards.mjs root, oxfmt --check and oxlint on the touched tests, and /bin/bash -n on the touched scripts all pass.

Test cost: the two new create-dmg cases run about 0.6 s each. The two new package-mac-dist cases run about 4–5 s each, measured on a heavily loaded host (load average around 200), because they go through the real --resume-notarization entry point with a built checkpoint; the existing checkpoint cases in that file have the same shape.

…ttach fails on a busy host

On a busy host, hdiutil attach can fail with EAGAIN after creating the
image devices but before mounting. create-dmg.sh then skipped detach and
deleted the RW image while it stayed attached, and the package-mac-dist
DMG audit left the retained release DMG attached.

Add scripts/lib/mac-disk-image.sh, which finds every attachment of an
image in hdiutil info by real path, detaches its whole-disk device
without -force, and fails closed if it stays attached. create-dmg.sh
checks before deleting its temp root and preserves it when the image is
still attached; the DMG audit runs the device cleanup when its attach
did not complete.
@clawsweeper

clawsweeper Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@steipete
steipete marked this pull request as ready for review October 16, 2026 02:58
@openclaw-barnacle openclaw-barnacle Bot added the scripts Repository scripts label Oct 16, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T03:01:43.099016Z b5a4110 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 16, 2026
@clawsweeper

clawsweeper Bot commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR cleans up disk-image devices left by failed macOS DMG attachment and preserves backing files when detachment fails.

Example: hdiutil creates /dev/disk8 and synthesized /dev/disk11, then exits 35 before mounting image-rw.dmg.

  • Before: Packaging deletes the temporary image while its devices remain attached.
  • After: Packaging detaches the image's whole disk, verifies removal, and preserves the files with a recovery command if cleanup fails.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, useful repair supported by native behavior results, boundary regression coverage, and green exact-head CI.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The PR records native macOS 27 execution of create-dmg.sh and the retained-DMG audit with real hdiutil: injected attach failure leaves devices on main and none after the fix; regression cases cover failed detachment and file retention.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: A failed DMG attach leaves disk devices attached and can delete their backing image, requiring manual host cleanup.
Reason: The maintainer-authored change addresses a demonstrated resource leak without introducing configuration, app behavior, or persistence changes.

Merge readiness

✅ Ready for maintainer review

Keep this PR open: it fixes a concrete packaging leak still present on current main, with no actionable correctness or security finding.

Priority: P2
Reviewed head: b5a411053760317314a8164881e14d359ecbbce4

Before merge

None.

Findings

None.

Agent review details

How this fits together

The macOS packaging scripts turn app bundles and retained notarization artifacts into distributable DMGs; their exit handlers own temporary mounts, image devices, and backing-file cleanup.

flowchart TD
  A[App bundle or retained DMG] --> B[Packaging script]
  B --> C[hdiutil attach]
  C --> D[Failed attachment]
  D --> E[Shared image cleanup]
  E --> F[Discover and detach whole disk]
  F --> G[Verify removal or preserve files]
Loading

Technical review

Best possible solution:

Use the shared image-device cleanup in both failed-attachment paths while retaining the existing successful-mount cleanup behavior.

Do we have a high-confidence way to reproduce the issue?

Current-main source exposes the failed-attach cleanup gap, and the PR records matching native macOS reproduction; this read-only review ran no target code.

Is this the best way to solve the issue?

The change repairs attachment ownership directly by matching canonical image paths, detaching the owning whole disk, and checking the resulting state before deleting backing files.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 680e6b5e329b.

Provenance checked

  • Run-private DMG cleanup keeps the original intent (e94b666: Isolate each run's image and mount and preserve files when detachment fails.)
  • Vanished-volume and device-node cleanup keeps the original intent (71875bf: Avoid false packaging failures when a busy volume disappears and retain device-node fallback.)
  • Retained-DMG frame auditing keeps the original intent (55edab0: Audit retained release artifacts before resumed notarization to reject unsafe ARM64 async frames.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

  • Verified introduction: The pinned introduction adds shared device discovery and cleanup to both affected packaging exit handlers. (scripts/lib/mac-disk-image.sh:8, b5a411053760)
  • Current main retains the defect: Current main still relies on successful attachment before mount cleanup; the affected production files have no changes from the introduction base to fetched main. (scripts/package-mac-dist.sh:241, 680e6b5e329b)
  • Native behavior evidence: The complete PR body records macOS 27 runs using real hdiutil with attach fault injection: main leaked devices, while the changed create-dmg script and retained-DMG audit left no attachment. (b5a411053760)
  • Related work covers a different caller: fix(macos): packaging leaks an attached Peekaboo snapshot image when attach fails on a busy host #172903 addresses the same failed-attachment leak class in Peekaboo snapshot cleanup; it does not repair these DMG packaging callers and independently documents whole-disk detachment releasing the synthesized APFS container. (scripts/lib/mac-swift-build.sh, 838d81d3fea0)
  • Existing cleanup intent preserved: Earlier cleanup work established run-private image ownership and file retention after failed detachment; the added image-device check extends that protection to unmounted attachments. (scripts/create-dmg.sh:147, e94b666e4541)
  • Exact-head CI: The reviewed head has a successful openclaw/ci-gate and successful aggregate commit statuses; paginated checks show no failure, pending, timeout, or action-required result. (b5a411053760)

Likely related people:

  • Peter Steinberger: Raw commit 55edab0 adds scripts/package-mac-dist.sh:220 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 55edab05a12d; files: scripts/package-mac-dist.sh)
  • Vincent Koc: Raw commit e94b666 adds scripts/create-dmg.sh:75 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: e94b666e4541; files: scripts/create-dmg.sh)

PR surface

Tests +126, Other +60. Total +186 across 6 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 0 0 0 0
Tests 3 130 4 +126
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 3 61 1 +60
Total 6 191 5 +186

Labels

Label changes:

  • add P2: Failed attachment can leak disk devices on macOS packaging hosts; the impact is limited to developer and release tooling.
  • add proof: sufficient: Contributor real behavior proof is sufficient.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P2: Failed attachment can leak disk devices on macOS packaging hosts; the impact is limited to developer and release tooling.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed October 15, 2026, 11:14 PM ET / October 16, 2026, 03:14 UTC.

@steipete
steipete merged commit 2e6caad into main Oct 16, 2026
168 of 170 checks passed
@steipete
steipete deleted the steipete/dmg-attach-leak branch October 16, 2026 04:06
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 16, 2026
…ttach fails on a busy host (openclaw#173686)

On a busy host, hdiutil attach can fail with EAGAIN after creating the
image devices but before mounting. create-dmg.sh then skipped detach and
deleted the RW image while it stayed attached, and the package-mac-dist
DMG audit left the retained release DMG attached.

Add scripts/lib/mac-disk-image.sh, which finds every attachment of an
image in hdiutil info by real path, detaches its whole-disk device
without -force, and fails closed if it stays attached. create-dmg.sh
checks before deleting its temp root and preserves it when the image is
still attached; the DMG audit runs the device cleanup when its attach
did not complete.
steipete added a commit that referenced this pull request Oct 16, 2026
…oo snapshots (#173934)

mac-swift-build.sh carried a private copy of the attachment lookup and
fail-closed detach that scripts/lib/mac-disk-image.sh already provides
(#173686). Source the shared helper next to the worker so both the
sourced packager path and the standalone per-architecture worker resolve
it, keep the snapshot attach EAGAIN retry loop, and call
detach_disk_image between attempts and in cleanup.

Release-note context: no packaging behavior change; a snapshot image
that stays attached now reports the shared wording
"ERROR: disk image <path> is still attached; detach it with:
hdiutil detach /dev/diskN".
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 17, 2026
…oo snapshots (openclaw#173934)

mac-swift-build.sh carried a private copy of the attachment lookup and
fail-closed detach that scripts/lib/mac-disk-image.sh already provides
(openclaw#173686). Source the shared helper next to the worker so both the
sourced packager path and the standalone per-architecture worker resolve
it, keep the snapshot attach EAGAIN retry loop, and call
detach_disk_image between attempts and in cleanup.

Release-note context: no packaging behavior change; a snapshot image
that stays attached now reports the shared wording
"ERROR: disk image <path> is still attached; detach it with:
hdiutil detach /dev/diskN".
hilsbos pushed a commit to hilsbos/openclaw that referenced this pull request Oct 22, 2026
…ttach fails on a busy host (openclaw#173686)

On a busy host, hdiutil attach can fail with EAGAIN after creating the
image devices but before mounting. create-dmg.sh then skipped detach and
deleted the RW image while it stayed attached, and the package-mac-dist
DMG audit left the retained release DMG attached.

Add scripts/lib/mac-disk-image.sh, which finds every attachment of an
image in hdiutil info by real path, detaches its whole-disk device
without -force, and fails closed if it stays attached. create-dmg.sh
checks before deleting its temp root and preserves it when the image is
still attached; the DMG audit runs the device cleanup when its attach
did not complete.

(cherry picked from commit 2e6caad)
Co-authored-by: RomneyDa <[email protected]>
RomneyDa pushed a commit that referenced this pull request Oct 27, 2026
…ttach fails on a busy host (#173686)

On a busy host, hdiutil attach can fail with EAGAIN after creating the
image devices but before mounting. create-dmg.sh then skipped detach and
deleted the RW image while it stayed attached, and the package-mac-dist
DMG audit left the retained release DMG attached.

Add scripts/lib/mac-disk-image.sh, which finds every attachment of an
image in hdiutil info by real path, detaches its whole-disk device
without -force, and fails closed if it stays attached. create-dmg.sh
checks before deleting its temp root and preserves it when the image is
still attached; the DMG audit runs the device cleanup when its attach
did not complete.

(cherry picked from commit 2e6caad)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. scripts Repository scripts size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant