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

Open
opened 2026-11-27 13:56:56 +00:00 by derz92 · 9 comments
derz92 commented 2026-11-27 13:56:56 +00:00 (Migrated from github.com)
Contributor

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:

  • 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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6647852619 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357). Originally posted by [derz92](https://github.com/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`: - 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 commented 2026-11-27 13:56:58 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by derz92 on 2026-11-27T13:56:58Z.


/claim

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7220142268 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7220142268). Originally posted by [derz92](https://github.com/derz92) on 2026-11-27T13:56:58Z. --- /claim
github-actions[bot] commented 2026-11-27 13:57:11 +00:00 (Migrated from github.com)
Author
Contributor

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 #357 in its description within 24 hours, or the claim expires.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7220147373 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7220147373). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-27T13:57:11Z. --- Claimed by @derz92. Open a draft pull request with `Refs #357` in its description within 24 hours, or the claim expires.
Olima34 commented 2026-11-27 14:52:32 +00:00 (Migrated from github.com)
Author
Contributor

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:

  • 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 #358 stops a stall, or any kill, from stranding memory.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7221338985 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7221338985). Originally posted by [Olima34](https://github.com/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: - **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 #358 stops a stall, or any kill, from stranding memory.
derz92 commented 2026-11-27 15:05:15 +00:00 (Migrated from github.com)
Author
Contributor

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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7221606481 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7221606481). Originally posted by [derz92](https://github.com/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.
derz92 commented 2026-11-27 20:43:27 +00:00 (Migrated from github.com)
Author
Contributor

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 (f2b476da from #383 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:

  • #437's tree (b998bb65 + #437 + #358), VICTORY_METAL_RESIDENCY_SETS=1 with #437'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 #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 =heap on macOS 26.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7227778896 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7227778896). Originally posted by [derz92](https://github.com/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 (f2b476da from #383 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:** - #437's tree (b998bb65 + #437 + #358), `VICTORY_METAL_RESIDENCY_SETS=1` with #437'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 #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 `=heap` on macOS 26.
Olima34 commented 2026-11-27 21:02:40 +00:00 (Migrated from github.com)
Author
Contributor

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 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: #383 gates dispatches, and #459 gates draws.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7228080006 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7228080006). Originally posted by [Olima34](https://github.com/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 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: #383 gates dispatches, and #459 gates draws.
derz92 commented 2026-11-27 21:54:48 +00:00 (Migrated from github.com)
Author
Contributor

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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7228864257 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7228864257). Originally posted by [derz92](https://github.com/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`.
lautarocherro commented 2026-11-27 22:22:24 +00:00 (Migrated from github.com)
Author
Contributor

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, 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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7229272843 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7229272843). Originally posted by [lautarocherro](https://github.com/lautarocherro) on 2026-11-27T22:22:24Z. --- 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.
maaalrey commented 2026-11-28 01:50:42 +00:00 (Migrated from github.com)
Author
Contributor

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_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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7231955230 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/357#issuecomment-7231955230). Originally posted by [maaalrey](https://github.com/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_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.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS#357
No description provided.