fix(videoout): end shutdown when a host thread cannot unwind #16791

Closed
wanderingbug wants to merge 3 commits from upstream-pr/16791 into main
wanderingbug commented 2026-11-07 16:43:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wanderingbug on 2026-11-07T16:43:26Z.


What

Open #16801 (fix/macos-test-compile, 38bddbff44) edits only ResourceRequest.cpp, RtcpTransportFeedback.cpp, and GuestCesGbSetters.cpp: <initializer_list> and ResourceRequestBase value initialization. None of those symbols are in this pull request.

If a host thread cannot unwind out of the guest, shutdown still finishes instead of leaving that thread to hold the process. The abandoned path is guest_shutdown_abandoned. Queue submission, the driver lifecycle, and the video-out driver take part in that ending.

This replaces closed #12113. That patch also changed DisplayBuffer.hpp, VulkanDevice.cpp, and VideoOut Buffer.cpp. This branch does not. Test-only #7804 and #4630 were in the Mac tree and are not code dependencies of this branch.

Tested

guest_shutdown_abandoned exited 0 in the same cache-reuse run: Apple M2 Max, macOS 27.2, x86_64 under Rosetta, snapshot 7a6e44ece4 plus the six overlays listed on #16790. This head was not rebuilt alone. The square smoke also reached process exit on that machine, which is not the Vulkan validation comparison CONTRIBUTING.md asks for. Not tested on a second machine; no game tested.

