Skip to content

fix(libc): run guest finalizers before driver shutdown - #5978

Merged
adriadam10 merged 3 commits into
boykopovar:mainfrom
MarcZX10:codex/libc-explicit-exit-finalizers-5931
Oct 16, 2026
Merged

adriadam10 merged 3 commits into
boykopovar:mainfrom
MarcZX10:codex/libc-explicit-exit-finalizers-5931

Conversation

@MarcZX10

@MarcZX10 MarcZX10 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

What

For an explicit guest exit() or return from main, run the guest's C++ finalizers before the AGC/VideoOut shutdown callbacks. This keeps those drivers available to finalizers that use them. The shutdown callbacks still run once, after the finalizers, including when a finalizer calls nested exit().

This is the explicit guest-exit path from #5931. Host window-close requests continue through #5843's no-handler path. Depends on #5843 (which depends on #5236).

Tested

  • Rebased onto current main 8cd937e29; git diff --check and the template-aware conventions check pass.
  • Debian 12 / GCC 12.2 / Ninja Release with testing enabled: configured a fresh build and built tests/guest_system_exit_tests successfully (712 build steps, including libc.prx, libSceSystemService.prx, and the test executable).
  • Ran guest_exit_finalizers_before_shutdown, guest_system_exit, guest_return_from_main, guest_system_relaunch, and guest_nested_exit. All five processes aborted before test logic at the emulator host-CPU guard: this Xeon E5-1620 v2 lacks LZCNT, BMI1/2, MOVBE, AVX2, and FMA. No CTest case is reported as passing on this host.
  • A focused Process.cpp probe passed on the pre-rebase head; it has not been rerun on this rebased head. No Windows or title runtime was tested.

Checklist

@ahmad3a4

Copy link
Copy Markdown
Contributor

Windows validation of #5843 + this PR, from the #5931 report:

Setup: Windows 11 Pro (26200), Core i5-13400 (passes the CPU guard), WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. main ed835aa, then #5843 (e0748f3, carrying #5236 as 9655d0c), then this PR (c6b42bf) merged on top.

Conflicts when this PR goes on top of #5843:

  • Process.cpp: fix(libc): skip guest exit handlers on host window close #5843 has StartExit() running LibcRunShutdown_nid_postfix(), and this PR has BeginExit() without it. I kept the name StartExit, took the shutdown out of it, and called LibcRunShutdown_nid_postfix() in LibcExitWithoutHandlers_nid_no_patch before the flush. Without that call, a resolution that just takes this PR's version would leave the window-close path terminating without running the AGC/VideoOut shutdown callbacks.
  • libSceSystemService/CMakeLists.txt: I kept fix(libc): skip guest exit handlers on host window close #5843's guest_system_exit/guest_requested_exit expectations ("Guest shutdown completed", plus the FAIL expression), added guest_exit_finalizers_before_shutdown, and put it in the Windows PATH list as well.

