fix(agc): drop queued library prebuilds when the prebuild pool closes #8678

Merged
CPritch merged 3 commits from upstream-pr/8678 into main 2026-10-22 18:39:46 +00:00
CPritch commented 2026-10-20 20:52:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CPritch on 2026-10-20T20:52:21Z.


What

Driver::stop calls CloseRegistrationPreparation() before device.Reset(), which runs the teardown pipeline cache save. RegistrationPreparePool::Close() waited until its queue was empty, so with cold library caches the one-thread prebuild pool built every queued library before the process could exit (#8470).

When the prebuild pool closes it now drops the tasks that have not started, removes their tickets from unfinished and notifies idle, so a concurrent AwaitSubmitted does not wait for them, and waits for the running task as before. The shader preparation pool still runs its whole queue at close, and AwaitLibraryPrebuilds (device replacement) and Submit after close are unchanged, as MarcZX10 described on #8470. A dropped prebuild is submitted again when its shader registers in a later session, so after an early exit the library cache fills over more sessions than before.

agc_driver_shutdown_threads_queued_prebuilds gates one running prebuild, queues three more and closes on another thread. A second thread calls AwaitLibraryPrebuilds() before the close. The test requires that close waits for the running one and returns once it ends, that the waiter returns too, and that none of the queued ones ran. With the ticket erase removed, the waiter never returns and the test fails ("a waiter for the library prebuilds did not return after closing dropped the queued ones").

Related: #7474 saves the pipeline cache on an immediate guest exit through libc's exit drain; it does not touch Driver::stop or the pools.

Tested

GTA V (PPSA04263 01.010.002), Performance mode: boot, Resume Story, 60 s after the Story loads, then SIGINT (SDL turns it into the normal quit path). Every run started with no pipeline cache file and __GL_SHADER_DISK_CACHE=0, in the order main, PR, main, PR. Time from SIGINT to exit, all runs exited on their own and wrote the teardown save:

machine main this PR
Linux 7.2.8, RTX 5060 Laptop GPU, NVIDIA 615.71.09, main 44e3389c8 432 s, 630 s 2 s, 2 s
Linux 6.17, RTX 4060, NVIDIA 580.126.20, main 83751168d 859 s, 865 s 2 s, 2 s

Teardown save on the RTX 4060: main 275479.1 and 275620.2 KiB, this PR 65340.0 and 65024.0 KiB (it lacks the libraries main built from the queue after SIGINT). Boot to the loaded Story was the same on both sides (90 to 102 s).

On the laptop, ctest -R 'agc_driver_shutdown|agc_driver_prepared_graphics_prebuild' -V with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ANYPS5_NO_SHADER_CACHE=1, ANYPS5_REQUIRE_VULKAN=1 and VK_LOADER_DEBUG=error (loader 1.4.363 reports a missing layer only with it), on main 4036fab1b: main 11 of 11, this PR 12 of 12; no was not found but was requested by env var VK_INSTANCE_LAYERS or VUID- line. The shutdown thread tests do not load Vulkan; the three prebuild policy tests ran on the RTX 5060 with Khronos validation 1.4.363 loaded. The new test with main's ShaderRegistry.cpp fails with "closing ran the queued library prebuilds" (50 of 50); with this PR it passes 200 of 200. agc_driver_registration_prepare_stack (#6598) passes. On the RTX 4060 (main c6145997e), the nine agc_driver_shutdown_threads_* tests pass, and the new one passes 100 of 100 repeats.

Not tested: Windows, AMD, other titles.

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: Fixes #8470
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 5060 Laptop GPU, 615.71.09, Linux 7.2.8; NVIDIA GeForce RTX 4060, 580.126.20, Linux 6.17 (game runs only)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4942335871 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8678). Originally posted by [CPritch](https://github.com/CPritch) on 2026-10-20T20:52:21Z. --- ### What `Driver::stop` calls `CloseRegistrationPreparation()` before `device.Reset()`, which runs the teardown pipeline cache save. `RegistrationPreparePool::Close()` waited until its queue was empty, so with cold library caches the one-thread prebuild pool built every queued library before the process could exit (#8470). When the prebuild pool closes it now drops the tasks that have not started, removes their tickets from `unfinished` and notifies `idle`, so a concurrent `AwaitSubmitted` does not wait for them, and waits for the running task as before. The shader preparation pool still runs its whole queue at close, and `AwaitLibraryPrebuilds` (device replacement) and `Submit` after close are unchanged, as MarcZX10 described on #8470. A dropped prebuild is submitted again when its shader registers in a later session, so after an early exit the library cache fills over more sessions than before. `agc_driver_shutdown_threads_queued_prebuilds` gates one running prebuild, queues three more and closes on another thread. A second thread calls `AwaitLibraryPrebuilds()` before the close. The test requires that close waits for the running one and returns once it ends, that the waiter returns too, and that none of the queued ones ran. With the ticket erase removed, the waiter never returns and the test fails ("a waiter for the library prebuilds did not return after closing dropped the queued ones"). Related: #7474 saves the pipeline cache on an immediate guest exit through libc's exit drain; it does not touch `Driver::stop` or the pools. ### Tested GTA V (PPSA04263 01.010.002), Performance mode: boot, Resume Story, 60 s after the Story loads, then SIGINT (SDL turns it into the normal quit path). Every run started with no pipeline cache file and `__GL_SHADER_DISK_CACHE=0`, in the order main, PR, main, PR. Time from SIGINT to exit, all runs exited on their own and wrote the teardown save: | machine | main | this PR | |---|---|---| | Linux 7.2.8, RTX 5060 Laptop GPU, NVIDIA 615.71.09, main 44e3389c8 | 432 s, 630 s | 2 s, 2 s | | Linux 6.17, RTX 4060, NVIDIA 580.126.20, main 83751168d | 859 s, 865 s | 2 s, 2 s | Teardown save on the RTX 4060: main 275479.1 and 275620.2 KiB, this PR 65340.0 and 65024.0 KiB (it lacks the libraries main built from the queue after SIGINT). Boot to the loaded Story was the same on both sides (90 to 102 s). On the laptop, `ctest -R 'agc_driver_shutdown|agc_driver_prepared_graphics_prebuild' -V` with `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ANYPS5_NO_SHADER_CACHE=1`, `ANYPS5_REQUIRE_VULKAN=1` and `VK_LOADER_DEBUG=error` (loader 1.4.363 reports a missing layer only with it), on main 4036fab1b: main 11 of 11, this PR 12 of 12; no `was not found but was requested by env var VK_INSTANCE_LAYERS` or `VUID-` line. The shutdown thread tests do not load Vulkan; the three prebuild policy tests ran on the RTX 5060 with Khronos validation 1.4.363 loaded. The new test with main's `ShaderRegistry.cpp` fails with "closing ran the queued library prebuilds" (50 of 50); with this PR it passes 200 of 200. `agc_driver_registration_prepare_stack` (#6598) passes. On the RTX 4060 (main c6145997e), the nine `agc_driver_shutdown_threads_*` tests pass, and the new one passes 100 of 100 repeats. Not tested: Windows, AMD, other titles. ### 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: Fixes #8470 - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 5060 Laptop GPU, 615.71.09, Linux 7.2.8; NVIDIA GeForce RTX 4060, 580.126.20, Linux 6.17 (game runs only) - [x] AI-assisted: yes
MarcZX10 commented 2026-10-20 20:56:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T20:56:33Z.

Review: COMMENTED


The ticket removal and idle.notify_all() look consistent with AwaitSubmitted's predicate. The queued-prebuild test currently verifies the running task is joined and queued work is dropped, but it does not start an AwaitLibraryPrebuilds() waiter before close. Your #8470 comment said you planned to add that case; please include it so the waiter guarantee claimed in the PR description is protected against regression. I have not submitted an approval while that promised coverage is missing.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5591625564 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8678#pullrequestreview-5591625564). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-20T20:56:33Z. **Review:** COMMENTED --- The ticket removal and `idle.notify_all()` look consistent with `AwaitSubmitted`'s predicate. The queued-prebuild test currently verifies the running task is joined and queued work is dropped, but it does not start an `AwaitLibraryPrebuilds()` waiter before close. Your #8470 comment said you planned to add that case; please include it so the waiter guarantee claimed in the PR description is protected against regression. I have not submitted an approval while that promised coverage is missing.
MarcZX10 commented 2026-10-21 04:08:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T04:08:56Z.

Review: COMMENTED


One small test-race concern: waiting becomes true whenever the waiter future is not ready after 20 ms, including if the detached thread has not been scheduled yet. In that case the test does not prove AwaitLibraryPrebuilds() blocks while the running task is held. A start handshake immediately before the call would reduce the false-positive window; a test hook/barrier at the pool wait point would make the assertion definitive. The close-side assertions still exercise dropping queued work and joining the running task.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5594855331 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8678#pullrequestreview-5594855331). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T04:08:56Z. **Review:** COMMENTED --- One small test-race concern: `waiting` becomes true whenever the waiter future is not ready after 20 ms, including if the detached thread has not been scheduled yet. In that case the test does not prove `AwaitLibraryPrebuilds()` blocks while the running task is held. A start handshake immediately before the call would reduce the false-positive window; a test hook/barrier at the pool wait point would make the assertion definitive. The close-side assertions still exercise dropping queued work and joining the running task.
MarcZX10 commented 2026-10-21 05:54:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T05:54:53Z.

Review: COMMENTED


Thanks for adding the AwaitLibraryPrebuilds() waiter case and the dropped-ticket negative control. The first waiting assertion still has the scheduling race from my earlier note: after launching the detached thread, a 20 ms timeout only proves its future is not ready; it does not prove the thread reached AwaitLibraryPrebuilds() before CloseRegistrationPreparation() starts. A start promise set immediately before the call would make the test deterministic about thread scheduling; a hook at the pool wait point would prove the stronger condition directly. Can you add that handshake before I approve?

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5595501607 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8678#pullrequestreview-5595501607). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T05:54:53Z. **Review:** COMMENTED --- Thanks for adding the `AwaitLibraryPrebuilds()` waiter case and the dropped-ticket negative control. The first `waiting` assertion still has the scheduling race from my earlier note: after launching the detached thread, a 20 ms timeout only proves its future is not ready; it does not prove the thread reached `AwaitLibraryPrebuilds()` before `CloseRegistrationPreparation()` starts. A start promise set immediately before the call would make the test deterministic about thread scheduling; a hook at the pool wait point would prove the stronger condition directly. Can you add that handshake before I approve?
CPritch commented 2026-10-21 07:23:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CPritch on 2026-10-21T07:23:20Z.


@MarcZX10 added the start handshake in 8cd100e48: the waiter thread sets a promise immediately before calling AwaitLibraryPrebuilds(), and the test waits for it before the 20 ms check, so waiting can no longer pass because the thread hasn't been scheduled. A hook inside the pool's wait would make it exact, but it would need a test-only seam in RegistrationPreparePool, so I've left that out unless you'd prefer it. 200 of 200 repeats pass.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6310457867 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8678#issuecomment-6310457867). Originally posted by [CPritch](https://github.com/CPritch) on 2026-10-21T07:23:20Z. --- @MarcZX10 added the start handshake in 8cd100e48: the waiter thread sets a promise immediately before calling `AwaitLibraryPrebuilds()`, and the test waits for it before the 20 ms check, so `waiting` can no longer pass because the thread hasn't been scheduled. A hook inside the pool's wait would make it exact, but it would need a test-only seam in `RegistrationPreparePool`, so I've left that out unless you'd prefer it. 200 of 200 repeats pass. AI-assisted: yes
ycbuzi commented 2026-10-21 07:46:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T07:46:25Z.


Windows run of 8cd100e486 merged onto main b77bd58806 (f641bc3c31, clean), one of the hosts the description lists as not tested. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, BUILD_TESTING=ON. GPU: NVIDIA GeForce RTX 4070 SUPER, driver 616.92, picked with ANYPS5_GPU=4070 (the machine also has a GTX 1650 SUPER).

Built libSceAgcDriver and agc_driver_shutdown_threads_tests with no errors; the build prints two memcmp bound warnings, which main prints the same way from Graphics/src/PipelineLibrary.cpp, a file this PR does not change. ctest --repeat until-fail:50 on the new agc_driver_shutdown_threads_queued_prebuilds: passes all 50 repeats. --repeat until-fail:3 on the eight agc_driver_shutdown_threads_* tests: all pass. On the previous head 703102d6ca the nine shutdown tests also passed twice each. Not run: AMD, a title.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6310834797 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8678#issuecomment-6310834797). Originally posted by [ycbuzi](https://github.com/ycbuzi) on 2026-10-21T07:46:25Z. --- Windows run of 8cd100e486 merged onto `main` b77bd58806 (f641bc3c31, clean), one of the hosts the description lists as not tested. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, `BUILD_TESTING=ON`. GPU: NVIDIA GeForce RTX 4070 SUPER, driver 616.92, picked with `ANYPS5_GPU=4070` (the machine also has a GTX 1650 SUPER). Built `libSceAgcDriver` and `agc_driver_shutdown_threads_tests` with no errors; the build prints two `memcmp` bound warnings, which `main` prints the same way from `Graphics/src/PipelineLibrary.cpp`, a file this PR does not change. `ctest --repeat until-fail:50` on the new `agc_driver_shutdown_threads_queued_prebuilds`: passes all 50 repeats. `--repeat until-fail:3` on the eight `agc_driver_shutdown_threads_*` tests: all pass. On the previous head 703102d6ca the nine shutdown tests also passed twice each. Not run: AMD, a title.
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!8678
No description provided.