metal, gpu: recover swept queue followers and isolate piece fences on GPU timeout #551

Open
github-bot wants to merge 1 commit from github-bot/gh-547 into main AGit
Owner

@⁠Sheffy007 on GitHub · 2026-11-28 02:57 UTC · original
Sheffy007:fix/gpu-timeout-cascade-recovery → main · +272 −75 · 5 files · 1 commits · open

Summary

Addresses the GPU timeout / residency-order cascade analyzed in #408 (and #525):

  1. Per-piece MTLFence & failed next_piece signal refund (crates/victory-metal/src/gfx.rs): Give each split-submission piece (VICTORY_PASSES_PER_BUFFER) its own intra-piece MTLFence (Order) while keeping inter-piece ordering on piece_event (MTLSharedEvent), so a timed-out earlier piece never leaves the next piece wedged on an unsignalled GPU-only MTLFence. If next_piece fails after queue.3 was incremented, attach (queue.2, queue.3) and the submission's fence signals to the last committed piece's completion refund (attach_refund_and_events).
  2. Fast recovery of swept queue followers ahead of the title's 30 s watchdog (crates/victory-metal/src/gfx.rs, crates/victory-gpu/src/native/glue.rs):
    • Flag HAD_FAILURE on landed(..., true) so glue::refresh checks recover_lost() immediately (and every 200 ms rather than behind the 5 s warning log throttle) and calls t.pump() as soon as lost work is recovered.
    • Recover unreported earlier serials (serial < failed_serial) on a failed FIFO queue after 0.5 s, sibling followers already committed on that queue when the failure/reset hit (since <= failed_at) after 3 s (leaving fresh post-failure work untouched), and any unreported buffer on any queue after 15 s — well ahead of the title's 30 s fatal watchdog (0x1423cf683 -> 0x1455e56f8).
    • Always include lagging order_state() queues in "work held for a wait that does not hold" warnings even when VICTORY_TRACE_GPU_FAILS is unset.
  3. Uncommitted submission fence signals (crates/victory-gpu/src/native/host.rs): When submit_passes fails with !f.committed, raise the submission's fence signals (when ahead of completed) after paying CPU fills and issuing a release fence.

Verification

  • cargo xtask policy
  • cargo test --locked -p victory-metal --lib -- owed_tests
  • cargo test --locked -p victory-gpu --lib -- vertex_tests
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo clippy --workspace --all-targets --all-features --locked --target x86_64-pc-windows-gnullvm -- -D warnings

Closes #408
Refs #80, #525
cc @⁠Olima34 @⁠wz11wz

