Stopping the process while command buffers wait on events can leave ~20 GB of GPU memory allocated until a reboot #360

Closed
opened 2026-11-27 14:07:58 +00:00 by github-bot · 9 comments
Owner

@⁠derz92 on GitHub · 2026-11-27 13:56 UTC · original

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:

  • After exit, ioreg -c IOAccelerator showed "In use system memory" at about 20–21 GB, against about 1–2 GB normally. vm_stat showed about 24 GB wired, against 4–6 GB normally. It did not come down over 20+ minutes, purge doesn'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.
  • No process owned it. A footprint sum over all processes came to about 2 GB of GPU memory. No AGXDeviceUserClient of the game was left.
  • In the second case there was no GPU reset (recoveryCount 0). The kernel log showed the game's command queues timed 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.
  • The size matched that run's residency sets: VICTORY_METAL_RESIDENCY_SETS=heap, heap_set_mb=16356.
  • For contrast, @⁠alexballvideos-netizen's run in #79 ended with the title's own access violation, after its timeouts, and the wired memory came back (2.7 GB after exit).
  • Small repros don't strand anything. I tried a 1 GB standalone repro: a heap in a residency set, a command buffer waiting on an event nobody signals, then 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.
  • Others have seen the same: see #258 (Olima34: "the kernel kept 20–40 GB of GPU memory after exit, which hung WindowServer"), and Apple developer forum thread 839089 (residency-set memory not freed after teardown on 26.5).

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 sigwait on a thread of their own, so the stop runs as ordinary code. I'd like to take this one and will send a PR.

**@⁠derz92** on GitHub · 2026-11-27 13:56 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357) ## 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`: - After exit, `ioreg -c IOAccelerator` showed `"In use system memory"` at about 20–21 GB, against about 1–2 GB normally. `vm_stat` showed about 24 GB wired, against 4–6 GB normally. It did not come down over 20+ minutes, `purge` doesn'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. - No process owned it. A `footprint` sum over all processes came to about 2 GB of GPU memory. No `AGXDeviceUserClient` of the game was left. - In the second case there was no GPU reset (`recoveryCount` 0). The kernel log showed the game's command queues `timed 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. - The size matched that run's residency sets: `VICTORY_METAL_RESIDENCY_SETS=heap`, `heap_set_mb=16356`. - **For contrast**, @⁠alexballvideos-netizen's run in #79 ended with the title's own access violation, after its timeouts, and the wired memory came back (2.7 GB after exit). - **Small repros don't strand anything.** I tried a 1 GB standalone repro: a heap in a residency set, a command buffer waiting on an event nobody signals, then `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. - Others have seen the same: see #258 (Olima34: "the kernel kept 20–40 GB of GPU memory after exit, which hung WindowServer"), and Apple developer forum thread 839089 (residency-set memory not freed after teardown on 26.5). ## 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 `sigwait` on a thread of their own, so the stop runs as ordinary code. I'd like to take this one and will send a PR.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 14:07:58 +00:00
Author
Owner

@⁠derz92 on GitHub · 2026-11-27 13:56 UTC · original

/claim

**@⁠derz92** on GitHub · 2026-11-27 13:56 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7220142268) /claim
Author
Owner

@⁠github-actions[bot] on GitHub · 2026-11-27 13:57 UTC · original

Claimed by @⁠derz92. Open a draft pull request with Refs #357 in its description within 24 hours, or the claim expires.

**@⁠github-actions[bot]** on GitHub · 2026-11-27 13:57 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7220147373) Claimed by @⁠derz92. Open a draft pull request with `Refs #357` in its description within 24 hours, or the claim expires.
Author
Owner

@⁠Olima34 on GitHub · 2026-11-27 14:52 UTC · original

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 #361:

  • The exit path in our cases wasn't a signal we sent. The runs ended in the title's own watchdog fault after the time-outs (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 (and std::process::exit after an unhandled exception) needs the same wake-and-drain.
  • Why the buffers wait in the first place, posted on #79 an hour ago: the buffer that never completes holds an ExecuteIndirect dispatch whose records the GPU reads as absurd group counts (e.g. [510, 1182027346, 1314076063]). Everything queued behind it then times out waiting on its order event. A fix for that is in progress here.

The two are complementary: that fix stops the stall, and #361 stops a stall, or any kill, from stranding memory.

**@⁠Olima34** on GitHub · 2026-11-27 14:52 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7221338985) 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 #361: - **The exit path in our cases wasn't a signal we sent.** The runs ended in the title's own watchdog fault after the time-outs (`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 (and `std::process::exit` after an unhandled exception) needs the same wake-and-drain. - **Why the buffers wait in the first place,** posted on #79 an hour ago: the buffer that never completes holds an ExecuteIndirect dispatch whose records the GPU reads as absurd group counts (e.g. [510, 1182027346, 1314076063]). Everything queued behind it then times out waiting on its order event. A fix for that is in progress here. The two are complementary: that fix stops the stall, and #361 stops a stall, or any kill, from stranding memory.
Author
Owner

@⁠derz92 on GitHub · 2026-11-27 15:05 UTC · original

Update: a GPU hang (kIOGPUCommandBufferCallbackErrorHang) at world entry, with the default per-heap residency sets, stranded 19.7 GB again even with #361's stop (in_flight=4 left=4: hung work can't be woken). So #361 covers a stop with waiting work, not a hung GPU. Details on #361 and #79.

**@⁠derz92** on GitHub · 2026-11-27 15:05 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7221606481) Update: a GPU **hang** (`kIOGPUCommandBufferCallbackErrorHang`) at world entry, with the default per-heap residency sets, stranded 19.7 GB again even with #361's stop (`in_flight=4 left=4`: hung work can't be woken). So #361 covers a stop with *waiting* work, not a hung GPU. Details on #361 and #79.
Author
Owner

@⁠derz92 on GitHub · 2026-11-27 20:43 UTC · original

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 + #361 (f2b476da from #386 included):

  • The world was entered. At about 165 s, five fences stopped advancing on the GPU (for example, signal 5147 queued while the GPU was at 5135), and one queue held 5 command buffers on a wait for 27 s.
  • At 192 s: kIOGPUCommandBufferCallbackErrorTimeout, 8 failed buffers.
  • The stop ran: sets emptied, 18,953 MB. But it reported in_flight=6 left=4: four command buffers never finished.
  • After the process exited, the driver still held 6.3 GB.
  • About 3 minutes later, with nothing else running, WindowServer's watchdog restarted the Mac ("WindowServer main thread unresponsive, 40 seconds since last successful checkin"; thermal pressure heavy).

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:

  • #441's tree (b998bb65 + #441 + #361), VICTORY_METAL_RESIDENCY_SETS=1 with #441's other switches, 337 s, cold cache.
  • The world was entered and rendered lit, with 0 failed command buffers and 0 out-of-memory errors.
  • GPU "In use system memory" peaked at about 14 GB and came back to 0.96 GB at exit.

This matches what Ketan-Kapse saw in #383: 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 =heap on macOS 26.

**@⁠derz92** on GitHub · 2026-11-27 20:43 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7227778896) 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 + #361 (f2b476da from #386 included):** - The world was entered. At about 165 s, five fences stopped advancing on the GPU (for example, signal 5147 queued while the GPU was at 5135), and one queue held 5 command buffers on a wait for 27 s. - At 192 s: `kIOGPUCommandBufferCallbackErrorTimeout`, 8 failed buffers. - The stop ran: sets emptied, 18,953 MB. But it reported `in_flight=6 left=4`: four command buffers never finished. - After the process exited, the driver still held 6.3 GB. - **About 3 minutes later, with nothing else running, WindowServer's watchdog restarted the Mac** ("WindowServer main thread unresponsive, 40 seconds since last successful checkin"; thermal pressure heavy). 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:** - #441's tree (b998bb65 + #441 + #361), `VICTORY_METAL_RESIDENCY_SETS=1` with #441's other switches, 337 s, cold cache. - The world was entered and rendered lit, with **0 failed command buffers and 0 out-of-memory errors**. - GPU "In use system memory" peaked at about 14 GB and came back to 0.96 GB at exit. This matches what Ketan-Kapse saw in #383: 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 `=heap` on macOS 26.
Author
Owner

@⁠Olima34 on GitHub · 2026-11-27 21:02 UTC · original

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 (#463). The SIGTERM stop waited 3 s + 20 s and exited with in_flight=15 left=9. The kernel logged GPURestart type=2 13 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: #386 gates dispatches, and #463 gates draws.

**@⁠Olima34** on GitHub · 2026-11-27 21:02 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7228080006) 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 (#463). The SIGTERM stop waited 3 s + 20 s and exited with `in_flight=15 left=9`. The kernel logged `GPURestart type=2` 13 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: #386 gates dispatches, and #463 gates draws.
Author
Owner

@⁠derz92 on GitHub · 2026-11-27 21:54 UTC · original

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 #441'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 #463/#383 better. Please read that comment as one hang plus a reboot on this Mac, not as evidence against =heap.

**@⁠derz92** on GitHub · 2026-11-27 21:54 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7228864257) 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 #441'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 #463/#383 better. Please read that comment as one hang plus a reboot on this Mac, not as evidence against `=heap`.
Author
Owner

@⁠lautarocherro on GitHub · 2026-11-27 22:22 UTC · original

A crash in the stop path on current main (b998bb65, with 9830ce50 and 38e74d00). M5 Pro 48 GB, macOS 27.2.1, Series X, headless runs stopped by run-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.

  • In one, the stop finished first: stop: residency sets emptied sets=2 mb=24052, then stopped the GPU's work; exiting in_flight=4 left=0, then the segfault.
  • In the other, two more command buffers were committed 23 ms after stopping: waking and draining the GPU's queued work first (cb commit #19624 and #19625 with VICTORY_TRACE_COMMITS=1). The process segfaulted before stopped the GPU's work was 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.

**@⁠lautarocherro** on GitHub · 2026-11-27 22:22 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7229272843) A crash in the stop path on current main (`b998bb65`, with 9830ce50 and 38e74d00). M5 Pro 48 GB, macOS 27.2.1, Series X, headless runs stopped by `run-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. - In one, the stop finished first: `stop: residency sets emptied sets=2 mb=24052`, then `stopped the GPU's work; exiting in_flight=4 left=0`, then the segfault. - In the other, two more command buffers were committed 23 ms after `stopping: waking and draining the GPU's queued work first` (`cb commit #19624` and `#19625` with `VICTORY_TRACE_COMMITS=1`). The process segfaulted before `stopped the GPU's work` was 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.
Author
Owner

@⁠maaalrey on GitHub · 2026-11-28 01:50 UTC · original

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 + #463 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_timer thread → 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, in write_buffer itself, at address 0x20.

In all four the log has stopping: waking and draining the GPU's queued work first signal=15 and then nothing: stop: residency sets emptied is never reached. So I read the fault as coming during the drain, after the events are raised and before release_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.

**@⁠maaalrey** on GitHub · 2026-11-28 01:50 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7231955230) 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 + #463 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_timer` thread → `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, in `write_buffer` itself, at address 0x20. In all four the log has `stopping: waking and draining the GPU's queued work first signal=15` and then nothing: `stop: residency sets emptied` is never reached. So I read the fault as coming during the drain, after the events are raised and before `release_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.
This discussion has been locked. Commenting is limited to contributors.
No description provided.