Checklist

  • Based on current main; no other open PR implements the same functions. Unchecked: parent is 30a863d12d, and upstream main is now daad891ea6. #16801 does not implement these functions. Other open pull requests were not fully scanned.
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt. The diff adds no // comments. The debt note is in docs/dev/debt/libSceVideoOut.md.
  • Unimplemented paths throw. Unchecked: the new shutdown path was not re-audited line by line in this pass.
  • No notes, investigation .md files or images added to the repository. Existing development documentation is edited; maintainer confirmation of this rule is still needed
  • New third-party code is a submodule built from source. No 3rdparty/ change.
  • Generic behaviour, not specific to one title
  • Depends on: none. Test-only #7804 and #4630 were in the Mac tree and are not in this branch.
  • Fixes: none found in this pass. No issue search.
  • Unblocks: none claimed. No title bring-up check.
  • GPUs tested: Apple M2 Max ran the combined MoltenVK 1.4.2 square smoke. Unchecked because this changes VideoOut and the AGC driver, and the validation-layer comparison with main was not run.
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5203778800 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16791). Originally posted by [wanderingbug](https://github.com/wanderingbug) on 2026-11-07T16:43:26Z. --- ### What Open #16801 (`fix/macos-test-compile`, `38bddbff44`) edits only `ResourceRequest.cpp`, `RtcpTransportFeedback.cpp`, and `GuestCesGbSetters.cpp`: `<initializer_list>` and `ResourceRequestBase` value initialization. None of those symbols are in this pull request. If a host thread cannot unwind out of the guest, shutdown still finishes instead of leaving that thread to hold the process. The abandoned path is `guest_shutdown_abandoned`. Queue submission, the driver lifecycle, and the video-out driver take part in that ending. This replaces closed #12113. That patch also changed `DisplayBuffer.hpp`, `VulkanDevice.cpp`, and `VideoOut` `Buffer.cpp`. This branch does not. Test-only #7804 and #4630 were in the Mac tree and are not code dependencies of this branch. ### Tested `guest_shutdown_abandoned` exited 0 in the same cache-reuse run: Apple M2 Max, macOS 27.2, x86_64 under Rosetta, snapshot `7a6e44ece4` plus the six overlays listed on #16790. This head was not rebuilt alone. The square smoke also reached process exit on that machine, which is not the Vulkan validation comparison `CONTRIBUTING.md` asks for. Not tested on a second machine; no game tested. ### Checklist - [ ] Based on current `main`; no other open PR implements the same functions. Unchecked: parent is `30a863d12d`, and upstream `main` is now `daad891ea6`. #16801 does not implement these functions. Other open pull requests were not fully scanned. - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt. The diff adds no `//` comments. The debt note is in `docs/dev/debt/libSceVideoOut.md`. - [ ] Unimplemented paths throw. Unchecked: the new shutdown path was not re-audited line by line in this pass. - [ ] No notes, investigation `.md` files or images added to the repository. Existing development documentation is edited; maintainer confirmation of this rule is still needed - [x] New third-party code is a submodule built from source. No `3rdparty/` change. - [x] Generic behaviour, not specific to one title - [x] Depends on: none. Test-only #7804 and #4630 were in the Mac tree and are not in this branch. - [ ] Fixes: none found in this pass. No issue search. - [ ] Unblocks: none claimed. No title bring-up check. - [ ] GPUs tested: Apple M2 Max ran the combined MoltenVK 1.4.2 square smoke. Unchecked because this changes VideoOut and the AGC driver, and the validation-layer comparison with `main` was not run. - [x] AI-assisted: yes
gabichuuuuu commented 2026-11-07 17:45:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T17:45:26Z.

Review: COMMENTED


Code review of head 9c37fd53ac. I read the diff and the code it calls; I didn't build or run it (this is a Windows machine and the new test is fork-based, under if(UNIX)).

1. guest_shutdown_abandoned can't pass as registered. Nothing defines SHUTDOWN_JOIN_TEST_STUB (not libc/CMakeLists.txt, not the test), so the test links the real libc. In the forked child, LateJoinCase passes LibcExitCode_nid_no_patch() while exitCode is still its initial 0, and when the worker notes the abandonment, JoinWhenReturnedOrEndIfAbandoned calls LibcTerminate_nid_no_patch(0), which takes the new hostShutdownAbandoned branch and does std::_Exit(0). The parent then requires WEXITSTATUS(status) == 42 and aborts. The 42 only exists in the stub branch. Also, add_test passes no argument, so the lock and join modes never run on their own. The linux job is skipped on this PR, so CI won't show it either.

Either set a known exit code in the child before LateJoinCase (for example through LibcRequestExit_nid_postfix(42) or whatever path really records it) or drop the stub block and expect 0; and register add_test(... lock) if LockCase should be covered.

2. Driver::stop() can now wait forever on a worker that never entered run(). worker.exited is only set by the Exited guard inside Driver::run(). The thread lambda in Driver::enqueue (Submission.cpp, unchanged) returns before run(queue) when RaiseWorkerThreadPriority throws, which it does when APS5_THREAD_PRIORITY is invalid or a requested priority is refused. That thread is joinable with exited == false, so JoinWhenReturnedOrEndIfAbandoned loops on its 20 ms sleep while !returned, and on Linux and Windows hostShutdownAbandoned is never set, so it never ends. Before this change thread.join() returned at once. Setting the flag in the lambda covers every exit and removes the workers.at(id) lookup under mutex in run():

if (!worker.thread.joinable()) worker.thread = std::thread([this, queue, exited = &worker.exited] {
    struct Exited {
        std::atomic<bool>* flag;
        ~Exited() noexcept { flag->store(true, std::memory_order_release); }
    } mark{exited};
    char role[32];
    std::snprintf(role, sizeof(role), "queue 0x%x worker", queue);
    try {
        RaiseWorkerThreadPriority(role);
    } catch (...) {
        ReportFailure(std::current_exception());
        return;
    }
    run(queue);
});

(workers is a std::map, so the address stays valid.)

3. Minor: a second exit() overwrites the recorded code. LibcRequestExit_nid_postfix stores exitCode before exitRequested.exchange(true), so if thread A calls exit(1) and thread B exit(2) after it, stop() and VideoOutDriver::Shutdown read 2, and the abandoned path ends with _Exit(2) while the exit that actually runs is 1. Storing after the exchange check keeps the first one.

4. Minor: JoinOrEndIfHostAbandoned is only used by the test; production code uses JoinWhenReturnedOrEndIfAbandoned.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5814077606 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16791#pullrequestreview-5814077606). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-07T17:45:26Z. **Review:** COMMENTED --- Code review of head `9c37fd53ac`. I read the diff and the code it calls; I didn't build or run it (this is a Windows machine and the new test is `fork`-based, under `if(UNIX)`). **1. `guest_shutdown_abandoned` can't pass as registered.** Nothing defines `SHUTDOWN_JOIN_TEST_STUB` (not `libc/CMakeLists.txt`, not the test), so the test links the real `libc`. In the forked child, `LateJoinCase` passes `LibcExitCode_nid_no_patch()` while `exitCode` is still its initial `0`, and when the worker notes the abandonment, `JoinWhenReturnedOrEndIfAbandoned` calls `LibcTerminate_nid_no_patch(0)`, which takes the new `hostShutdownAbandoned` branch and does `std::_Exit(0)`. The parent then requires `WEXITSTATUS(status) == 42` and aborts. The `42` only exists in the stub branch. Also, `add_test` passes no argument, so the `lock` and `join` modes never run on their own. The `linux` job is skipped on this PR, so CI won't show it either. Either set a known exit code in the child before `LateJoinCase` (for example through `LibcRequestExit_nid_postfix(42)` or whatever path really records it) or drop the stub block and expect `0`; and register `add_test(... lock)` if `LockCase` should be covered. **2. `Driver::stop()` can now wait forever on a worker that never entered `run()`.** `worker.exited` is only set by the `Exited` guard inside `Driver::run()`. The thread lambda in `Driver::enqueue` (`Submission.cpp`, unchanged) returns before `run(queue)` when `RaiseWorkerThreadPriority` throws, which it does when `APS5_THREAD_PRIORITY` is invalid or a requested priority is refused. That thread is joinable with `exited == false`, so `JoinWhenReturnedOrEndIfAbandoned` loops on its 20 ms sleep while `!returned`, and on Linux and Windows `hostShutdownAbandoned` is never set, so it never ends. Before this change `thread.join()` returned at once. Setting the flag in the lambda covers every exit and removes the `workers.at(id)` lookup under `mutex` in `run()`: ```cpp if (!worker.thread.joinable()) worker.thread = std::thread([this, queue, exited = &worker.exited] { struct Exited { std::atomic<bool>* flag; ~Exited() noexcept { flag->store(true, std::memory_order_release); } } mark{exited}; char role[32]; std::snprintf(role, sizeof(role), "queue 0x%x worker", queue); try { RaiseWorkerThreadPriority(role); } catch (...) { ReportFailure(std::current_exception()); return; } run(queue); }); ``` (`workers` is a `std::map`, so the address stays valid.) **3. Minor: a second `exit()` overwrites the recorded code.** `LibcRequestExit_nid_postfix` stores `exitCode` before `exitRequested.exchange(true)`, so if thread A calls `exit(1)` and thread B `exit(2)` after it, `stop()` and `VideoOutDriver::Shutdown` read 2, and the abandoned path ends with `_Exit(2)` while the exit that actually runs is 1. Storing after the `exchange` check keeps the first one. **4. Minor:** `JoinOrEndIfHostAbandoned` is only used by the test; production code uses `JoinWhenReturnedOrEndIfAbandoned`. AI-assisted: yes.
adriadam10 commented 2026-11-11 10:21:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-11-11T10:21:28Z.

Review: CHANGES_REQUESTED


Checked the diff against Submission.cpp and the PR thread (including the reviews of @gabichuuuuu and @adam25136).

  • core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/QueueWorker.cpp:157: exited is only set by the guard inside Driver::run(). The thread lambda in Submission.cpp:808-817 returns before run(queue) when RaiseWorkerThreadPriority throws, so that worker is joinable with exited == false. Driver::stop() then loops in JoinWhenReturnedOrEndIfAbandoned forever (the abandoned flag is never set outside macOS), where main joined at once. @adam25136 reproduced it: agc_driver_wait_shutdown with APS5_THREAD_PRIORITY=bogus times out 3 of 3 with this PR and passes on main. Fix: put the Exited guard in the thread lambda (before RaiseWorkerThreadPriority), passing &worker.exited, which also removes the workers.at(id) lookup under mutex in run(). workers is a std::map, so the address is stable.
  • Please add a test for that case (a worker that returns before run() must not block stop()); the current tests only exercise the normal path.
  • The PR body: the conventions check fails. Tick every checklist box and fill Fixes/Unblocks/GPUs tested as the template asks.
  • ShutdownJoin.hpp:19-25: JoinOrEndIfHostAbandoned is only used by the test; drop it or use it.
  • Process.cpp:76: LibcRequestExit_nid_postfix stores exitCode before the exchange, so a second exit() overwrites the code of the exit that actually runs. Store it only when exchange returns false.

The first point of the earlier review (exit status 42 in the abandoned-join test) is already fixed by ed16d0fb.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5857217645 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16791#pullrequestreview-5857217645). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-11-11T10:21:28Z. **Review:** CHANGES_REQUESTED --- Checked the diff against `Submission.cpp` and the PR thread (including the reviews of @gabichuuuuu and @adam25136). - `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/QueueWorker.cpp:157`: `exited` is only set by the guard inside `Driver::run()`. The thread lambda in `Submission.cpp:808-817` returns before `run(queue)` when `RaiseWorkerThreadPriority` throws, so that worker is joinable with `exited == false`. `Driver::stop()` then loops in `JoinWhenReturnedOrEndIfAbandoned` forever (the abandoned flag is never set outside macOS), where `main` joined at once. @adam25136 reproduced it: `agc_driver_wait_shutdown` with `APS5_THREAD_PRIORITY=bogus` times out 3 of 3 with this PR and passes on `main`. Fix: put the `Exited` guard in the thread lambda (before `RaiseWorkerThreadPriority`), passing `&worker.exited`, which also removes the `workers.at(id)` lookup under `mutex` in `run()`. `workers` is a `std::map`, so the address is stable. - Please add a test for that case (a worker that returns before `run()` must not block `stop()`); the current tests only exercise the normal path. - The PR body: the `conventions` check fails. Tick every checklist box and fill `Fixes`/`Unblocks`/`GPUs tested` as the template asks. - `ShutdownJoin.hpp:19-25`: `JoinOrEndIfHostAbandoned` is only used by the test; drop it or use it. - `Process.cpp:76`: `LibcRequestExit_nid_postfix` stores `exitCode` before the `exchange`, so a second `exit()` overwrites the code of the exit that actually runs. Store it only when `exchange` returns false. The first point of the earlier review (exit status 42 in the abandoned-join test) is already fixed by `ed16d0fb`.
MarcZX10 commented 2026-11-21 05:02:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T05:02:12Z.

Review: APPROVED


Reviewed — looks good

Nothing in the inspected changes establishes a blocking defect; shutdown coordination is handled by tracking worker return and ending the process when an abandoned host thread prevents progress.

What I ran

  • Read the prepared diff and source at head 07b9df164e406fdb88157d38cf052657d14333e9 using the pinned repo_read.py reader.
  • No build or test command was run; the description reports an author run of guest_shutdown_abandoned and a square smoke on Apple M2 Max, macOS 27.2, x86_64 under Rosetta, using a combined snapshot and overlays.

Results

Check Result Note
Source review passed Shutdown and unwind changes inspected
Build and tests not run Author-reported run only

Findings

Blocking

None.

Non-blocking

  • The test’s LateJoinCase never sets returned, so it exercises the abandoned-flag exit path rather than a worker-return race. A focused case that makes the worker return and sets the flag would strengthen coverage of JoinWhenReturnedOrEndIfAbandoned.
  • The description says this head was not rebuilt alone and the Vulkan validation comparison with main was not run. Consider adding that comparison before relying on the reported smoke for video-out behavior.

Not verified

  • No build, test, or runtime checks were performed in this review. The reported test and smoke were not independently verified.
  • The change does not claim physical-console testing; no title testing or second-machine run is reported.
  • Read all 13 changed files listed in the prepared manifest, including the changed lines and targeted surrounding context. The supplied manifest marks all changed files as included; no changed files were excluded by rule.

To get this to a good state

Nothing is required. The two points above are optional follow-up feedback.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6002962240 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16791#pullrequestreview-6002962240). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T05:02:12Z. **Review:** APPROVED --- # Reviewed — looks good Nothing in the inspected changes establishes a blocking defect; shutdown coordination is handled by tracking worker return and ending the process when an abandoned host thread prevents progress. ## What I ran - Read the prepared diff and source at head `07b9df164e406fdb88157d38cf052657d14333e9` using the pinned `repo_read.py` reader. - No build or test command was run; the description reports an author run of `guest_shutdown_abandoned` and a square smoke on Apple M2 Max, macOS 27.2, x86_64 under Rosetta, using a combined snapshot and overlays. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Shutdown and unwind changes inspected | | Build and tests | not run | Author-reported run only | ## Findings ### Blocking None. ### Non-blocking - The test’s `LateJoinCase` never sets `returned`, so it exercises the abandoned-flag exit path rather than a worker-return race. A focused case that makes the worker return and sets the flag would strengthen coverage of `JoinWhenReturnedOrEndIfAbandoned`. - The description says this head was not rebuilt alone and the Vulkan validation comparison with `main` was not run. Consider adding that comparison before relying on the reported smoke for video-out behavior. ## Not verified - No build, test, or runtime checks were performed in this review. The reported test and smoke were not independently verified. - The change does not claim physical-console testing; no title testing or second-machine run is reported. - Read all 13 changed files listed in the prepared manifest, including the changed lines and targeted surrounding context. The supplied manifest marks all changed files as included; no changed files were excluded by rule. ## To get this to a good state Nothing is required. The two points above are optional follow-up feedback.

Pull request closed

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!16791
No description provided.