dlss: shut Streamline down at process exit (VICTORY_DLSS_TEARDOWN) #139

Open
github-bot wants to merge 3 commits from github-bot/gh-138 into main AGit
Owner

@⁠SovietBearz0r on GitHub · 2026-12-05 17:16 UTC · original
SovietBearz0r:dlss-shutdown-exit → main · +7258 −481 · 59 files · 49 commits · draft

Main defines dlss::shutdown() but the only caller is a test (dlss.rs:2319): on process exit Streamline (and DLSS frame generation, whose proxied swap chain and callbacks live in the plugin) is torn down by DLL detach after the title's callbacks are gone — the situation MiniVI's #413 fixes for crashes/0xc0000005 at exit.

Fix

  • dlss::shutdown() is now single-owner and idempotent (SHUTTING_DOWN CAS); repeat/concurrent calls are no-ops and the hardware test's call still works. Ready flags, evaluate_dlss, swap-chain upgrade/create return "off" once teardown begins.
  • slShutdown is reachable even when slInit succeeded but init_api then found no supported feature (a separate TEARDOWN handle is captured right after slInit).
  • New dlss::shutdown_for_exit() (gated on having initialized, VICTORY_DLSS_TEARDOWN on unless 0, and the device not removed): release the presenter first (DLSS-G off while its swap chain is alive, FSR FG retired, submitted work settled non-blockingly), null the device/queue/factory proxies, then slShutdown — MiniVI's order.
  • New loader hook victory_loader::set_before_dll_teardown(fn()), invoked in detach_all after the title images get DLL_PROCESS_DETACH and before the CRT ends the process (our tree has no ending.rs; this is the closest safe analogue). run.rs registers dlss::shutdown_for_exit there. Window-close/restart (lifecycle::end) is deliberately not hooked: it uses TerminateProcess, which skips DLL detach.

Verification

  • cargo test -p victory-gpu --lib: 715 passed, 0 failed, 21 ignored (new tests: teardown env parser, gating, no-op/idempotence without Streamline); cargo check -p victory, cargo xtask policy (439 files), clippy: no warnings in changed code.
  • Needs in-game NVIDIA check: RTX 40/50 with -Dlss quality -DlssFrameGen, clean quit → logs process exit: shutting NVIDIA Streamline down / NVIDIA Streamline shut down, no crash at exit; also DLSS-SR-only and VICTORY_DLSS_TEARDOWN=0 as an escape hatch.
  • Known limit: a title that calls ExitProcess directly / aborts still skips the hook (same as before).

Source: MiniVI #413, adapted to this tree's loader atexit emulation and DLSS layering.

**@⁠SovietBearz0r** on GitHub · 2026-12-05 17:16 UTC · [original](https://github.com/ReTerryDavis/ReVIctory/pull/138) `SovietBearz0r:dlss-shutdown-exit` → `main` · +7258 −481 · 59 files · 49 commits · draft Main defines `dlss::shutdown()` but the only caller is a test (`dlss.rs:2319`): on process exit Streamline (and DLSS frame generation, whose proxied swap chain and callbacks live in the plugin) is torn down by DLL detach after the title's callbacks are gone — the situation MiniVI's #413 fixes for crashes/`0xc0000005` at exit. ## Fix - `dlss::shutdown()` is now single-owner and idempotent (`SHUTTING_DOWN` CAS); repeat/concurrent calls are no-ops and the hardware test's call still works. Ready flags, `evaluate_dlss`, swap-chain upgrade/create return "off" once teardown begins. - `slShutdown` is reachable even when `slInit` succeeded but `init_api` then found no supported feature (a separate `TEARDOWN` handle is captured right after `slInit`). - New `dlss::shutdown_for_exit()` (gated on having initialized, `VICTORY_DLSS_TEARDOWN` on unless `0`, and the device not removed): release the presenter first (DLSS-G off while its swap chain is alive, FSR FG retired, submitted work settled non-blockingly), null the device/queue/factory proxies, then `slShutdown` — MiniVI's order. - New loader hook `victory_loader::set_before_dll_teardown(fn())`, invoked in `detach_all` after the title images get `DLL_PROCESS_DETACH` and before the CRT ends the process (our tree has no `ending.rs`; this is the closest safe analogue). `run.rs` registers `dlss::shutdown_for_exit` there. Window-close/restart (`lifecycle::end`) is deliberately not hooked: it uses `TerminateProcess`, which skips DLL detach. ## Verification - `cargo test -p victory-gpu --lib`: **715 passed, 0 failed, 21 ignored** (new tests: teardown env parser, gating, no-op/idempotence without Streamline); `cargo check -p victory`, `cargo xtask policy` (439 files), clippy: no warnings in changed code. - **Needs in-game NVIDIA check**: RTX 40/50 with `-Dlss quality -DlssFrameGen`, clean quit → logs `process exit: shutting NVIDIA Streamline down` / `NVIDIA Streamline shut down`, no crash at exit; also DLSS-SR-only and `VICTORY_DLSS_TEARDOWN=0` as an escape hatch. - Known limit: a title that calls `ExitProcess` directly / aborts still skips the hook (same as before). Source: MiniVI #413, adapted to this tree's loader atexit emulation and DLSS layering.
A run that used NVIDIA DLSS / DLSS Frame Generation never called
slShutdown: the process ended with the Streamline and NGX DLLs detached
in their own order, and the plugins could release an NGX feature
handle after its instance manager was gone, null-dereferencing at the
process's very end. The only caller of dlss::shutdown() was the
ignored hardware smoke test.

Port MiniVI PR #413: the loader gets a last hook that runs once the
title's images have detached and before the process's own DLLs are,
and the runner registers dlss::shutdown_for_exit() there. The teardown
turns DLSS-G off and drops the presenter (proxied swap chain, FSR FG,
submitted GPU work waited for when the device is healthy) before
slShutdown, is idempotent and single-owner, is skipped for a removed
device and can be turned off with VICTORY_DLSS_TEARDOWN=0. A library
initialised by slInit is now shut down even when no feature was picked
up afterwards. shutdown() (the test's call) stays working.

In-game NVIDIA DLSS-FG verification is still needed; CI/unit tests only
here.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-05 17:18:45 +00:00
Author
Owner

@⁠ReTerryDavis on GitHub · 2026-12-05 17:38 UTC · original

Marking this as draft for now. VICTORY_DLSS_TEARDOWN hasn't been tested on RTX 40/50 hardware yet, and there's a shutdown-ordering hazard to address first:

  • BEFORE_DLL_TEARDOWN runs in crates/victory-loader/src/threads.rs::detach_all after end_other_threads has already killed other threads (including Streamline/NGX/D3D12 workers) via TerminateThread. Running DlssSwap::resizing() and slShutdown() afterward risks deadlocking internal Streamline/NGX mutexes until the 5s watchdog fires.
  • dlss::teardown() (PROXIES.lock()) and DlssSwap::resizing() (RUNTIME.lock()) also use blocking .lock() rather than try_lock().

Happy to revisit once teardown runs before end_other_threads (or safely handles terminated workers) and is tested on RTX hardware.

**@⁠ReTerryDavis** on GitHub · 2026-12-05 17:38 UTC · [original](https://github.com/ReTerryDavis/ReVIctory/pull/138#issuecomment-7442356133) Marking this as draft for now. `VICTORY_DLSS_TEARDOWN` hasn't been tested on RTX 40/50 hardware yet, and there's a shutdown-ordering hazard to address first: - `BEFORE_DLL_TEARDOWN` runs in `crates/victory-loader/src/threads.rs::detach_all` *after* `end_other_threads` has already killed other threads (including Streamline/NGX/D3D12 workers) via `TerminateThread`. Running `DlssSwap::resizing()` and `slShutdown()` afterward risks deadlocking internal Streamline/NGX mutexes until the 5s watchdog fires. - `dlss::teardown()` (`PROXIES.lock()`) and `DlssSwap::resizing()` (`RUNTIME.lock()`) also use blocking `.lock()` rather than `try_lock()`. Happy to revisit once teardown runs before `end_other_threads` (or safely handles terminated workers) and is tested on RTX hardware.
The BEFORE_DLL_TEARDOWN hook ran at the tail of detach_all, i.e. after
end_other_threads had ended the process's other threads with TerminateThread,
Streamline's / its NGX plugins' / the driver's workers among them. Running
DlssSwap::resizing() and slShutdown() then can wait forever on a mutex an ended
thread held (until the 5 s exit watchdog ends the process), which is the hazard
the maintainer flagged.

Run the hook after the title's exit functions and the runner's final save, but
before end_other_threads, while those libraries' worker threads are alive. It
runs no title code, so it needs neither the title's images detached nor the
other threads gone: the title images still get their DLL_PROCESS_DETACH after
the threads end, in ExitProcess's order, and the process's own DLLs are
detached after detach_all returns either way. The ordering is explained in the
loader's comments.

Take the two exit-time locks without waiting: dlss::teardown()'s PROXIES lock
(skip and log the shutdown if it is held) and DlssSwap's RUNTIME lock in
set_dlssg_off (the DLSS-G turn-off is still sent; only the bookkeeping update
is skipped, with a warning), so a lock another thread is in cannot hang the
exit. The teardown stays idempotent and single-owner, and VICTORY_DLSS_TEARDOWN
(on unless 0) still gates the exit hook.

RTX 40/50 in-game testing remains owed (cannot be done here).
Author
Owner

@⁠SovietBearz0r on GitHub · 2026-12-05 18:55 UTC · original

Shutdown ordering fixed: BEFORE_DLL_TEARDOWN now runs after the runner's final save and before end_other_threads, while Streamline/NGX/driver workers are still alive; title-image DLL_PROCESS_DETACH deliberately stays after thread termination (its contract is "no other thread may use the image") and the one-step deviation from ExitProcess order is documented. dlss::teardown()'s PROXIES lock and set_dlssg_off()'s RUNTIME lock are now try_lock with clean skip+warn (the DLSS-G turn-off is still sent; only bookkeeping is skipped). VICTORY_DLSS_TEARDOWN gate and idempotency kept. victory-gpu 719 tests, loader 138. Staying draft for the RTX 40/50 in-game teardown check.

**@⁠SovietBearz0r** on GitHub · 2026-12-05 18:55 UTC · [original](https://github.com/ReTerryDavis/ReVIctory/pull/138#issuecomment-7443555436) Shutdown ordering fixed: `BEFORE_DLL_TEARDOWN` now runs after the runner's final save and **before `end_other_threads`**, while Streamline/NGX/driver workers are still alive; title-image `DLL_PROCESS_DETACH` deliberately stays after thread termination (its contract is "no other thread may use the image") and the one-step deviation from ExitProcess order is documented. `dlss::teardown()`'s `PROXIES` lock and `set_dlssg_off()`'s `RUNTIME` lock are now `try_lock` with clean skip+warn (the DLSS-G turn-off is still sent; only bookkeeping is skipped). `VICTORY_DLSS_TEARDOWN` gate and idempotency kept. `victory-gpu` 719 tests, loader 138. Staying draft for the RTX 40/50 in-game teardown check.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/139/head:github-bot/gh-138'
git switch 'github-bot/gh-138'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-138'
git switch 'github-bot/gh-138'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-138'
git switch 'github-bot/gh-138'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-138'
git switch 'main'
git merge --squash 'github-bot/gh-138'
git switch 'main'
git merge --ff-only 'github-bot/gh-138'
git switch 'main'
git merge 'github-bot/gh-138'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.