Skip to content

metal: a stream with a residency set drains its queue before the set is torn down - #915

Closed
midagedev wants to merge 2 commits into
mainfrom
mac-gpupanic
Closed

midagedev wants to merge 2 commits into
mainfrom
mac-gpupanic

Conversation

@midagedev

Copy link
Copy Markdown
Owner

What and why

On two M1 Pro Macs and two macOS builds, the Metal e2e gate caused four kernel panics. All four have the same stack:
IOGPUCommandQueue::set_resource_groups → IOGPUDevice::lockResourceNamespace on a NULL object (far 0x10). The
symbols were resolved from the on-disk kernelcache.

MetalStream dropped its residency set while command buffers were still in committed.pending. The drop ran
endResidency and removeResidencySet and released the buffers the set retained. Now Drop for MetalStream waits for
those buffers, bounded by SYNC_TIMEOUT. If they do not finish in time, it leaks the set with a named stderr line.

Also:

  • BLOOMERY_METAL_BREADCRUMB=<file> (a lever row) appends and fsyncs one line before each queue, set, event, commit
    and wait call class, and at each e2e clause.
  • metal_residency_repro is a bounded loop of the teardown pattern.

Release notes: Fixed — a Metal session torn down with GPU work in flight could panic macOS.

Code shape

  • No second copy. The drain sits at the one owner of both the pending buffers and the set (MetalStream).
  • Model code holds only architecture facts. No model file changed.
  • Dead code is gone.
  • No silent failure. A drain that times out is a named stderr line. A breadcrumb file that cannot be opened is
    a named stderr line.
  • Comments state what is true now.
  • No relaxed gate.
  • cargo fmt and cargo clippy are clean on what I touched.

Duplication found or removed: none.

