metal, app: wake and drain the GPU's work before exiting on a signal (#357) #358
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/358"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by derz92 on 2026-11-27T13:58:25Z.
What this changes
Refs #357.
SIGTERM, SIGINT and SIGHUP now stop the GPU's work before the process exits, instead of the process dying with command buffers asleep in the kernel:
victory_metal::gfx::track_event: the backend remembers, weakly, every shared event it makes: queue order events (create_queue), title fences (create_fence) and piece events (submit_passeswithVICTORY_PASSES_PER_BUFFER).victory_metal::gfx::stop_gpu(wait)(re-exported asvictory_gpu::stop_gpu):waitfor the command buffers in flight (the existingflights()list) to be reported complete;Owed::recover, whatever is still left;(in_flight, left).apps/victory(macOS):mainblocks the three signals before any other thread starts, and a thread of its own takes them withsigwait. It logs the stop, callsstop_gpu(3 s), logs the result and exits with128 + signal. The stop runs as ordinary code, not in a signal handler.docs/native-host.md has the finding and the change, next to the existing note on Metal's 5 s event-wait limit.
How it was tested
cargo test -p victory-metal --lib(arm64): 37 passed. New:a_stop_wakes_command_buffers_waiting_on_events_nobody_signals. A command buffer waits on a fence nobody signals. After waking that fence and the queue's order event, it completes on the GPU within the deadline: its own fence signal is reached, nothing is left to pay, and both events were tracked when made. With nothing woken (checked by running the test with an empty event list), it is still in flight at the deadline (left1).--console series-s. A watcher's SIGTERM during loading gavestopping: waking and draining the GPU's queued work first signal=15and thenstopped the GPU's work; exiting in_flight=0 left=0, and the process exited at once, without the SIGKILL that follows 15 s later. The GPU driver's "In use system memory" was 1819 MB before the run and 1810 MB after.cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory: all pass excepthost_posix::ucrt::tests::dlls_that_are_not_the_c_runtime_are_left_alone, which fails the same way on main.cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warningscargo xtask policyChecklist
VICTORY_*variable and documented (no new diagnostics; the stop logs two lines when a signal arrives)Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by derz92 on 2026-11-27T15:02:14Z.
A game-run data point for the case the PR is for: a stop with work in flight.
Apple M5 Max, macOS 26.5,
--console series-s,VICTORY_METAL_RESIDENCY_SETS=0, this branch on main728845d1(with #335, #341, #342). A world run stopped by a watcher's SIGTERM after 240 s in the world (about one frame every 13 s at that point) logged:So 8 command buffers were in flight when the signal came, all completed on the GPU within the 3 s, nothing had to be paid on the CPU, and the process exited by itself before the watcher's SIGKILL (15 s later). The GPU driver's "In use system memory" was 1854 MB before the run and 1165 MB after.
That run didn't hit a GPU time-out, so it still doesn't reproduce the stranded-memory case from #357. It does show the stop working with real work in flight.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by derz92 on 2026-11-27T15:05:12Z.
A correction to the scope of this PR, from the next run.
Same Mac and branch, this time with the default residency mode for 36 GB (
mode="a set per heap",switch="unset"). At world entry, about 89 s in, Metal reported a real GPU hang,kIOGPUCommandBufferCallbackErrorHang(code 3), followed bykIOGPUCommandBufferCallbackErrorInnocentVictim. The watcher sent SIGTERM at once. The stop loggedin_flight=4 left=4: the hung GPU never completed those four, so they were paid on the CPU. After exit the GPU driver held 19,724 MB "In use system memory" (1,219 MB before the run), and it isn't coming back without a reboot.So this change does not prevent the strand when the GPU itself hangs. Raising the events can wake command buffers that are waiting, not ones the GPU is stuck executing. It still makes a stop with waiting work clean (the earlier run:
in_flight=8 left=0, memory returned), but it can't replace avoiding the hang. I'll update #357 to say so. I'm keeping the PR open as a partial mitigation unless you'd rather not take it on those terms.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by derz92 on 2026-11-27T15:32:12Z.
Pushed a second commit (rebased onto main
728845d1): a stop now also empties the residency sets before the process exits.Why: process exit skips
Heap'sDrop, so the heaps' residency sets (and the pool's) were still committed, with everything in them, when the process went. In the run that hung the GPU, those sets held about 16 GB, and 19.7 GB stayed "in use" on the driver after exit. The backend now tracks its residency sets weakly (track_residency_set, atcreate_heapand the pool set), andstop_gpu, after the drain, callsremoveAllAllocationsandcommiton each. It logsstop: residency sets emptied sets=<n> mb=<held>. New test:a_stop_empties_the_residency_sets. docs/native-host.md is updated.Game run (M5 Max, macOS 26.5,
VICTORY_METAL_RESIDENCY_SETS=heap, Series S): about 4 minutes in the world at about 1–2 fps, no GPU errors, stopped by a watcher's SIGTERM:The driver's "In use system memory" was 1447 MB before the run and 699 MB after.
Caveat, as before: this run didn't hang the GPU, so it doesn't yet show the release preventing the strand after a hang. I'll keep running world tests in heap mode, and I'll post here when one hangs, whichever way it goes.
Checks:
cargo test -p victory-metal --lib39 passed; Windows clippy gate clean;cargo xtask policyok; x86_64 HLE/loader/app tests pass except theucrttest that fails on main too.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T15:37:31Z.
@derz92 nice result:
sets=4 mb=17519emptied, and the driver back to 699 MB after exit, is the first stop we've seen give the memory back with the heap sets on. Three things that may help:eee76bf6265f33bb, is refused, so it never stores the record's [0, 1, 1], and the record keeps stale memory. The maintainer sees the same: a step under 1 ms running 25 s on garbage counts. #383 is a guard: a record above 2^22 groups runs nothing. With it, 5 of 6 world runs here went 170–300 s with no GPU error, against 5 of 5 freezing without it. Could you run heap mode with #358 + #383 together? If theHangs stop, that's the two halves: no hang, and a clean stop if one happens anyway. We're translatingeee76bf6itself now, which is the real fix.unhandled exception … image+0x55e56f8, exit 134) after the time-outs. Ifstop_gpuand the residency-set release only run on SIGTERM, SIGINT and SIGHUP, that path still strands memory. Hooking the same stop into the unhandled-exception or abort path would cover it.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by derz92 on 2026-11-27T15:55:36Z.
@Olima34 here's the run you asked for: heap mode with #358 + #383 together.
M5 Max, 36 GB, macOS 26.5,
--console series-s,VICTORY_METAL_RESIDENCY_SETS=heap,VICTORY_TRACE_GPU_FAILS=1, main728845d1+ #335 #341 #342 + this branch (now also with the fatal-fault hook, below) + #383, plus a local translator change. Cold shader cache.HangorTimeoutthrough the story load and into the world (frame 2176). My own watcher stopped it at 165 s, on a GPU-memory cap I'd set too low for heap mode (22 GB; the sets alone hold ~17 GB).residency sets emptied sets=4 mb=17379,in_flight=0 left=0. "In use system memory" was 1267 MB before and 1992 MB 5 s after exit, then 2.1 GB, not stranded.…ErrorHang). One run isn't proof, but it fits your 5 of 6.On your point 2: done. The loader now runs a hook once before the unhandled-exception filter (
victory_loader::native::seh::set_fatal_hook), and the app sets it to the samestop_gpu. So the title's watchdog fault drains and empties the sets too. Testthe_fatal_hook_runs_once, Windows clippy clean, docs updated. Pushing it to this branch.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T16:13:52Z.
Thanks @derz92, that's the combined evidence we needed:
The fatal-fault hook covers the exit our runs took. @LakshanSivakumar, from our side these two read as a pair for #79/#357: #383 stops the stale-count hang, and #358 makes any stop, including the title's watchdog fault, give the GPU memory back.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T16:15:39Z.
Your earlier head (
9830ce50) is merged in4cf84c83. I added one fix on top: after the first signal the stop thread unblocks SIGINT/SIGTERM/SIGHUP, so a second Ctrl-C ends the process if the drain or the exit hangs, and they are unblocked again if the thread cannot be started (9d775ebe). Tests, clippy, policy, release build and a 170 s headless game run passed on that tree. Leaving this open for your new commit41670d8d(stop on a fatal fault): it runs the GPU stop inside the exception path on the faulting thread, and I have not reviewed or run that yet.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T16:19:34Z.
A correction to my point 3 above. The ~0.35 s of kernel time is not a per-buffer cost, and not the frame-rate ceiling. In a traced 300 s run, only 7 command buffers had 50 ms or more of kernel time (0.31–0.36 s each). They came in 3 episodes about 25 s apart, across 3 queues at once, and each episode held the queues for 1.5–3.3 s with GPU time near 0.
An isolated Metal test shows that an unchanged set of 30,000 textures, or 8 GiB of wrapped memory, costs 0.03 ms per buffer. So an episode looks more like the whole set being re-wired (about 20 GiB) than like attach cost. Its trigger isn't identified yet. It matters for the 5 s limit (#79) rather than for fps.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by derz92 on 2026-11-27T19:54:23Z.
Rebased onto
b998bb65, which already has your second-signal fix (9d775ebe). What's left here is the one commit for stopping on a fatal fault. The conflict inmain.rswas only the newspawnedhandle: the hook is now set before the stop thread starts, and your unblock-on-failure path is untouched. Checks on this tree: victory and victory-loader tests pass (includingthe_fatal_hook_runs_once), Windows clippy is clean, and policy is ok. The game run that took this path (title watchdog fault →in_flight=8 left=0, sets emptied, driver memory back) is in my comment above.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by derz92 on 2026-11-27T23:41:53Z.
Rebased onto
86cefd22. The only conflicts were the docs paragraph (your 20 s wait for command buffers still in flight is kept, plus one sentence for the fatal-fault stop) andstop_gpu's newlongestargument. The fatal hook now passes the same 3 s / 20 s as the signal path, so both stops wait the same way. I flag it because it means the faulting thread can wait up to 20 s inside the exception path before the unhandled filter runs. If that's too long there, I can pass a shorter limit for the fatal case. Checks: victory and victory-loader tests pass (includingthe_fatal_hook_runs_once), Windows clippy clean, policy ok.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-28T00:38:15Z.
Review: CHANGES_REQUESTED
Thanks @derz92, and for the runs behind it. We read the whole diff of
3ad63471while helping the maintainer clear the queue. Stopping the GPU on a fatal fault makes sense, but one thing needs changing before it merges, plus a small docs fix:dispatch.rs,unhandled). If the filter returnsEXCEPTION_CONTINUE_EXECUTION,unhandledresumes the title, butstop_gpuhas already raised every event past its awaited value and emptied every residency set. The title then goes on submitting work against heaps that are no longer resident, and fences that are already past their values. Please run the hook only on the paths that end the process: theEXCEPTION_EXECUTE_HANDLERbranch beforestd::process::exit, and before the finalabort. If you also want to cover a filter that ends the process itself, the title's own exit/terminate path is the place for that, not before the filter. A test that a filter returningEXCEPTION_CONTINUE_EXECUTIONdoes not run the hook would pin it.CI is green on this head. We didn't do a game run (the maintainer asked us to work on GitHub only).
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by derz92 on 2026-11-28T00:42:02Z.
@Olima34, thanks, you're right on all three. Fixed in the new head commit:
unhandledasks the filter first. A newafter_filter(answer, ending)turns its answer intoResume,ExitorAbort, and runs the hook only forExit(beforestd::process::exit) andAbort(before the finalabort), never forEXCEPTION_CONTINUE_EXECUTION. The new testthe_fatal_hook_runs_only_when_the_filter_ends_the_processpins it: a resuming filter doesn't run the hook, and execute-handler, any other answer and no filter each run it once. I left a filter that ends the process itself (through the title's own exit) to the existing signal and exit paths, as you suggest.Checks on this head: victory and victory-loader tests pass, Windows clippy clean, policy ok.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-28T00:46:57Z.
The maintainer is handling this one himself; please follow his lead rather than my review.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.