Results:

  • All 10 exit tests pass: guest_system_exit, guest_requested_exit, guest_exit_finalizers_before_shutdown, guest_nested_exit, guest_return_from_main, guest_quick_exit, guest_immediate_exit, guest_system_relaunch, guest_system_exit_parameters and guest_thread_atexit.
  • Syobon Action (PPSA47402, Syobon Action (PPSA47402) #5932) closed with WM_CLOSE after it presents 300 to 600 frames: 3 of 3 runs end with exit code 0 and no abort. On main e34c479 it was 4 of 4 exit thread cannot wait for itself aborts with 0xC0000409.
  • That close goes through fix(libc): skip guest exit handlers on host window close #5843's no-handler path. This PR's own path is covered here only by guest_exit_finalizers_before_shutdown, since Syobon has no exit of its own.

Linux not run.

AI-assisted.

@github-actions github-actions Bot added libc build prx needs rebase Conflicts with main, rebase needed labels Oct 14, 2026
@MarcZX10
MarcZX10 force-pushed the codex/libc-explicit-exit-finalizers-5931 branch from c6b42bf to f619f96 Compare October 15, 2026 00:55
@MarcZX10

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (8cd937e29) and resolved the exit-path conflicts. The current StartExit() preserves the newer host-window-close flow; explicit LibcExit_nid_no_patch() runs guest C++ finalizers before LibcRunShutdown_nid_postfix(), while LibcExitWithoutHandlers_nid_no_patch() still runs shutdown callbacks before terminating. The new finalizer-order test and existing guest exit/relaunch/nested-exit tests compile. I ran those five CTests, but all abort at the existing host CPU feature guard before their test bodies on this Xeon E5-1620 v2, so I am not claiming a passing runtime test here. Current conventions check passes; hosted checks are running.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

Hosted checks have now finished: Ubuntu, Windows and macOS relinker jobs all pass; register-context and write-watch jobs pass as well. Linux and Windows full-build jobs were skipped by path filters. The five local exit CTests still stop at this host’s CPU-feature guard before their test bodies, so I am not claiming a local runtime pass. The Windows title results earlier in this thread remain @ahmad3a4’s separate validation of the prior stacked head.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 15, 2026
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 6044 core/libs/prx/libSceSystemService/CMakeLists.txt
core/libs/tests/GuestSystemExit.cpp
PR 6447 core/libs/prx/libc/src/Process.cpp

@milescoviello

Copy link
Copy Markdown
Contributor

The CTest half, on a host with AVX2 (Ubuntu 24.04, i7-13700K, GCC 13.3, Release), main ab9a476 with #5843 and this PR merged. The only conflict was the test registrations in libSceSystemService/CMakeLists.txt, where I kept both sides. All nine exit tests pass:

guest_system_exit, guest_requested_exit, guest_system_exit_parameters, guest_quick_exit, guest_immediate_exit, guest_return_from_main, guest_system_relaunch, guest_nested_exit and the new guest_exit_finalizers_before_shutdown.

With Syobon Action (PPSA47402), closing the window 20 s in exits 0 with #5843 alone and with both PRs; main aborts with exit thread cannot wait for itself. I couldn't drive the title's own exit() path from outside, so that part of this PR isn't covered by the title run.

AI-assisted: yes

@dogukanatlihan

Copy link
Copy Markdown
Contributor

I prepared a small supplemental regression for the nested-exit coverage request. It extends guest_nested_exit to require a live shutdown token in the remaining finalizer, exactly one shutdown marker afterward, and the existing nested exit status 7.

Windows 11 Pro build 26300, i7-14700K, WinLibs GCC 15.2.0 r7, Release:

  • Negative control with pre-fix libc: child exits 8 and records only shutdown; the enhanced CTest fails as expected.
  • With fix(libc): skip guest exit handlers on host window close #5843 eb8146b73 + this PR f619f964e: child exits 7 and records exactly remaining handler\nshutdown\n.
  • Full build passes; 19 exit/shutdown/related tests pass with no skips, including guest_requested_exit, guest_system_exit, guest_exit_finalizers_before_shutdown, the enhanced guest_nested_exit, and patched_libraries.
  • The supplemental patch passes the conventions checker and whitespace check. No title or Linux run performed.

The runtime build used main fa9365ca7 plus the existing #6452 build prerequisite. The patch was prepared on 03c4cb5e7; the three relevant source files are unchanged between those bases, and the tested patched copies matched the prepared copies by hash. This is local stack validation, not a GitHub merge-commit CI result.

The production changes remain the work of the existing PR authors. The supplemental test patch below addresses the remaining coverage request.

Supplemental test patch
diff --git a/core/libs/tests/GuestSystemExit.cpp b/core/libs/tests/GuestSystemExit.cpp
index bcfe623a5..35c1882d9 100644
--- a/core/libs/tests/GuestSystemExit.cpp
+++ b/core/libs/tests/GuestSystemExit.cpp
@@ -90,7 +90,10 @@ void AppendMarker(const std::string& line) {
 }
 void MainShutdown() { AppendMarker("shutdown"); }
 void SecondaryShutdown() { AppendMarker("secondary shutdown"); }
-void APS5_VABI RemainingHandler(void*) { AppendMarker("remaining handler"); }
+void APS5_VABI RemainingHandler(void*) {
+    if (LibcShutdownToken_nid_postfix().stop_requested()) std::_Exit(8);
+    AppendMarker("remaining handler");
+}
 void APS5_VABI ExitingHandler(void*) { exit_nid_postfix(7); }
 void GuestExitShutdown() { AppendMarker("shutdown"); }
 void APS5_VABI CheckShutdownStillRunning(void*) {
@@ -135,7 +138,7 @@ int DriveNestedExit() {
     std::error_code ignored;
     std::filesystem::remove(marker, ignored);
     Require(code == 7);
-    Require(text == "remaining handler\n");
+    Require(text == "remaining handler\nshutdown\n");
     std::puts("Guest nested exit completed");
     return 0;
 }
@@ -209,6 +212,7 @@ int main(int argc, char** argv) {
         return DriveNestedExit();
     }
     if (argc > firstArgument && std::strcmp(argv[firstArgument], "--nested-exit") == 0) {
+        LibcRegisterShutdown_nid_postfix(GuestExitShutdown);
         Require(__cxa_atexit_nid_postfix(RemainingHandler, nullptr, nullptr) == 0);
         Require(__cxa_atexit_nid_postfix(ExitingHandler, nullptr, nullptr) == 0);
         exit_nid_postfix(0);

@MarcZX10
MarcZX10 force-pushed the codex/libc-explicit-exit-finalizers-5931 branch from f619f96 to 8412834 Compare October 15, 2026 21:05
@MarcZX10

Copy link
Copy Markdown
Contributor Author

The latest Windows relinker job built successfully and passed 176/177 CTest cases. Its only failure is build_eligibility: two tests timed out in the nested Node harness at the 20-second subprocess limit; the other relinker tests passed. This test is outside the changed files, and tools.tests.test_build_eligibility passes locally on Linux (7/7). GitHub does not allow rerunning this workflow run (run ... cannot be rerun; its workflow file may be broken), so the Windows result remains a CI failure rather than a confirmed transient.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

Update: the Windows full-build job on current main (e16b2bc) completed successfully in run 38806909876, so the build_eligibility timeout is intermittent rather than a consistent baseline failure. The harness reuse change is in #6583; I will report its Windows result when that check completes.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

Follow-up: PR #6583 now passes the build_eligibility relinker suite on all three hosted platforms: Ubuntu (2m29s), macOS (2m57s), and Windows (6m35s). Eligibility and conventions also pass. This is the amended single-Node harness head; it addresses the nested-process timeout observed on this PR and #5978.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

The current Windows relinker check has the same build_eligibility subprocess timeout as #6184: the nested Node harness hit the 20-second limit (two scenarios in this run), while the other 176/177 CTest cases passed. PR #6583 keeps one Node process alive across the scenarios; its current head passes the eligibility relinker suite on Ubuntu, macOS and Windows. Since #6583 is still open, this remains a proposed follow-up; a rerun after it merges would confirm the fix on this PR.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

The current Windows relinker failure is in the shared build_eligibility CTest, not the AGC changes: the author_with_a_commit_in_main_runs_the_relinker scenario times out after 20 seconds while its Node subprocess runs. All other 176/177 Windows relinker tests passed; Linux, macOS, conventions and eligibility checks pass. This same timeout is present on #5978. I found #6484 already exercises better timeout diagnostics for this test and its Windows relinker check passes. I have not changed your branch or opened a duplicate; the runner may be intermittently overloaded, and the failing run is from Oct 15. Could you rerun the Windows relinker job on the current head to establish whether it reproduces?

@MarcZX10

Copy link
Copy Markdown
Contributor Author

Update: the current head of #6583 (e192246) has green Ubuntu, macOS and Windows relinker checks. Its single-Node harness directly addresses the nested Node timeout reported here and on #5978. #6583 remains open; after it lands on main, rerunning this PR's Windows relinker check will verify the timeout is resolved. No further action is needed on this branch until then.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

The only failing check in this run is build_eligibility: its Node harness exceeded the 20-second subprocess timeout. This is unrelated to this PR's source changes. #6583 addresses the repeated Node startup that caused these Windows timeouts, and its Windows relinker check passed. I cannot rerun the workflow from this account (push permission is unavailable); once #6583 lands, this PR should be rerun against updated main.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

The latest Windows check confirms the same build_eligibility 20-second Node harness timeout as #6184; the other tests pass. #6583 reuses a single Node process and has green Windows relinker checks. Once #6583 lands, please rerun this PR’s check to confirm the timeout is gone.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

I rechecked the current Windows relinker failure: it is the same build_eligibility 20-second Node harness timeout as #6184, while the other relinker tests pass. Open PR #6583 reuses one Node process and has green Ubuntu/macOS/Windows checks, but it has not merged yet. I cannot dispatch a workflow rerun without repository admin rights. Once #6583 lands, this Windows job needs a fresh run against the current head.

@MarcZX10
MarcZX10 force-pushed the codex/libc-explicit-exit-finalizers-5931 branch from 8412834 to 22f1c99 Compare October 16, 2026 17:13
@MarcZX10

Copy link
Copy Markdown
Contributor Author

I rebased the two existing commits onto current main in 011e8ef82 and 22f1c9957 so the current build eligibility harness is used. Both commits remain MarcZX100-authored, with no co-author added. The fresh eligibility, conventions and report checks are running; the old Windows timeout run could not be rerun because GitHub reports its workflow file may be broken. I will report the current-head result when it completes.

@adriadam10 adriadam10 self-assigned this Oct 16, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 16, 2026
@adriadam10

Copy link
Copy Markdown
Collaborator

Rebased onto current main (only conflict: the test registrations in libSceSystemService/CMakeLists.txt, kept both sides) and added one commit: the new --exit-order* modes and --relaunch-driver in GuestSystemExit.cpp now parse with firstArgument like the rest of main().

Checked on the rebased head: build and scoped ctest green for all exit tests (guest_exit_finalizers_before_shutdown, guest_nested_exit, guest_requested_exit, guest_return_from_main, guest_system_relaunch, guest_system_exit*). Reverting the Process.cpp change makes guest_nested_exit and guest_exit_finalizers_before_shutdown fail. Windows and title runs by @ahmad3a4, @milescoviello and @dogukanatlihan cover what we can't run here; merging once the full Build is green.

@adriadam10
adriadam10 force-pushed the codex/libc-explicit-exit-finalizers-5931 branch from 22f1c99 to 160ba25 Compare October 16, 2026 19:58
@MarcZX10

Copy link
Copy Markdown
Contributor Author

Thanks for the rebase and for adding the firstArgument parsing fix to the new exit modes. The scoped exit tests and the negative control make the finalizer regression clear. I have no further changes to request; I’ll wait for the full Build result.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 16, 2026
@adriadam10
adriadam10 merged commit 29a546d into boykopovar:main Oct 16, 2026
14 checks passed
milescoviello added a commit to milescoviello/AnyPS5 that referenced this pull request Oct 17, 2026
…tarts the exit

Since boykopovar#5978, StartExit only parks the other guest threads and exit runs
LibcRunShutdown itself, so LoadExec's three exits (the converted
executable on Windows and Linux, and the host executable) now call
LibcStartExit and then LibcRunShutdown, as exit does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants