Stopping the process while command buffers wait on events can leave ~20 GB of GPU memory allocated until a reboot #357
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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:56:56Z.
Summary
If the process is stopped (SIGTERM, Ctrl-C, or a watcher's kill) while command buffers are asleep in the kernel waiting on shared events nobody will signal, the GPU memory they hold can stay allocated after the process exits, owned by no process, until a reboot.
What I saw
Apple M5 Max, 36 GB, macOS 26.5. This happened twice in world runs. Both times the game was then stopped (SIGTERM, then SIGKILL after 3 s) right after the first
kIOGPUCommandBufferCallbackErrorTimeout:ioreg -c IOAcceleratorshowed"In use system memory"at about 20–21 GB, against about 1–2 GB normally.vm_statshowed about 24 GB wired, against 4–6 GB normally. It did not come down over 20+ minutes,purgedoesn't touch it, and a reboot clears it. In between, the Mac swapped heavily. The second time it eventually froze and needed a hard restart.footprintsum over all processes came to about 2 GB of GPU memory. NoAGXDeviceUserClientof the game was left.recoveryCount0). The kernel log showed the game's command queuestimed out waiting for prior signals!every 5 s, and two more such timeouts on the game's queues 3.5 s after launchd had removed the process. So the process was torn down with command buffers still asleep on events.VICTORY_METAL_RESIDENCY_SETS=heap,heap_set_mb=16356.kill -9, also after the 5 s timeout. The memory always came back. So it needs the scale or the pressure of the real run as well, and I didn't push further, to avoid freezing the Mac again.Proposed change
On SIGTERM, SIGINT and SIGHUP, before exiting: raise every shared event the backend made (queue order events, fences, piece events) past any awaited value, so no command buffer stays asleep; wait a few seconds for those in flight to complete; pay on the CPU whatever is left; then exit. The signals are taken with
sigwaiton a thread of their own, so the stop runs as ordinary code. I'd like to take this one and will send a PR.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by derz92 on 2026-11-27T13:56:58Z.
/claim
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-27T13:57:11Z.
Claimed by @derz92. Open a draft pull request with
Refs #357in its description within 24 hours, or the claim expires.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T14:52:32Z.
Same picture here (M5 Pro 48 GB): five times today the kernel kept 20–40 GB after a world run, with no owning process, and WindowServer froze until a reboot. Two things that may help #358:
unhandled exception … image+0x55e56f8, exit 134), and the memory still stayed stranded. So draining only on SIGTERM/SIGINT/SIGHUP may miss it. The fault and abort path (andstd::process::exitafter an unhandled exception) needs the same wake-and-drain.The two are complementary: that fix stops the stall, and #358 stops a stall, or any kill, from stranding memory.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by derz92 on 2026-11-27T15:05:15Z.
Update: a GPU hang (
kIOGPUCommandBufferCallbackErrorHang) at world entry, with the default per-heap residency sets, stranded 19.7 GB again even with #358's stop (in_flight=4 left=4: hung work can't be woken). So #358 covers a stop with waiting work, not a hung GPU. Details on #358 and #79.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by derz92 on 2026-11-27T20:43:27Z.
A heap-mode GPU hang that this PR's stop can't recover from, and a mode-1 run of the same session that was clean. M5 Max, 36 GB, macOS 26.5, Series S.
Heap mode, main
b998bb65+ #358 (f2b476dafrom #383 included):kIOGPUCommandBufferCallbackErrorTimeout, 8 failed buffers.in_flight=6 left=4: four command buffers never finished.So once the GPU itself hangs, waking and draining doesn't help (as I noted earlier). On macOS 26.5 the hung work can also take WindowServer down with it, not just strand memory.
Mode 1, same session after the reboot:
b998bb65+ #437 + #358),VICTORY_METAL_RESIDENCY_SETS=1with #437's other switches, 337 s, cold cache.This matches what Ketan-Kapse saw in #380: heap mode froze their 24 GB machine too, and mode 1 was clean. Until the hang is understood, I'd suggest the docs warn against
=heapon macOS 26.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T21:02:40Z.
A data point for the stranding, with the stop now on main. On preview
b3c1a1b0+ the fatal-fault hook, the GPU was running a draw that never ends: one indirect draw read as 864,026,624 instances from stale memory (#459). The SIGTERM stop waited 3 s + 20 s and exited within_flight=15 left=9. The kernel loggedGPURestart type=213 s later, and the driver kept 18.75 GB until a reboot.So the stop gives the memory back for waiting buffers (derz92's runs), but not while the GPU is executing work that never ends. For that case the fix has to be preventive: #383 gates dispatches, and #459 gates draws.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by derz92 on 2026-11-27T21:54:48Z.
A correction to my heap-mode comment above. The heap run that hung was on main
b998bb65, and the clean mode-1 run was on #437's tree, so the build differs between the two. Heap mode also ran clean in two earlier runs here. I can't attribute the hang to residency mode. It fits the stale indirect-args cause traced in #459/#383 better. Please read that comment as one hang plus a reboot on this Mac, not as evidence against=heap.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by lautarocherro on 2026-11-27T22:22:24Z.
A crash in the stop path on current main (
b998bb65, with9830ce50and38e74d00). M5 Pro 48 GB, macOS 27.2.1, Series X, headless runs stopped byrun-native.sh --seconds 300(SIGTERM). 2 of 8 runs ended with exit code 139 (SIGSEGV) instead of 143, after the world had run to the time limit.stop: residency sets emptied sets=2 mb=24052, thenstopped the GPU's work; exiting in_flight=4 left=0, then the segfault.stopping: waking and draining the GPU's queued work first(cb commit #19624and#19625withVICTORY_TRACE_COMMITS=1). The process segfaulted beforestopped the GPU's workwas logged.So title threads keep submitting while the stop drains and empties the sets, and the process exits under them. Blocking new submissions before the drain (a flag the submit path checks, parking the caller) would likely close both. GPU memory came back after both runs (0.7 GB a minute later), so nothing was stranded. I haven't changed the code, since it's under your claim.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by maaalrey on 2026-11-28T01:50:42Z.
A stack for the exit 139 at the stop, in case it helps (lautarocherro reported 2 of 8 without one).
M4 Max, 128 GB, macOS 27.2.1, Series S, windowed,
86cefd22+ #459 plus local dump-only changes. My runs are stopped by SIGTERM from a timer. Of the eight runs on this build that were in the world when the signal came, 4 ended with exit 139 and 4 exited cleanly (stopped the GPU's work; exiting in_flight=0 left=0). Runs stopped on the loading screen all exited cleanly.The four crash reports show the same path. The faulting thread is the host's refresh timer thread, not the stopping thread:
start_timerthread →glue::refresh→pump→execute→remirror_tables→mirror→write_buffer→Metal::heap_entry→-[IOGPUMetalResidencySet commit]→ bad address (0x28 in one, wild pointers in two). The fourth faults one step earlier, inwrite_bufferitself, at address 0x20.In all four the log has
stopping: waking and draining the GPU's queued work first signal=15and then nothing:stop: residency sets emptiedis never reached. So I read the fault as coming during the drain, after the events are raised and beforerelease_sets, with the refresh thread still mirroring descriptors and committing a heap's residency set. That fits lautarocherro's point that work goes on being submitted while the stop drains; I have not found what the commit trips over.Driver memory was back to 1 to 2 GiB after each of these four, so I saw no stranded memory.
The issue is claimed, so I am leaving the code alone.