fix(libSceSystemService): end LoadExec exit without running guest exit handlers #5236
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5236"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by David-cmrg on 2026-10-13T15:22:52Z.
What
Fixes #5235.
#4872 made
sceSystemServiceLoadExec("exit")run the title's__cxa_atexithandlers before the host's, which fixed #4846. This change stops running the title's handlers on"exit"at all, because the console doesn't run them: the shell closes the title asynchronously, and titles are written for that. PS5_SwitchWaker sleeps forever after the call (catchReturnFromMain), and VLC for PS5 calls_exit(0)right after it (plat_quit). Wind Waker (PPSA99309) segfaulted in one of its own destructors at this point."exit"now calls a new libc helper,LibcExitWithoutHandlers_nid_no_patch, next toLibcExit_nid_no_patch. Both first mark the exit as requested and started and runLibcRunShutdown, or park if another exit is already running; that part is now shared. The new helper then flushes the host streams and callsLibcTerminate, withoutCxaFinalizeorstd::exit. Marking the exit first keeps the title's other threads as they are onexit(): a thread that getsProcessShutdownduring the shutdown, for example inAgcDriver::Submit, parks inLibcAwaitExit_nid_postfix, and a thread that callsexit()meanwhile waits instead of running the title's handlers. #4846 stays fixed, since no guest code, and so no guest free, runs during the exit.guest_system_exitregisters a guest__cxa_atexithandler and a hostatexithandler that both print "Guest exit handler ran" and fail. Its shutdown callback also starts a thread that callsLibcAwaitExit_nid_postfix; the test fails with "Guest thread resumed during exit" if that call throwsProcessShutdowninstead of parking the thread. The test passes on "Guest shutdown completed", printed by the shutdown callback. The technical debt entry forsceSystemServiceLoadExecdescribes the"exit"ending. It also notes that a line a title forwards through its own thread just before may be lost: Wind Waker's last line, "exit status 14: asking the shell to close the title", was missing in 1 of 3 runs with the change.#5083 (open) changes the executable-launch branch of the same function and lists this PR in its merge order. This PR leaves that branch alone and only changes
"exit".Tested
Linux, Fedora 44, GCC 16.2.1, Release,
maina3aeaf884with and without this change:guest_system_exit_testsmodes (guest_system_exit,guest_system_exit_parameters,guest_quick_exit,guest_immediate_exit,guest_return_from_main,guest_system_relaunch,guest_nested_exit),guest_system_kill_app,guest_system_kill_app_status,guest_system_kill_app_parameters,guest_system_service,guest_system_service_language,guest_exit_order,application_heap_exit_order,guest_thread_atexit,guest_user_contextandguest_user_context_exit.guest_system_exit: with the first version of this PR (LibcRunShutdown, a flush andLibcTerminate, without marking the exit), it fails in 3 of 3 runs with "Guest thread resumed during exit"; withmain'sExport.cpp, it fails in 3 of 3 with "Guest exit handler ran".mainit ends with a segmentation fault (exit code 139) in 3 of 3 runs; with the change it exits with code 0 in 3 of 3.main9510e71d6with the first version of this change: ProsperoEden (PPSA99008, the title from #4846), which also ends withsceSystemServiceLoadExec("exit"), exited with code 0 in 7 of 10 runs both onmainand with the change; in the other 3 on each side it segfaulted before callingsceSystemServiceLoadExec, in the AGC driver thread. From an empty shader cache, its first run wrote 7 shader cache entries (68 KiB) and its second run wrote none, both onmainand with the change.python3 tools/check_conventions.py --base origin/mainpasses, andgit diff --checkis clean.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by AlexOfTheForest on 2026-10-14T05:58:48Z.
The "exit" path no longer goes through LibcExit_nid_no_patch, so exitRequested and exitStarted (core/libs/prx/libc/src/Process.cpp) stay false while LibcRunShutdown_nid_postfix runs.
LibcRunShutdown_nid_postfix stops the shutdown token. A guest thread that is inside AgcDriver::Submit (or the other entry points in Execution/src/Driver/EntryPoints.cpp, or libSceVideoOut/src/Event.cpp) at that moment gets ProcessShutdown and calls LibcAwaitExit_nid_postfix. With exitRequested == false, that throws ProcessShutdown{} again instead of parking the thread, so the exception goes back into guest code during shutdown. On main, LibcExit_nid_no_patch sets exitRequested first, so these threads park.
Same if another guest thread calls exit() meanwhile: exitStarted is still false, so it ends up in CxaFinalize_nid_no_patch and runs the title's handlers that this PR wants to skip, racing LibcTerminate_nid_no_patch.
A small libc helper next to LibcExit_nid_no_patch that sets exitRequested/exitStarted, then shuts down, flushes and terminates without CxaFinalize would keep both cases as on main. The relaunch path has the same pattern, but that can be a separate PR.
Removing VerifyExit loses no coverage: normal exit() handlers are still tested by guest_exit_order and guest_system_kill_app.
Imported from boykopovar/AnyPS5. Originally posted by David-cmrg on 2026-10-14T07:52:06Z.
Updated to
b3ef07ba8, rebased ontomaina3aeaf884:"exit"now calls a newLibcExitWithoutHandlers_nid_no_patchnext toLibcExit_nid_no_patch. Both start with the same shared part, which setsexitRequestedandexitStartedand runs the shutdown, or parks if another exit is already running. The new helper then flushes and callsLibcTerminateinstead ofCxaFinalizeandstd::exit;LibcExit_nid_no_patchbehaves as before.guest_system_exit's shutdown callback now also starts a thread that callsLibcAwaitExit_nid_postfix, and the test fails if that thread getsProcessShutdownback. It fails in 3 of 3 runs with the previous head'sExport.cppand passes with the helper.exit()during the shutdown has no test, since that race can't fail reliably. The same change covers it:exitStartedis set before the shutdown starts, so such a thread parks inLibcAwaitExit_nid_postfix.On the new base, 17 exit tests pass in 4 of 4 runs, including
guest_exit_order,guest_system_kill_appandguest_user_context_exit, and Wind Waker still exits with code 0 in 3 of 3 runs (139 onmain). The description has the details. Thanks for checking that removingVerifyExitloses no coverage.Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-14T12:35:45Z.
Windows run, the platform where Wind Waker's exit shows up as an access violation. Windows 11 Pro 25H2, RTX 3080 (NVIDIA 591.86), WinLibs GCC 15.2.0, Ninja Release. Head
b3ef07ba8merged ontomain2aad30682with no conflicts (Export.cppauto-merged).guest_system_exit_tests(guest_system_exit,parameters,quick_exit,immediate,return_from_main,relaunch,nested_exit) pass 5 of 5 each, onmainand on the merge. The merge build has 0 warnings.main: PPSA99309, catalog release, relinked with--windows, no disc image, 3 runs. All 3 end withFATAL: unhandled exception 0xc0000005atapp.exe+0xddabd2, reading0x98afterexit status 14: asking the shell to close the title. A Linux gdb run of the same title shows the cause:LoadExec("exit")reachesCxaFinalize, and one of the title's static destructors loads0x98through a null pointer.exit status 14line is missing in 1 of the 3, as the debt entry describes.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T18:10:24Z.
Checked on Linux with the PR merged onto current
main: build and scoped ctest green (116/116), including the sevenguest_system_exit_testsmodes. With the non-test changes reverted,guest_system_exitfails, so the test covers the change (no guest/host exit handler runs, and a guest thread parks inLibcAwaitExit_nid_postfixinstead of gettingProcessShutdown). Windows run by @babyhuey also confirms; merging once the full Build is green.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T19:45:10Z.
Rebased onto current
main(only conflict: thesceSystemServiceLoadExecline indocs/dev/debt/libSceSystemService.mdafter #5083; kept main's text and the"exit"sentence from this PR). Build and scoped ctest green on the rebased head (117/117, including the sevenguest_system_exit_testsmodes). The code diff is unchanged, so the earlier checks (negative control, Windows run by @babyhuey) still apply; merging once the full Build is green.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T20:41:03Z.
Rebased onto current
main(only conflict: thesceSystemServiceLoadExecline indocs/dev/debt/libSceSystemService.mdafter #5083; kept main's text and the"exit"sentence from this PR). Build and scoped ctest green on the rebased head (117/117, including the sevenguest_system_exit_testsmodes). The code diff is unchanged, so the earlier checks (negative control, Windows run by @babyhuey) still apply; the one red in the first full Build wasagc_interpolant_mappingtiming out on Windows, in code this PR doesn't touch, and the job was rerun. Merging once the full Build is green.