fix(agc): shut the driver down before frame replay exits with an error #14100

Merged
chitters2004 merged 3 commits from upstream-pr/14100 into main 2026-11-06 00:42:31 +00:00
chitters2004 commented 2026-10-31 18:58:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by chitters2004 on 2026-10-31T18:58:44Z.


What

agc_frame_replay now shuts the driver down on every exit path. It used to do so only after a successful replay.

On a replay error, main called std::_Exit(code) with the driver still running. On Windows that goes through ExitProcess, which ends the worker threads and then detaches the DLLs. Detaching the prx runs the static Driver's destructor, and its stop() waits on workers that are already gone. The process then hangs, or aborts, instead of returning its exit code.

  • Every path now calls AgcDriverShutdown_nid_postfix() before exiting, including exit code 3 (unfinished submissions).
  • A shutdown failure is printed instead of dropped. If the replay itself succeeded, the tool now exits 2 rather than 0.
  • Frames now outlives the driver. It lived on run()'s stack while the driver kept ReplayOutputs that reference it, so a failure reported during stop() would touch dead memory. Found by reading the code; no run has triggered it.

thuanquang found the hang and tested this approach on an RX 6700 XT, and savvaniss confirmed it on an RX 9070 XT, both on #12568. This PR is that fix against main, so it doesn't have to wait for that stack. agc_driver_frame_replay_reservation_error, which #11435 adds, is the first test to reach this path with the driver running.

Tested

Windows 11, RTX 3080, NVIDIA 621.01, WinLibs GCC 15.2.0 r7, Release.

The bug on main + #11435 (9392228b5c), with and without this PR. Direct runs of agc_frame_replay on the reservation_error capture, 6 each. A run still alive after 15 s counts as hung. All 24 runs print the expected EOP flip token is reserved for a different flip.

Setup Without this PR With this PR
Validation layer 6 of 6 FATAL: abort() at exit, exit code 127, about 0.95 s 6 of 6 exit code 2, no FATAL, about 0.64 s
No layer 6 of 6 hung 6 of 6 exit code 2, no FATAL, about 0.63 s

The test itself passes on this machine with the layer either way, because it only checks for a non-zero exit. A hung process here is left with one thread and refuses taskkill, which fits a process already in ExitProcess.

Focused tests. ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ctest -R "frame_(replay|capture)":

Tree Without this PR With this PR
main ca073ec3ad 11 of 11 11 of 11
main + #11435 15 of 15 15 of 15

Neither side logged a VUID- line or was not found but was requested by env var VK_INSTANCE_LAYERS, in the ctest logs or the tool's own logs. With this PR, 15 of 15 also pass without the layer, and reservation_error passes 5 of 5 on repeat both with and without it.

Exit code 3 on main + #11435. A capture with a registered video output and submissions that can never finish, so a Vulkan device is alive. Drain limit lowered from 600 s to 10 s locally:

Setup Without this PR With this PR
Validation layer 3 of 3 FATAL, test fails, about 11 s 6 of 6 exit 3, no FATAL, about 11 s
No layer 3 of 3 hung, killed at the 40 s ctest timeout 6 of 6 exit 3, no FATAL, about 11 s

On main alone this capture starts no device, so exit 3 behaves the same either way. No test for it is added here, because with #11435's 600 s limit it would take 10 minutes per run.

Final head 659a6eecb4. On main 0836dfdf4f: 11 of 11, with and without the layer. With #11435 9392228b5c merged, its test registrations carried over by hand because it conflicts with main: 15 of 15, with and without the layer. No VUID- lines.

Linux / RADV. janzezno ran f18a18ee45 on an RX 7900 GRE: 17 of 17 three times, and the error exit took about 0.056 s. This did not cover exit code 3 or the Frames commit.

