fix(agc): drain the registration preparation pool before the device is reset #2976

Merged
SleglJan merged 1 commit from upstream-pr/2976 into main 2026-10-10 23:44:41 +00:00
SleglJan commented 2026-10-10 19:16:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-10T19:16:58Z.


What

Carried from #2909 by @queeeef, which the intake pause closed; queeeef agreed to the carry there. The commit 8153d5ab is unchanged (author Daan Dehing). It is based on main 57f96fd2 and merges cleanly with main 4a747c48; none of the commits since its parent touch its four files. I'll handle the review follow-ups.

Fixes #2891. Background registration preparations (#2583) copy the device's SpirvTarget, whose capability and extension spans point into the device's shader profile. Driver::stop joined the queue workers and reset the device without waiting for the detached RegistrationPreparePool, so a preparation still queued or running read freed extension names in RecompileCacheKey::appendInterface. This is the agc_driver crash at exit on RADV that #2891 and the review of #2589 report.

  • RegistrationPreparePool::Close stops queueing and waits until no task is queued or running. Driver::stop calls it (CloseRegistrationPreparation) after joining the queue workers and before device.Reset(). The pool counts running tasks and destroys each task's captures before it reports idle.
  • A preparation submitted after the pool is closed, by a registration racing shutdown, runs on the caller's thread. This is the pool's existing fallback for when no thread could be created. RegisterShader holds its own shared_ptr to the device for the duration, so the spans stay valid. Throwing ProcessShutdown there instead would also be possible; happy to switch if preferred.
  • PrepareRegisteredInBackground submits through SubmitRegistrationPreparation, which the test also uses.

This is the drain Celegans12 asked for in a comment on #2583 ("Could the pool be included in shutdown").

Overlap: #2856 conflicts trivially in ShaderRegistry.hpp, where both add an #include and a declaration next to each other. #2589 adds more submissions to the same pool and already conflicts with main.

Tested

queeeef's results:

Linux (Ubuntu 24.04, GCC 13.3), lavapipe (Mesa 25.2.8), main at 57f96fd. Full build with -DANYPS5_ENABLE_SPIRV_TOOLS=ON and the CI ctest invocation (--parallel 4 --timeout 120).

  • New agc_driver_registration_prepare_shutdown (0.2 s, no GPU needed): a latch-based shutdown test. A task on the preparation pool blocks on a latch while another thread runs LibcRunShutdown_nid_postfix. Shutdown must not return before the latch is released and the task has finished. A preparation submitted after shutdown must run on the caller's thread. It fails without the Close call in Driver::stop ("shutdown returned while a registration preparation was still running"). It also fails when Close ignores running tasks, and when Submit ignores the closed pool. It passes 30 of 30 repeated runs.
  • 701 of 702 tests pass. The one failure, guest_avplayer (links only libSceAvPlayer and libc), aborted once under parallel load and passed 10 of 10 reruns alone and 10 of 10 under GPU load; it is unrelated. agc_driver_flat_store is the only test over 30 s (50 s), as on main.
  • agc_driver_tests: 0 of 30 runs fail. It did not crash on main there either; the crash has only been reproduced on RADV.
  • tools/check_conventions.py and tools/check_test_registration.py pass.

Rerun of the #2891 loop on RADV (#2909 comment): Linux (CachyOS, kernel 7.2.9, GCC 16.2.1), Release with BUILD_TESTING=ON and ANYPS5_ENABLE_SPIRV_TOOLS=ON, RADV (Mesa 26.2.4, Ryzen 9 7950X3D iGPU, gfx1036) and RTX 3080 (NVIDIA 615.71.09). ctest -R '^agc_driver$' ran alone, one run at a time:

runs that fail
RADV, main 57f96fd2 (the commit's parent) 26 of 30
RADV, 8153d5ab 0 of 30
RTX 3080, 8153d5ab 0 of 10 (main was 0 of 8 there in #2891, so this only checks for regressions)

agc_driver_registration_prepare_shutdown passes 30 of 30 there too.

Windows (Yuva1024 on #2891, main 1476f769, RTX 4060, 4 copies in parallel): a rare heap-corruption exit of agc_driver_tests still happens with this change (1 of 400 runs, against 1 of 240 on main). So this change doesn't fix it, and those runs can't tell whether it lowers it. That exit needs its own issue, since this PR closes #2891.

@adriadam10, could you rerun your loop (10 of 30 on #2589) with this branch?

Checklist

  • Based on main 57f96fd2, merges cleanly with 4a747c48; 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 in TechnicalDebt
  • 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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes (Claude Code; queeeef reviewed and tested the change, and Claude Code helped with this carry)

🤖 Generated with Claude Code

https://claude.ai/code/session_01Fnrk5Dazjb2tLooVSpdRQq

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4813785502 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2976). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-10T19:16:58Z. --- ### What Carried from #2909 by @queeeef, which the intake pause closed; queeeef agreed to the carry there. The commit 8153d5ab is unchanged (author Daan Dehing). It is based on `main` 57f96fd2 and merges cleanly with `main` 4a747c48; none of the commits since its parent touch its four files. I'll handle the review follow-ups. Fixes #2891. Background registration preparations (#2583) copy the device's `SpirvTarget`, whose capability and extension spans point into the device's shader profile. `Driver::stop` joined the queue workers and reset the device without waiting for the detached `RegistrationPreparePool`, so a preparation still queued or running read freed extension names in `RecompileCacheKey::appendInterface`. This is the `agc_driver` crash at exit on RADV that #2891 and the review of #2589 report. - `RegistrationPreparePool::Close` stops queueing and waits until no task is queued or running. `Driver::stop` calls it (`CloseRegistrationPreparation`) after joining the queue workers and before `device.Reset()`. The pool counts running tasks and destroys each task's captures before it reports idle. - A preparation submitted after the pool is closed, by a registration racing shutdown, runs on the caller's thread. This is the pool's existing fallback for when no thread could be created. `RegisterShader` holds its own `shared_ptr` to the device for the duration, so the spans stay valid. Throwing `ProcessShutdown` there instead would also be possible; happy to switch if preferred. - `PrepareRegisteredInBackground` submits through `SubmitRegistrationPreparation`, which the test also uses. This is the drain Celegans12 asked for in a comment on #2583 ("Could the pool be included in shutdown"). Overlap: #2856 conflicts trivially in `ShaderRegistry.hpp`, where both add an `#include` and a declaration next to each other. #2589 adds more submissions to the same pool and already conflicts with `main`. ### Tested queeeef's results: Linux (Ubuntu 24.04, GCC 13.3), lavapipe (Mesa 25.2.8), main at 57f96fd. Full build with `-DANYPS5_ENABLE_SPIRV_TOOLS=ON` and the CI ctest invocation (`--parallel 4 --timeout 120`). - New `agc_driver_registration_prepare_shutdown` (0.2 s, no GPU needed): a latch-based shutdown test. A task on the preparation pool blocks on a latch while another thread runs `LibcRunShutdown_nid_postfix`. Shutdown must not return before the latch is released and the task has finished. A preparation submitted after shutdown must run on the caller's thread. It fails without the `Close` call in `Driver::stop` ("shutdown returned while a registration preparation was still running"). It also fails when `Close` ignores running tasks, and when `Submit` ignores the closed pool. It passes 30 of 30 repeated runs. - 701 of 702 tests pass. The one failure, `guest_avplayer` (links only `libSceAvPlayer` and `libc`), aborted once under parallel load and passed 10 of 10 reruns alone and 10 of 10 under GPU load; it is unrelated. `agc_driver_flat_store` is the only test over 30 s (50 s), as on main. - `agc_driver_tests`: 0 of 30 runs fail. It did not crash on `main` there either; the crash has only been reproduced on RADV. - `tools/check_conventions.py` and `tools/check_test_registration.py` pass. Rerun of the #2891 loop on RADV (#2909 comment): Linux (CachyOS, kernel 7.2.9, GCC 16.2.1), Release with `BUILD_TESTING=ON` and `ANYPS5_ENABLE_SPIRV_TOOLS=ON`, RADV (Mesa 26.2.4, Ryzen 9 7950X3D iGPU, gfx1036) and RTX 3080 (NVIDIA 615.71.09). `ctest -R '^agc_driver$'` ran alone, one run at a time: | | runs that fail | |---|---| | RADV, `main` 57f96fd2 (the commit's parent) | 26 of 30 | | RADV, 8153d5ab | 0 of 30 | | RTX 3080, 8153d5ab | 0 of 10 (`main` was 0 of 8 there in #2891, so this only checks for regressions) | `agc_driver_registration_prepare_shutdown` passes 30 of 30 there too. Windows (Yuva1024 on #2891, `main` 1476f769, RTX 4060, 4 copies in parallel): a rare heap-corruption exit of `agc_driver_tests` still happens with this change (1 of 400 runs, against 1 of 240 on `main`). So this change doesn't fix it, and those runs can't tell whether it lowers it. That exit needs its own issue, since this PR closes #2891. @adriadam10, could you rerun your loop (10 of 30 on #2589) with this branch? ### Checklist - [x] Based on `main` 57f96fd2, merges cleanly with 4a747c48; 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [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 (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes (Claude Code; queeeef reviewed and tested the change, and Claude Code helped with this carry) 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01Fnrk5Dazjb2tLooVSpdRQq
adriadam10 commented 2026-10-10 23:39:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T23:39:48Z.


Rebased onto current main (only conflict: adjacent #include <functional> / <initializer_list> in ShaderRegistry.hpp).

  • Build + AGC/AgcDriver ctest scope pass on the rebased tree; agc_driver_registration_prepare_shutdown, agc_driver and the prepared_* tests pass 10/10 repeats.
  • The new test does not build without the change.
  • Remaining Windows heap-corruption exit (1/400) is not covered here, as the description says; please open a separate issue for it.

Merging on green CI.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103399898 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2976#issuecomment-6103399898). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T23:39:48Z. --- Rebased onto current `main` (only conflict: adjacent `#include <functional>` / `<initializer_list>` in `ShaderRegistry.hpp`). - Build + AGC/AgcDriver ctest scope pass on the rebased tree; `agc_driver_registration_prepare_shutdown`, `agc_driver` and the `prepared_*` tests pass 10/10 repeats. - The new test does not build without the change. - Remaining Windows heap-corruption exit (1/400) is not covered here, as the description says; please open a separate issue for it. Merging on green CI.
SleglJan commented 2026-10-11 00:02:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-11T00:02:07Z.


Opened #3171 for the remaining Windows heap-corruption exit, with @Yuva1024's and @queeeef's data and the page-heap suggestion. Thanks for the review and the rebase.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103557503 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2976#issuecomment-6103557503). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-11T00:02:07Z. --- Opened #3171 for the remaining Windows heap-corruption exit, with @Yuva1024's and @queeeef's data and the page-heap suggestion. Thanks for the review and the rebase. AI-assisted: yes (Claude Code)
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!2976
No description provided.