**@⁠Sheffy007** on GitHub · 2026-11-28 02:57 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/547) `Sheffy007:fix/gpu-timeout-cascade-recovery` → `main` · +272 −75 · 5 files · 1 commits · open ## Summary Addresses the GPU timeout / residency-order cascade analyzed in #408 (and #525): 1. **Per-piece `MTLFence` & failed `next_piece` signal refund (`crates/victory-metal/src/gfx.rs`):** Give each split-submission piece (`VICTORY_PASSES_PER_BUFFER`) its own intra-piece `MTLFence` (`Order`) while keeping inter-piece ordering on `piece_event` (`MTLSharedEvent`), so a timed-out earlier piece never leaves the next piece wedged on an unsignalled GPU-only `MTLFence`. If `next_piece` fails after `queue.3` was incremented, attach `(queue.2, queue.3)` and the submission's fence signals to the last committed piece's completion refund (`attach_refund_and_events`). 2. **Fast recovery of swept queue followers ahead of the title's 30 s watchdog (`crates/victory-metal/src/gfx.rs`, `crates/victory-gpu/src/native/glue.rs`):** - Flag `HAD_FAILURE` on `landed(..., true)` so `glue::refresh` checks `recover_lost()` immediately (and every 200 ms rather than behind the 5 s warning log throttle) and calls `t.pump()` as soon as lost work is recovered. - Recover unreported earlier serials (`serial < failed_serial`) on a failed FIFO queue after `0.5 s`, sibling followers already committed on that queue when the failure/reset hit (`since <= failed_at`) after `3 s` (leaving fresh post-failure work untouched), and any unreported buffer on any queue after `15 s` — well ahead of the title's 30 s fatal watchdog (`0x1423cf683` -> `0x1455e56f8`). - Always include lagging `order_state()` queues in `"work held for a wait that does not hold"` warnings even when `VICTORY_TRACE_GPU_FAILS` is unset. 3. **Uncommitted submission fence signals (`crates/victory-gpu/src/native/host.rs`):** When `submit_passes` fails with `!f.committed`, raise the submission's fence signals (when ahead of `completed`) after paying CPU fills and issuing a release fence. ## Verification - `cargo xtask policy` - `cargo test --locked -p victory-metal --lib -- owed_tests` - `cargo test --locked -p victory-gpu --lib -- vertex_tests` - `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo clippy --workspace --all-targets --all-features --locked --target x86_64-pc-windows-gnullvm -- -D warnings` Closes #408 Refs #80, #525 cc @⁠Olima34 @⁠wz11wz
When macOS terminates a command buffer with kIOGPUCommandBufferCallbackErrorTimeout
(#404, #521), three gaps let the queue's residency-order cascade starve cross-queue
barriers past the title's 30 s fatal watchdog:

1. Multi-piece submissions (VICTORY_PASSES_PER_BUFFER) shared one GPU-only MTLFence
   across pieces, so a timed-out earlier piece left the next piece waiting on an
   unsignalled MTLFence that CPU recovery cannot raise, and a failed next_piece
   left queue.2 and submission fence signals unpaid. Give each piece its own
   intra-piece MTLFence (inter-piece ordering already goes through piece_event)
   and attach queue.2 + submission signals to the last committed piece's refund.
2. recover_lost waited 5 s plus glue's 5 s log throttle (10.9 s observed) for
   earlier serials and 30 s (matching the title's fatal watchdog) for sibling
   followers already committed when the queue reset hit. Flag failures for
   immediate host wakeup, check recover_lost every 200 ms with an immediate pump,
   recover earlier serials after 0.5 s, swept followers committed before the
   failure after 3 s, and any unreported buffer after 15 s, and always log
   lagging queue order_state values when work is held.
3. Uncommitted submissions in NativeHost::execute paid CPU fills without raising
   the submission's fence signals; signal fences>=completed after CPU fills.

Closes #404
Refs #80, #521
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 03:00:26 +00:00
Author
Owner

@⁠Olima34 on GitHub · 2026-11-28 03:54 UTC · original · requested changes

Thanks @⁠Sheffy007. Giving each piece its own MTLFence and refunding the order event and fence signals when next_piece fails both read right to us, and pay_refund keeps the take-under-lock rule the maintainer just landed. We read the whole diff of ba6821ea while helping the maintainer clear the queue. The part we can't take without evidence is the new recovery horizons in recover_lost_after:

  1. LOST_ANYWAY 30 s → 15 s. docs/native-host.md on main records long command buffers that did complete: "a single 30 s dispatch completed" (the Metal event-wait finding), and "a pass of res5 ran 25 s and completed" (the stop paragraph, #360). Under the new rule, a buffer like that has its fence signals and value writes paid from the CPU at 15 s while it is still running. The title then goes on and reads results the GPU hasn't written yet, and the GPU's late writes land over newer values. Cold-cache world frames are where such buffers happen.
  2. LOST_WITH_A_FAILURE (3 s, f.since <= at). This treats every buffer already committed on the queue when one failed as swept, including one that is simply still running after the failed one. The same problem as 1 then applies at 3 s. If Metal's reset really sweeps followers without reporting them, a traced run should show it: buffers committed before the failure, never reported completed or failed, for minutes. Please post the VICTORY_TRACE_GPU_FAILS=1 lines from such a run. The rule could also be narrowed to buffers whose status Metal shows as not completed after the failure, rather than by age alone.
  3. LOST_BEHIND_A_FAILURE 5 s → 0.5 s. It's the same FIFO argument as before, but at 0.5 s an older buffer that is merely slow gets paid too. What did you see that makes 5 s too long, ahead of the 30 s watchdog?

The "Guess:" note in the code is the right place for this, and docs/native-host.md should say the same. What's missing is a run showing the new horizons recover a real cascade without paying work that would have finished. Two before/after headless world runs with VICTORY_TRACE_GPU_FAILS=1, compared with tools/macos/run-summary.sh --compare, would settle it. If the faster pump (take_had_failure), the per-piece fences and the refunded events can stand on their own, splitting them from the horizon change would let those merge first.

CI is green on this head. No game run on our side.

**@⁠Olima34** on GitHub · 2026-11-28 03:54 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/547#pullrequestreview-6095593430) · **requested changes** Thanks @⁠Sheffy007. Giving each piece its own `MTLFence` and refunding the order event and fence signals when `next_piece` fails both read right to us, and `pay_refund` keeps the take-under-lock rule the maintainer just landed. We read the whole diff of ba6821ea while helping the maintainer clear the queue. The part we can't take without evidence is the new recovery horizons in `recover_lost_after`: 1. **`LOST_ANYWAY` 30 s → 15 s.** docs/native-host.md on main records long command buffers that did complete: "a single 30 s dispatch completed" (the Metal event-wait finding), and "a pass of res5 ran 25 s and completed" (the stop paragraph, #360). Under the new rule, a buffer like that has its fence signals and value writes paid from the CPU at 15 s while it is still running. The title then goes on and reads results the GPU hasn't written yet, and the GPU's late writes land over newer values. Cold-cache world frames are where such buffers happen. 2. **`LOST_WITH_A_FAILURE` (3 s, `f.since <= at`).** This treats every buffer already committed on the queue when one failed as swept, including one that is simply still running after the failed one. The same problem as 1 then applies at 3 s. If Metal's reset really sweeps followers without reporting them, a traced run should show it: buffers committed before the failure, never reported completed or failed, for minutes. Please post the `VICTORY_TRACE_GPU_FAILS=1` lines from such a run. The rule could also be narrowed to buffers whose status Metal shows as not completed after the failure, rather than by age alone. 3. **`LOST_BEHIND_A_FAILURE` 5 s → 0.5 s.** It's the same FIFO argument as before, but at 0.5 s an older buffer that is merely slow gets paid too. What did you see that makes 5 s too long, ahead of the 30 s watchdog? The "Guess:" note in the code is the right place for this, and docs/native-host.md should say the same. What's missing is a run showing the new horizons recover a real cascade without paying work that would have finished. Two before/after headless world runs with `VICTORY_TRACE_GPU_FAILS=1`, compared with `tools/macos/run-summary.sh --compare`, would settle it. If the faster pump (`take_had_failure`), the per-piece fences and the refunded events can stand on their own, splitting them from the horizon change would let those merge first. CI is green on this head. No game run on our side.
Author
Owner

@⁠wz11wz on GitHub · 2026-11-28 05:12 UTC · original

This matches what we measured in #408 — strong LGTM on the mechanism, with two additions from our data.

Confirming evidence (240s headless repro runs, VICTORY_TRACE_GPU_FAILS=1):

  • The sweep is device-wide: at the wedge we saw command buffers on two different queues killed by kIOGPUCommandBufferCallbackErrorTimeout in the same instant, including a 0-pass signal-only buffer — followers on the swept queue then hang at waits=0 (the residency-order wait is not counted in the label's waits), and four other queues stall on one fence each missing exactly one signal.
  • The title's ~30s fatal watchdog matches our timeline: the title died ~20–25s after the wedge, at 0x1455e56f8 via 0x1423cf683 — same chain your recovery targets.
  • Kernel spin is ruled out: the six blamed containers plus two controls finish on this stack (dispatch + 64-list concurrency, victory-gpu/src/native/suspect_tests.rs), and IR analysis shows none of them contains a loop. The freeze-window kernels (12 containers via victory-shader --example dump_dxil) are all bounded too — the two Scheduled-stuck command buffers' full dispatch lists are in the report.

1. The order counter race is still in this PR. queue.3 is a Cell<u64>: let n = queue.3.get(); queue.3.set(n + 1); is a lost-update window if the title calls ExecuteCommandLists on one queue from two threads (legal in D3D12), and the new tail_events.push((queue.2.clone(), queue.3.get())) re-reads it again — under concurrency that signals a slot another submission was waiting on, breaking the serialization this whole design relies on (two command buffers running concurrently = buffer races the residency ordering exists to prevent). We run an atomic fetch_add with each submission signalling its own n + 1 (captured at encode time, also used for Owed) — happy to split that out as a PR if you'd rather keep this one focused.

2. One crash shape this PR doesn't cover yet: with VICTORY_METAL_RESIDENCY_SETS=0 (no order-event chain at all) we still got the same 0xc0000005 at image+0x55e56f8 crash at t≈168 — with zero GPU Timeout kills and the GPU alive but slow (fence lag ≈ 9, counters moving). Nothing failed, so HAD_FAILURE/recover_lost never fires, and the title's watchdog still tripped on lagging fences. That shape points at throughput collapse in the Prestream zone rather than a stuck sweep — recovery alone won't stop it; the budget problem (a world submission exceeding the ~5s device watchdog under ordered residency) remains. Worth keeping in scope before closing #408.

Verification tooling: our cascade repro (a_never_finishing_dispatch_cascades_to_waits_zero_followers: spin-kernel root → trivial follower, asserts the follower hangs) plus the suspect-dispatch tests are in victory-gpu/src/native/suspect_tests.rs — they should pass identically before/after this PR and directly exercise the swept-follower path your recover now covers.

**@⁠wz11wz** on GitHub · 2026-11-28 05:12 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/547#issuecomment-7234524181) This matches what we measured in #408 — strong LGTM on the mechanism, with two additions from our data. **Confirming evidence** (240s headless repro runs, `VICTORY_TRACE_GPU_FAILS=1`): - The sweep is device-wide: at the wedge we saw command buffers on *two different queues* killed by `kIOGPUCommandBufferCallbackErrorTimeout` in the same instant, including a 0-pass signal-only buffer — followers on the swept queue then hang at `waits=0` (the residency-order wait is not counted in the label's `waits`), and four other queues stall on one fence each missing exactly one signal. - The title's ~30s fatal watchdog matches our timeline: the title died ~20–25s after the wedge, at `0x1455e56f8` via `0x1423cf683` — same chain your recovery targets. - Kernel spin is ruled out: the six blamed containers plus two controls finish on this stack (dispatch + 64-list concurrency, `victory-gpu/src/native/suspect_tests.rs`), and IR analysis shows none of them contains a loop. The freeze-window kernels (12 containers via `victory-shader --example dump_dxil`) are all bounded too — the two Scheduled-stuck command buffers' full dispatch lists are in the report. **1. The order counter race is still in this PR.** `queue.3` is a `Cell<u64>`: `let n = queue.3.get(); queue.3.set(n + 1);` is a lost-update window if the title calls `ExecuteCommandLists` on one queue from two threads (legal in D3D12), and the new `tail_events.push((queue.2.clone(), queue.3.get()))` re-reads it again — under concurrency that signals a slot another submission was waiting on, breaking the serialization this whole design relies on (two command buffers running concurrently = buffer races the residency ordering exists to prevent). We run an atomic `fetch_add` with each submission signalling its own `n + 1` (captured at encode time, also used for `Owed`) — happy to split that out as a PR if you'd rather keep this one focused. **2. One crash shape this PR doesn't cover yet:** with `VICTORY_METAL_RESIDENCY_SETS=0` (no order-event chain at all) we still got the same `0xc0000005 at image+0x55e56f8` crash at t≈168 — with **zero GPU Timeout kills** and the GPU *alive but slow* (fence lag ≈ 9, counters moving). Nothing failed, so `HAD_FAILURE`/`recover_lost` never fires, and the title's watchdog still tripped on lagging fences. That shape points at throughput collapse in the Prestream zone rather than a stuck sweep — recovery alone won't stop it; the budget problem (a world submission exceeding the ~5s device watchdog under ordered residency) remains. Worth keeping in scope before closing #408. **Verification tooling:** our cascade repro (`a_never_finishing_dispatch_cascades_to_waits_zero_followers`: spin-kernel root → trivial follower, asserts the follower hangs) plus the suspect-dispatch tests are in `victory-gpu/src/native/suspect_tests.rs` — they should pass identically before/after this PR and directly exercise the swept-follower path your `recover` now covers.
Author
Owner

@⁠wz11wz on GitHub · 2026-11-28 06:44 UTC · original

Measured data on the LOST_ANYWAY threshold — 15s misfires on legitimate frames, and lag accumulation caps what any recovery threshold can buy.

We kept your branch (plus an unrelated order-counter atomicity fix) and ran the same 300s headless repro with VICTORY_METALFX=1, ordered residency (mode="a set per heap"), VICTORY_TRACE_GPU_FAILS=1:

LOST_ANYWAY runs GPU Timeout kills never-reported recoveries title crash
15s (this PR) 1 0 4 in a row (3/3/4/2) t≈246, image+0x55e56f8
30s (reverted) 1 0 1 t≈254, same address

The 15s threshold pays running command buffers. The world-frame submission #9443 (171 passes, 362 draws + indirect compute) was committed at t=170.56, and recover_lost_after fired at 05:31:44 — its first unreported was 15.2s old — while #9443 was still executing. It completed at 05:31:52 with failed=False (took 22.752s, gpu 18.421s): the CPU-paid signals/events landed 7.5s before the GPU's real ones, and the GPU's late writes fell on top of paid values. Three more recovery bursts followed (3/3/4/2). With the threshold at 30s the same frame completes unmolested (one recovery, for a different buffer).

But 30s does not stop the crash — lag accumulates. Single-frame GPU time (18.4s) is under the title's ~30s tolerance, yet frames queue back to back: frame N takes 18s, frame N+1 queues behind it, so frame N+1's fences lag ~36s — past the title's watchdog — and it crashes at the same 0x1455e56f8. That's why every threshold fix only moved the crash later (t≈136 → 246 → 254) without removing it.

Recommendations:

  1. Revert LOST_ANYWAY to 30s (the measured legitimate-frame ceiling is 18.4s; 15s pays healthy work every world frame, which is worse than not paying — the GPU's late writes land on top of paid values).
  2. This PR's value stands regardless: it eliminated the timeout sweep entirely (0 kills vs 3–86) and extended survival. Keep it as the stopgap.
  3. #408 should not close on this PR: the terminal fix is per-frame GPU time (the 171-pass world frame needs 4–18s against a 33ms budget — mesh→VS/PS fallback, translation-layer speedups, resolution), plus the title-side fence tolerance. Happy to keep our status/order-state probes and the cascade repro (victory-gpu/src/native/suspect_tests.rs) upstreamed as the regression harness for whichever direction lands.
**@⁠wz11wz** on GitHub · 2026-11-28 06:44 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/547#issuecomment-7235491460) **Measured data on the `LOST_ANYWAY` threshold — 15s misfires on legitimate frames, and lag accumulation caps what any recovery threshold can buy.** We kept your branch (plus an unrelated order-counter atomicity fix) and ran the same 300s headless repro with `VICTORY_METALFX=1`, ordered residency (`mode="a set per heap"`), `VICTORY_TRACE_GPU_FAILS=1`: | `LOST_ANYWAY` | runs | GPU Timeout kills | never-reported recoveries | title crash | |---|---|---|---|---| | 15s (this PR) | 1 | 0 | 4 in a row (3/3/4/2) | t≈246, image+0x55e56f8 | | 30s (reverted) | 1 | 0 | **1** | t≈254, same address | **The 15s threshold pays running command buffers.** The world-frame submission `#9443` (171 passes, 362 draws + indirect compute) was committed at t=170.56, and `recover_lost_after` fired at 05:31:44 — its first unreported was 15.2s old — while `#9443` was still executing. It completed at 05:31:52 with `failed=False` (`took 22.752s, gpu 18.421s`): the CPU-paid signals/events landed 7.5s before the GPU's real ones, and the GPU's late writes fell on top of paid values. Three more recovery bursts followed (3/3/4/2). With the threshold at 30s the same frame completes unmolested (one recovery, for a different buffer). **But 30s does not stop the crash — lag accumulates.** Single-frame GPU time (18.4s) is under the title's ~30s tolerance, yet frames queue back to back: frame N takes 18s, frame N+1 queues behind it, so frame N+1's fences lag ~36s — past the title's watchdog — and it crashes at the same `0x1455e56f8`. That's why every threshold fix only moved the crash later (t≈136 → 246 → 254) without removing it. Recommendations: 1. Revert `LOST_ANYWAY` to 30s (the measured legitimate-frame ceiling is 18.4s; 15s pays healthy work every world frame, which is worse than not paying — the GPU's late writes land on top of paid values). 2. This PR's value stands regardless: it eliminated the timeout sweep entirely (0 kills vs 3–86) and extended survival. Keep it as the stopgap. 3. #408 should not close on this PR: the terminal fix is per-frame GPU time (the 171-pass world frame needs 4–18s against a 33ms budget — mesh→VS/PS fallback, translation-layer speedups, resolution), plus the title-side fence tolerance. Happy to keep our status/order-state probes and the cascade repro (`victory-gpu/src/native/suspect_tests.rs`) upstreamed as the regression harness for whichever direction lands.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/551/head:github-bot/gh-547'
git switch 'github-bot/gh-547'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-547'
git switch 'github-bot/gh-547'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-547'
git switch 'github-bot/gh-547'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-547'
git switch 'main'
git merge --squash 'github-bot/gh-547'
git switch 'main'
git merge --ff-only 'github-bot/gh-547'
git switch 'main'
git merge 'github-bot/gh-547'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.