Not tested: exit code 3 and the Frames commit on AMD or Linux. A real GPU wedge can still block shutdown, because a fence wait or vkDeviceWaitIdle in ~State relies on TDR or device loss to return.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 3080, driver 621.01, Windows 11
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5100591967 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14100). Originally posted by [chitters2004](https://github.com/chitters2004) on 2026-10-31T18:58:44Z. --- ### What `agc_frame_replay` now shuts the driver down on every exit path. It used to do so only after a successful replay. On a replay error, `main` called `std::_Exit(code)` with the driver still running. On Windows that goes through `ExitProcess`, which ends the worker threads and then detaches the DLLs. Detaching the prx runs the static `Driver`'s destructor, and its `stop()` waits on workers that are already gone. The process then hangs, or aborts, instead of returning its exit code. - Every path now calls `AgcDriverShutdown_nid_postfix()` before exiting, including exit code 3 (unfinished submissions). - A shutdown failure is printed instead of dropped. If the replay itself succeeded, the tool now exits 2 rather than 0. - `Frames` now outlives the driver. It lived on `run()`'s stack while the driver kept `ReplayOutput`s that reference it, so a failure reported during `stop()` would touch dead memory. Found by reading the code; no run has triggered it. thuanquang found the hang and tested this approach on an RX 6700 XT, and savvaniss confirmed it on an RX 9070 XT, both [on #12568](https://github.com/boykopovar/AnyPS5/pull/12568#issuecomment-6523688555). This PR is that fix against `main`, so it doesn't have to wait for that stack. `agc_driver_frame_replay_reservation_error`, which #11435 adds, is the first test to reach this path with the driver running. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11, RTX 3080, NVIDIA 621.01, WinLibs GCC 15.2.0 r7, Release. **The bug on `main` + #11435 (`9392228b5c`), with and without this PR.** Direct runs of `agc_frame_replay` on the `reservation_error` capture, 6 each. A run still alive after 15 s counts as hung. All 24 runs print the expected `EOP flip token is reserved for a different flip`. | Setup | Without this PR | With this PR | |---|---|---| | Validation layer | 6 of 6 `FATAL: abort()` at exit, exit code 127, about 0.95 s | 6 of 6 exit code 2, no `FATAL`, about 0.64 s | | No layer | 6 of 6 hung | 6 of 6 exit code 2, no `FATAL`, about 0.63 s | The test itself passes on this machine with the layer either way, because it only checks for a non-zero exit. A hung process here is left with one thread and refuses `taskkill`, which fits a process already in `ExitProcess`. **Focused tests.** `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ctest -R "frame_(replay|capture)"`: | Tree | Without this PR | With this PR | |---|---|---| | `main` `ca073ec3ad` | 11 of 11 | 11 of 11 | | `main` + #11435 | 15 of 15 | 15 of 15 | Neither side logged a `VUID-` line or `was not found but was requested by env var VK_INSTANCE_LAYERS`, in the ctest logs or the tool's own logs. With this PR, 15 of 15 also pass without the layer, and `reservation_error` passes 5 of 5 on repeat both with and without it. **Exit code 3 on `main` + #11435.** A capture with a registered video output and submissions that can never finish, so a Vulkan device is alive. Drain limit lowered from 600 s to 10 s locally: | Setup | Without this PR | With this PR | |---|---|---| | Validation layer | 3 of 3 `FATAL`, test fails, about 11 s | 6 of 6 exit 3, no `FATAL`, about 11 s | | No layer | 3 of 3 hung, killed at the 40 s ctest timeout | 6 of 6 exit 3, no `FATAL`, about 11 s | On `main` alone this capture starts no device, so exit 3 behaves the same either way. No test for it is added here, because with #11435's 600 s limit it would take 10 minutes per run. **Final head `659a6eecb4`.** On `main` `0836dfdf4f`: 11 of 11, with and without the layer. With #11435 `9392228b5c` merged, its test registrations carried over by hand because it conflicts with `main`: 15 of 15, with and without the layer. No `VUID-` lines. **Linux / RADV.** janzezno [ran `f18a18ee45`](https://github.com/boykopovar/AnyPS5/pull/14100#issuecomment-6548429899) on an RX 7900 GRE: 17 of 17 three times, and the error exit took about 0.056 s. This did not cover exit code 3 or the `Frames` commit. **Not tested:** exit code 3 and the `Frames` commit on AMD or Linux. A real GPU wedge can still block shutdown, because a fence wait or `vkDeviceWaitIdle` in `~State` relies on TDR or device loss to return. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 3080, driver 621.01, Windows 11 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-31 19:11:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T19:11:16Z.

Review: COMMENTED


Reviewed — code 3 path remains unverified

The new shutdown call runs after run() returns exit code 3, but the review evidence does not exercise that path.

What I ran

Reviewed head f18a18ee4508e0e602806ea8432ea3e8f714409a against base 16452da93473aa42d8054da95f89e44a2a1d8d97. Diff, conventions and merge-preview checks passed; the hosted Windows job passed. I did not build or run C++ tests locally.

Findings

I have not approved this PR yet because the exit-code-3 path needs evidence. FrameReplay.cpp now calls shutdown for every nonzero result, including 3. Shutdown joins driver workers and then calls DrawFinisher::Drain(); when jobs remain, that drain waits for the queue to empty with no timeout (implementation). The PR reports only a source inspection for exit code 3. Please run a controlled unfinished-submission replay through shutdown and report that it exits, or adjust the shutdown path if it can remain blocked.

Not verified

The author's Windows exit-code-2 reproduction is reported in the PR description; I did not independently reproduce it. Exit code 3 and its pending draw-finisher behavior were not tested.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5724465302 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14100#pullrequestreview-5724465302). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-31T19:11:16Z. **Review:** COMMENTED --- # Reviewed — code 3 path remains unverified The new shutdown call runs after `run()` returns exit code 3, but the review evidence does not exercise that path. ## What I ran Reviewed head `f18a18ee4508e0e602806ea8432ea3e8f714409a` against base `16452da93473aa42d8054da95f89e44a2a1d8d97`. Diff, conventions and merge-preview checks passed; the hosted Windows job passed. I did not build or run C++ tests locally. ## Findings I have not approved this PR yet because the exit-code-3 path needs evidence. [`FrameReplay.cpp`](https://github.com/boykopovar/AnyPS5/blob/f18a18ee4508e0e602806ea8432ea3e8f714409a/core/libs/prx/libSceAgcDriver/tools/FrameReplay.cpp#L785-L800) now calls shutdown for every nonzero result, including 3. Shutdown joins driver workers and then calls [`DrawFinisher::Drain()`](https://github.com/boykopovar/AnyPS5/blob/f18a18ee4508e0e602806ea8432ea3e8f714409a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cpp#L166-L174); when jobs remain, that drain waits for the queue to empty with no timeout ([implementation](https://github.com/boykopovar/AnyPS5/blob/f18a18ee4508e0e602806ea8432ea3e8f714409a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPipeline.cpp#L228-L234)). The PR reports only a source inspection for exit code 3. Please run a controlled unfinished-submission replay through shutdown and report that it exits, or adjust the shutdown path if it can remain blocked. ## Not verified The author's Windows exit-code-2 reproduction is reported in the PR description; I did not independently reproduce it. Exit code 3 and its pending draw-finisher behavior were not tested.
slopgobblercobbler commented 2026-10-31 19:18:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by slopgobblercobbler on 2026-10-31T19:18:38Z.

Review: COMMENTED


Audited the shutdown sequencing and Windows process exit lifecycle:

  • Windows ExitProcess deadlock resolution: The analysis of std::_Exit() on Windows is spot on. Win32 ExitProcess terminates all secondary worker threads before running static destructors during DLL teardown. When the static Driver destructor subsequently invoked stop(), attempting to join worker threads that had already been killed by the OS resulted in a permanent deadlock/hang. Calling AgcDriverShutdown_nid_postfix() explicitly in main ensures the driver workers are signaled via stopping = true and joined cleanly while still alive.
  • Shutdown sequencing on error exits: Placing shutdown in a dedicated try/catch block following run() ensures that any exceptions during shutdown are reported to stderr rather than dropped, correctly sets code = 2 if a nominally clean replay fails during teardown, and guarantees stdout/stderr are flushed before process exit.
  • Exit code 3 & DrawFinisher::Drain(): Audited DrawFinisher behavior in DrawPipeline.cpp: DrawFinisher jobs are CPU-side completion/retirement callbacks rather than GPU hardware fence waits. Because Driver::stop() halts and joins the driver worker threads before calling DrawFinisher::Get().Drain(), no new jobs can be enqueued while the finisher thread drains the remaining queue. Furthermore, Driver::stop() wraps DrawFinisher::Get().Drain() in try ... catch (...), preventing any trapped callback error from wedging driver termination.
  • Conventions: Verified python tools/check_conventions.py --base upstream/main --head HEAD passes cleanly with 0 findings.

Solid fix that cleanly eliminates the hung process issue on Windows replay errors.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5724494197 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14100#pullrequestreview-5724494197). Originally posted by [slopgobblercobbler](https://github.com/slopgobblercobbler) on 2026-10-31T19:18:38Z. **Review:** COMMENTED --- Audited the shutdown sequencing and Windows process exit lifecycle: - Windows `ExitProcess` deadlock resolution: The analysis of `std::_Exit()` on Windows is spot on. Win32 `ExitProcess` terminates all secondary worker threads before running static destructors during DLL teardown. When the static `Driver` destructor subsequently invoked `stop()`, attempting to join worker threads that had already been killed by the OS resulted in a permanent deadlock/hang. Calling `AgcDriverShutdown_nid_postfix()` explicitly in `main` ensures the driver workers are signaled via `stopping = true` and joined cleanly while still alive. - Shutdown sequencing on error exits: Placing shutdown in a dedicated `try/catch` block following `run()` ensures that any exceptions during shutdown are reported to `stderr` rather than dropped, correctly sets `code = 2` if a nominally clean replay fails during teardown, and guarantees `stdout`/`stderr` are flushed before process exit. - Exit code 3 & `DrawFinisher::Drain()`: Audited `DrawFinisher` behavior in `DrawPipeline.cpp`: `DrawFinisher` jobs are CPU-side completion/retirement callbacks rather than GPU hardware fence waits. Because `Driver::stop()` halts and joins the driver worker threads before calling `DrawFinisher::Get().Drain()`, no new jobs can be enqueued while the finisher thread drains the remaining queue. Furthermore, `Driver::stop()` wraps `DrawFinisher::Get().Drain()` in `try ... catch (...)`, preventing any trapped callback error from wedging driver termination. - Conventions: Verified `python tools/check_conventions.py --base upstream/main --head HEAD` passes cleanly with 0 findings. Solid fix that cleanly eliminates the hung process issue on Windows replay errors.
MarcZX10 commented 2026-10-31 21:18:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T21:18:54Z.

Review: APPROVED


Reviewed — looks good

At head 659a6eecb4b4c82c7f76c808f0ce3833c07fe204 against base 16452da93473aa42d8054da95f89e44a2a1d8d97, the requested unfinished-submission exit-code-3 path is now covered by the author's Windows RTX 3080 run (6/6 exits with code 3, with and without validation), and the hosted Windows job passes. I ran the no-build precheck, diff and conventions checks, and inspected the shutdown and frame-lifetime paths; I did not build or run tests locally. Linux/RADV evidence covers exit code 2, not exit code 3, and the reported hardware runs are author-provided rather than independently reproduced. The precheck's ABI/test-quality helpers were unavailable in its isolated checkout.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5724968265 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14100#pullrequestreview-5724968265). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-31T21:18:54Z. **Review:** APPROVED --- # Reviewed — looks good At head `659a6eecb4b4c82c7f76c808f0ce3833c07fe204` against base `16452da93473aa42d8054da95f89e44a2a1d8d97`, the requested unfinished-submission exit-code-3 path is now covered by the author's Windows RTX 3080 run (6/6 exits with code 3, with and without validation), and the hosted Windows job passes. I ran the no-build precheck, diff and conventions checks, and inspected the shutdown and frame-lifetime paths; I did not build or run tests locally. Linux/RADV evidence covers exit code 2, not exit code 3, and the reported hardware runs are author-provided rather than independently reproduced. The precheck's ABI/test-quality helpers were unavailable in its isolated checkout.
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
boykopovar/AnyPS5!14100
No description provided.