What I ran

  • just remote-static origin/main: mac-static: ok (680 s).

  • tools/gate-batch.sh --ledger --tier fixture with gate-levers (the only gate affected selects):
    DONE total=1 red=0.

  • cargo test -p bloomery-metal --lib: 94 passed. cargo clippy -p bloomery-metal -p bloomery-levers --all-targets:
    0 warnings.

  • The fixed tree, with the dense 27B branch merged in, on the second Mac (the Mac that panicked on the 10-28 run):

    • the same order as the panicked run (three rows, qwen35moe --mutants-all, qwen38d --mutants-all): no panic;
    • the breadcrumb shows 72 of 185 teardowns found command buffers pending (teardown drain N pending, N ≥ 1).
  • The fixed tree on the lead Mac (main only), five rounds of the default row and its --mutants-all: 10 of 10 PASS, no panic; 213 of 650 teardowns found buffers pending.

  • FAIL-first on the tree without the drain did not reproduce the panic:

    • 15,500 iterations of metal_residency_repro;
    • 610 runs of --row qwen38d --mutant dense_slot_one.

    The panic needs more state than one child run. So the evidence for this fix is the symbolicated stack, the pending
    count, and Apple's contract: a resource may be removed from a set only when "the GPU no longer needs" it
    (https://developer.apple.com/documentation/metal/mtlresidencyset).

AI use: written and checked with Claude Code.

🤖 Generated with Claude Code

https://claude.ai/code/session_01X1hsQfNpqr2jqmpYe8SFXd

midagedev and others added 2 commits October 28, 2026 20:05
…is torn down

The four kernel panics in the Metal e2e gate are one IOGPUFamily path:
IOGPUCommandQueue::set_resource_groups -> IOGPUDevice::lockResourceNamespace
on a null object. MetalStream dropped its residency set (endResidency,
removeResidencySet, the retained buffers released) with command buffers
still in committed.pending. MetalStream's Drop now waits for them, bounded by
SYNC_TIMEOUT, and leaks the set with a named stderr line if they do not
finish.

Also: BLOOMERY_METAL_BREADCRUMB, a crash breadcrumb that appends and fsyncs
one line before each queue, set, event, commit and wait call class and at
each e2e gate clause; and metal_residency_repro, the bounded loop of the
teardown pattern for the fix's FAIL-first (run only on a Mac the user names).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01X1hsQfNpqr2jqmpYe8SFXd
@midagedev midagedev added the needs-gpu Changes code a card must check, and carries no gate run on a card label Oct 28, 2026
@github-actions github-actions Bot added the host Host crates: qdot, threads, engram, engram-lab, runtime, sampler, levers, decision, vision, refset label Oct 28, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MUST_FIX: the repro code and its comments state a kernel fault that the PR body says no run observed, and no test guards the drain.

Must fix

1. The repro claims a fault it did not produce (kind B, misleading comment and unneeded element)

residency.rs:276 says "None faults, Sync survives". metal_residency_repro.rs:9 says the teardown "frees the buffer under an in-flight command buffer, the driver walks a freed resource, and the kernel faults". residency.rs:270 names IOGPUDevice::getResource as the path. The PR body says 15,500 iterations of this repro on the tree without the drain did not panic. So the comments state as fact what the author measured to be false, and the repro does not fail on the bug. A reader will trust "None faults" and conclude the drain is proven.
Cost: the repro adds a [[bin]], a public ReproWait enum with 3 arms, and a public repro fn in the library, and it guards nothing. Settle asks a question whose answer is not recorded. Removing it now is cheaper than keeping a probe that reads as proof.
Fix: delete crates/metal/src/bin/metal_residency_repro.rs, the [[bin]] metal_residency_repro block in crates/metal/Cargo.toml, and ReproWait and repro in crates/metal/src/residency.rs (with the MetalDevice import). If you keep it, state in the doc what was measured (iterations, result: no panic) and drop "faults" and the driver symbol names.

Non-blocking

⚠ No test turns red when the drain is removed: stream.rs:706. Mutant: delete the sync_for call in Drop. The unit tests (94) and gate-levers stay green, and the body says FAIL-first did not reproduce. A fix PR needs layer 2 or a reason; the body gives the reason (the panic needs more state). Name it as the open gap, or add a macOS test that commits a buffer, drops the stream and asserts pending was empty at the release (the drain's own observable).

⚠ Two errors are dropped without a name: stream.rs:730 let _ = self.sync_for(..) discards the failure of a command buffer that ended in error, and the comment says it "is read elsewhere". sync_for removes an errored buffer from pending and reports it once, so if the drop is the first to see it, nothing reads it after. stream.rs:433 discards a failed writeln or sync_data, so a full disk leaves a stale last line that names the wrong call after a panic. Print one stderr line in each case (the open failure already does).

also 1 minor: the Drop doc and the lever doc state the driver mechanism as established; the evidence is one symbolicated stack and Apple's contract.

Checked

  • Duplicates: 9 new items checked (mark, breadcrumb, breadcrumb_clause, BREADCRUMB, ReproWait, repro, Drop for MetalStream, lever row, bin). No existing owner found. Dryer pairs: both are the 4-line MetalDevice::open error arm in the repro bin; short copies, and finding 1 removes the bin. Not a finding.
  • Complexity: crapper flagged nothing.
  • Silent failure: 5 sites followed (let _ at stream.rs:433, :435, :730; open failure of the breadcrumb is named; timeout leak is named).
  • Performance: mark is added to the commit, record, wait and submit paths. Unset, it is one OnceLock load and a branch per command buffer, and format_args! does not allocate. No finding.
  • Tests: no test added or changed; mutation above.
  • Gates: no pin moved. Lever row, levers-direct.txt and the in-place read agree.
  • Escape catalog: no pattern matched (no shell changed).

Notes

  • Evidence is from a Mac. No nvidia-smi run is needed for these crates.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@github-actions github-actions Bot removed the needs-gpu Changes code a card must check, and carries no gate run on a card label Oct 28, 2026
@midagedev

Copy link
Copy Markdown
Owner Author

Superseded by #916: the same two commits rebased on main (the lever registry conflict resolved by keeping both sides' rows).

@midagedev midagedev closed this Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

host Host crates: qdot, threads, engram, engram-lab, runtime, sampler, levers, decision, vision, refset

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant