fix(gpu): shut down Streamline before ExitProcess to prevent DLSS-G teardown null deref #413

Merged
Androtim merged 4 commits from upstream-pr/413 into main 2026-12-04 01:43:43 +00:00
Androtim commented 2026-12-04 00:30:17 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-04T00:30:17Z.


Root Cause

With DLSS Frame Generation (VICTORY_FRAMEGEN=1) enabled, process exit paths (first-start warm-up auto-restart, settings menu restart, and window close) went through ending::end_process, which invoked ExitProcess(code) directly without shutting down Streamline (slShutdown was previously only called in upscaler-check).

Disassembly and symbol analysis of nvngx_dlssg.dll (NGX 310.9.16) at crash offset +0x16c40 identifies the failure mechanism:

  • +0x16b10..+0x16fa1 is DLSSGInstanceManager::ReleaseInstance, called by NVSDK_NGX_D3D12_ReleaseFeature (+0x13ba0).
  • At +0x16c40, mov rcx, [r15 + rax*8 + 0x8] indexes the std::unordered_map bucket array (r15 = [r14 + 0x60]).
  • When ExitProcess runs DLL_PROCESS_DETACH without a prior slShutdown(), nvngx_dlssg.dll's static DLSSGInstanceManager destructor frees its bucket vector (r15 = nullptr) before sl.dlss_g.dll tears down and invokes NVSDK_NGX_D3D12_ReleaseFeature, causing a 0xc0000005 null pointer dereference.

Changes

Streamline Pre-Exit Teardown

  • Added streamline::shutdown_for_exit() (idempotent shutdown_once() with VICTORY_STREAMLINE_TEARDOWN=0 escape hatch) invoked from ending::end_process before ExitProcess(code) on clean/restart exits.
  • Clears ready flags, drops Streamline's proxied swap chain, clears retained resource handles, and invokes slShutdown() under the Streamline call lock.

Gate 2 Audit Hardening

  1. Immediate termination on abnormal/fatal or device-removed exits (ending.rs):
    • On abnormal/fatal exit codes (code > 255, such as DXGI_ERROR_DEVICE_REMOVED 0x887A_0005) or when host_made().is_some_and(|h| unsafe { h.device.GetDeviceRemovedReason() }.is_err()), end_process skips shutdown_for_exit() and ExitProcess and calls TerminateProcess(GetCurrentProcess(), code) directly after save_pipeline_cache().
    • Eliminates same-thread and cross-thread AB-BA deadlocks on host.presenter / host.submitted during host::fail(), avoids DLL_PROCESS_DETACH, and preserves the exact exit code.
    • Clean/restart exits (code <= 255 with a healthy device and VICTORY_EXIT_WAIT_MS > 0) continue to use bounded shutdown_for_exit() + ExitProcess(code).
  2. Lock ordering and present/resize serialization (host.rs, streamline.rs):
    • Replaced drop_presenter_with_settle with drop_presenter_before_shutdown(settle, before_drop), which locks self.presenter first (preserving self.presenter -> Streamline CALLS lock order), runs disable_framegen_for_shutdown() while the proxied swap chain is still alive and no Present/ResizeBuffers is in flight, settles GPU work without holding CALLS, and drops the presenter.
    • Moved frame_token(sl, frame) inside the calls() lock after the SHUTTING_DOWN check and moved mark(abi::PRESENT_START) before drop(calls) in present_begins().
    • Guarded public resizing() on SHUTTING_DOWN both before and under calls().
  3. Single-owner end_process serialization (ending.rs):
    • Added ENDING_THREAD: AtomicU32 single-owner claim: concurrent callers from another thread park indefinitely so the first caller completes save_pipeline_cache() -> shutdown_for_exit() -> ExitProcess(code) cleanly; same-thread re-entry terminates immediately via TerminateProcess(GetCurrentProcess(), code).
  4. Avoid lazy device creation and uninitialized slShutdown (streamline.rs):
    • Replaced crate::host::host() with crate::host::host_made() in shutdown_once() and added early return in device_made() when SHUTTING_DOWN is set or !SL_INITIALIZED.
    • Added SL_INITIALIZED: AtomicBool set only after slInit returns 0 and gated shutdown_once() on it.

Verification

  • cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu -- streamline (23 passed)
  • cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu -- ending (25 passed, including exit-mode selection and ENDING_THREAD single-owner/re-entry tests)
  • cargo +1.98.1-x86_64-pc-windows-gnu check --workspace (clean)
<!-- greyfinch-sync:Androtim/MiniVI:issue:5637707032 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/413). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-04T00:30:17Z. --- ## Root Cause With DLSS Frame Generation (`VICTORY_FRAMEGEN=1`) enabled, process exit paths (first-start warm-up auto-restart, settings menu restart, and window close) went through `ending::end_process`, which invoked `ExitProcess(code)` directly without shutting down Streamline (`slShutdown` was previously only called in `upscaler-check`). Disassembly and symbol analysis of `nvngx_dlssg.dll` (NGX 310.9.16) at crash offset `+0x16c40` identifies the failure mechanism: - `+0x16b10..+0x16fa1` is `DLSSGInstanceManager::ReleaseInstance`, called by `NVSDK_NGX_D3D12_ReleaseFeature` (`+0x13ba0`). - At `+0x16c40`, `mov rcx, [r15 + rax*8 + 0x8]` indexes the `std::unordered_map` bucket array (`r15 = [r14 + 0x60]`). - When `ExitProcess` runs `DLL_PROCESS_DETACH` without a prior `slShutdown()`, `nvngx_dlssg.dll`'s static `DLSSGInstanceManager` destructor frees its bucket vector (`r15 = nullptr`) before `sl.dlss_g.dll` tears down and invokes `NVSDK_NGX_D3D12_ReleaseFeature`, causing a `0xc0000005` null pointer dereference. ## Changes ### Streamline Pre-Exit Teardown - Added `streamline::shutdown_for_exit()` (idempotent `shutdown_once()` with `VICTORY_STREAMLINE_TEARDOWN=0` escape hatch) invoked from `ending::end_process` before `ExitProcess(code)` on clean/restart exits. - Clears ready flags, drops Streamline's proxied swap chain, clears retained resource handles, and invokes `slShutdown()` under the Streamline call lock. ### Gate 2 Audit Hardening 1. **Immediate termination on abnormal/fatal or device-removed exits (`ending.rs`)**: - On abnormal/fatal exit codes (`code > 255`, such as `DXGI_ERROR_DEVICE_REMOVED` `0x887A_0005`) or when `host_made().is_some_and(|h| unsafe { h.device.GetDeviceRemovedReason() }.is_err())`, `end_process` skips `shutdown_for_exit()` and `ExitProcess` and calls `TerminateProcess(GetCurrentProcess(), code)` directly after `save_pipeline_cache()`. - Eliminates same-thread and cross-thread AB-BA deadlocks on `host.presenter` / `host.submitted` during `host::fail()`, avoids `DLL_PROCESS_DETACH`, and preserves the exact exit code. - Clean/restart exits (`code <= 255` with a healthy device and `VICTORY_EXIT_WAIT_MS > 0`) continue to use bounded `shutdown_for_exit()` + `ExitProcess(code)`. 2. **Lock ordering and present/resize serialization (`host.rs`, `streamline.rs`)**: - Replaced `drop_presenter_with_settle` with `drop_presenter_before_shutdown(settle, before_drop)`, which locks `self.presenter` first (preserving `self.presenter` -> Streamline `CALLS` lock order), runs `disable_framegen_for_shutdown()` while the proxied swap chain is still alive and no `Present`/`ResizeBuffers` is in flight, settles GPU work without holding `CALLS`, and drops the presenter. - Moved `frame_token(sl, frame)` inside the `calls()` lock after the `SHUTTING_DOWN` check and moved `mark(abi::PRESENT_START)` before `drop(calls)` in `present_begins()`. - Guarded public `resizing()` on `SHUTTING_DOWN` both before and under `calls()`. 3. **Single-owner `end_process` serialization (`ending.rs`)**: - Added `ENDING_THREAD: AtomicU32` single-owner claim: concurrent callers from another thread park indefinitely so the first caller completes `save_pipeline_cache()` -> `shutdown_for_exit()` -> `ExitProcess(code)` cleanly; same-thread re-entry terminates immediately via `TerminateProcess(GetCurrentProcess(), code)`. 4. **Avoid lazy device creation and uninitialized `slShutdown` (`streamline.rs`)**: - Replaced `crate::host::host()` with `crate::host::host_made()` in `shutdown_once()` and added early return in `device_made()` when `SHUTTING_DOWN` is set or `!SL_INITIALIZED`. - Added `SL_INITIALIZED: AtomicBool` set only after `slInit` returns `0` and gated `shutdown_once()` on it. ## Verification - `cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu -- streamline` (23 passed) - `cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu -- ending` (25 passed, including exit-mode selection and `ENDING_THREAD` single-owner/re-entry tests) - `cargo +1.98.1-x86_64-pc-windows-gnu check --workspace` (clean)
Androtim commented 2026-12-04 00:32:20 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-04T00:32:20Z.


Triage (8762b54d, CI IN_PROGRESS):

  • Clean, 100% title-neutral DLSS-G teardown crash fix (+180/-43 across ending.rs, host.rs, host/menu.rs, streamline.rs, docs/dlss.md, zero opt-in toggles): sets ENDING/SHUTTING_DOWN, releases presenter lock before end_process, drops the proxied swap chain, and calls slShutdown() prior to ExitProcess to prevent nvngx_dlssg.dll DLL_PROCESS_DETACH null derefs.
  • Holding auto-merge for green CI + Gate 2 Audit (#282) (specifically checking shutdown_for_exit vs present / record / reaper lock ordering and non-DLSS/AMD/Intel no-op path) + Gate 3 live DLSS-G exit & F1/warmup restart verification.
<!-- greyfinch-sync:Androtim/MiniVI:comment:7396892723 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/413#issuecomment-7396892723). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-04T00:32:20Z. --- Triage (`8762b54d`, CI `IN_PROGRESS`): - Clean, 100% title-neutral DLSS-G teardown crash fix (`+180/-43` across `ending.rs`, `host.rs`, `host/menu.rs`, `streamline.rs`, `docs/dlss.md`, zero opt-in toggles): sets `ENDING`/`SHUTTING_DOWN`, releases `presenter` lock before `end_process`, drops the proxied swap chain, and calls `slShutdown()` prior to `ExitProcess` to prevent `nvngx_dlssg.dll` `DLL_PROCESS_DETACH` null derefs. - Holding auto-merge for green CI + **Gate 2 Audit (`#282`)** (specifically checking `shutdown_for_exit` vs `present` / `record` / `reaper` lock ordering and non-DLSS/AMD/Intel no-op path) + **Gate 3 live DLSS-G exit & F1/warmup restart verification**.
Sign in to join this conversation.
No reviewers
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
Androtim/MiniVI!413
No description provided.