fix(libSceSystemService): park guest threads during LoadExec's shutdown as exit does #6237
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6237"
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 milescoviello on 2026-10-15T06:23:12Z.
What
Metal Gear Solid 4 (PPSA18194, 01.004.003) starts in its Master Collection launcher, which runs
/app0/mgs4/mgs4.elfthroughsceSystemServiceLoadExecwhen Start Game is chosen. Onmaind92a8dad3the process ends right there, before the game starts:With gdb, the aborting thread is one of the title's render threads: it is in
CreateInterpolantMapping→AgcDriverResolveShaderAbi_nid_postfixwhen LoadExec's shutdown stops the driver, and the entry point'scatchcallsLibcAwaitExit_nid_postfix. That function parks the thread only once an exit has been requested (exitRequested).exit,sceSystemServiceKillAppand"exit"request one throughStartExitbefore the shutdown runs; LoadExec calledLibcRunShutdown_nid_postfixdirectly, soLibcAwaitExit_nid_postfixthrewProcessShutdownback into title code, which has nothing to catch it.LoadExec now starts the shutdown the way exit does, on both the
fexecveandCreateProcessWpaths and the argument-less fallback:LibcStartExit_nid_postfix(which isStartExit) and thenLibcRunShutdown_nid_postfix. Since #5978,StartExitno longer runs the shutdown callbacks, so both calls are needed. A thread that reaches a stopped library during it waits until the process is replaced. Thedocs/dev/debt/libSceSystemService.mdentry says so.guest_system_relaunchnow starts a thread that waits inLibcAwaitExit_nid_postfixfrom the first image's shutdown callback, asguest_system_exitdoes forexit.Tested
Ubuntu 24.04, GCC 13.3, Release, on
maind92a8dad3:guest_system_relaunchand the other sixguest_system_exittests pass 5/5. Withmain'sLoadExec.cpp,Export.cpp,Process.cppandShutdown.hppand the new check,guest_system_relaunchfails 3/3 (Guest thread resumed during exit).^guest_|loadexec|shutdownpass, includingloadexec_argumentsand the AGC, AudioOut and job-manager shutdown tests.--to-intel --executable mgs4/mgs4.elfand run with Cross pressed every 1.5 s (RTX 4070 Ti, NVIDIA 595.99): onmainit ends at Start Game with the lines above. With this change the launcher's process is replaced,mgs4.elfopens its save data and Vulkan device, and it plays its opening (the logo, then the TV channel intro) at about 60 FPS until the 180 s limit. A 600 s run with Cross pressed every 1.5 s also reaches its limit, with no driver failure or unhandled exception in the game's log. With other menu presses, onmainb4a4fcac0with this PR and #5904 (which the launcher's Online Manual button needs), a 600 s run gets past the TV intro into Act 1's opening cutscene, at about 4 to 13 FPS there, and reaches the limit.plat_restartcallssceSystemServiceLoadExec("/app0/eboot.bin", NULL)(platform_ps5.c); a pad sequence that reaches it does so 18 s in. Onmainthe process aborts there withunhandled exception '15ProcessShutdown', 5 of 5 runs (4 ond92a8dad3, 1 on0afa6a607). With temporary prints (not part of this PR), the call reachesLibcRunShutdownwithexitRequestedfalse. With this PR the shutdown runs with it set and the process is replaced (2 of 2 runs, plus 1 traced). The relaunched image's output only shows up with #5699 as well: with both, it reloads the pipeline cache, hits the shader cache and presents frames until the 60 s limit, 2 of 2 runs.main489466d3f. A test process keeps an AudioOut2 mixer thread pushing through LoadExec's shutdown: onmainthe image is relaunched 0 of 10 times (the sameProcessShutdownterminate), with this PR 30 of 30. They trace the Metal Gear Solid: Peace Walker launcher regression from #6000 (PPSA18216) to this path. Their test is in that comment if a regression test for the audio path is wanted here.main8dc92ae29. VLC's restart takes the null-listCreateProcessWpath there. Onmainit ends in the sameProcessShutdownterminate; with this PR the new process starts and presents to the 120 s limit (2 of 2 runs, and 2 more with #5699). Theguest_system_exittests andloadexec_argumentspass 3 of 3, and the negative control fails as here. They also note a Windows edge case for a follow-up: a LoadExec with an argument list while another thread is already inexitparks here, and leaves the suspended child behind.main82ddae75b. PS5CEMU-HAR 4.0.1 (PPSA99360) restarts itself through LoadExec after its self-update. Onmainit ends in the same unhandledProcessShutdownterminate (exit 0xC0000409). With this PR, 2 of 2 runs that reach the restart exit 0, and the new instance starts.tools/check_conventions.py --base upstream/mainpasses, andtools/pr_overlap.py --branch fix/loadexec-exit-shutdownfinds no overlap. #5843 and #5978 change other lines ofProcess.cpp(the exit paths), not LoadExec.Rebased onto
main864729dc0d(5b072bc2c4), pluse71ade0ac3for theLibcRunShutdowncalls, after @oneandonlydean's review. Ubuntu 24.04, GCC 13.3, Release:The nine exit and relaunch tests (
guest_system_exit*,guest_system_relaunch,guest_requested_exit,guest_quick_exit,guest_immediate_exit,guest_return_from_main,guest_nested_exit,guest_exit_finalizers_before_shutdown) pass 3 of 3.With 5b072bc2c4's
Export.cppandLoadExec.cpp(noLibcRunShutdownafterLibcStartExit),guest_system_relaunchfails and the other eight pass. It expectsshutdownandsecondary shutdownfrom the callbacks each image registers before its LoadExec.ctest -R '^guest_|libc|loadexec': 402 of 402 pass.MGS4 (PPSA18194), 300 s with pad input. MGS4 also needs #6842 (scripting heap), so both builds have it. On
main+ #6842 + this PR, the launcher's LoadExec startsmgs4/mgs4.elfand the game runs to the limit. Onmain+ #6842 alone, it ends at 43 s with[libc] unhandled exception '15ProcessShutdown'andterminate.Metal Gear Solid: Peace Walker (PPSA18216, run by @mrfelek): Linux, RTX 3080 Ti,
mainc9e6c45dewith this PR and the other PRs it needs, relinked with--executable mgspw/mgspw.elf. Without a hand-over fix,ExitAndStartNewProjecttomgspw.elfends interminate called after throwing an instance of 'ProcessShutdown', aborted fromsceAudioOutOutput'sProcessShutdowncatch. With this PR the new image draws the title and the opening movies and runs to the 240 s limit.Not checked on a console. Windows: see screamm's and DavidSamir's runs above.
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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T07:33:09Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)docs/dev/debt/libSceSystemService.mdL20Some 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.
core/libs/tests/GuestSystemExit.cppcore/libs/prx/libSceSystemService/Export.cppcore/libs/prx/libc/src/Process.cppdocs/dev/debt/libSceSystemService.mdcore/libs/prx/libSceSystemService/Export.cppcore/libs/prx/libSceSystemService/LoadExec.cppcore/libs/tests/GuestSystemExit.cppImported from boykopovar/AnyPS5. Originally posted by isnisk on 2026-10-15T13:13:46Z.
This also fixes the Peace Walker launcher regression reported on #6000 (PPSA18216, same
'15ProcessShutdown'terminate at the LoadExec hand-over). That one comes from the audio side: since #6000 the AudioOut2 exports park throughLibcAwaitExit_nid_postfixonce the shutdown token is set, and the guest's mixer thread pushes every few milliseconds, so with LoadExec's shutdown running withoutexitRequestedthe next push throwsProcessShutdowninto the title.Checked on Ubuntu 26.04 (WSL2), GCC 15.2.0, Release,
SDL_AUDIODRIVER=dummy, with this PR merged ontomain489466d3f(clean merge). A test process starts an AudioOut2 mixer thread (set the port data, blocking push, repeat), registers a 100 ms shutdown callback in place of the AGC and VideoOut shutdowns, then callssceSystemServiceLoadExec("/app0/picker/picker.bin", {"--secondary"}); the relaunched image writes a marker.main489466d3f[libc] unhandled exception '15ProcessShutdown',terminate called after throwing an instance of 'ProcessShutdown'main+ this PRguest_system_exit,guest_system_relaunchand the fouraudio_out*_shutdowntests pass on both builds, so none of the existing tests covers a guest audio thread during LoadExec. The test is below if you want it as a regression test (Linux only as written; the 100 ms callback makes it deterministic):AudioLoadExec.cpp
Registered locally next to
guest_system_exit_tests:Not checked: Windows (
CreateProcessWpath), a title run.AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-15T13:15:33Z.
Thanks for checking the audio path, and for tying it to Peace Walker. I added your run to the description.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-15T14:38:18Z.
Cross-validation on Windows 11 Pro 26220, NVIDIA RTX 4070 (596.21), MinGW GCC 15.2, PR head a03e02345 merged on main
8dc92ae29:PPSA85300 (VLC 0.3.1). Relinked with
--windows-fixed-base 0x400000,app0/eboot.binstaged, 120 s runs. On a first launch the restart is reached throughAPS5_PAD_INPUT_FILE: Cross, Right (Classic look), Options in the setup.setup_finishthen callsplat_restart->sceSystemServiceLoadExec("/app0/eboot.bin", NULL), so on Windows this is the null-listCreateProcessWpath. The restart comes 17-24 s in.8856f180c, whose LoadExec and libc shutdown files are identical to8dc92ae29; 1 run): the last line invlc-ps5.logisVLC-PS5: restarting to see new drives, then on the console:app.exeexits with 0xC0000409 about 26 s in. No second process starts, and 1163 frames were presented.app.exeexits with 0x00000000 at 18-21 s. The newapp.exestarts at 20.9 s and 18.9 s and prints its ownTransferring control to ELF entry pointon the console. Invlc-ps5.logit reachesstart: first frame on screen,VLC-PS5: splash hidden (0)and[shader-disk-cache] (10 s): 6 hits, 0 misses, then presents until the limit: 11753 and 12060 frames at about 120 FPS. The old process presented 1361 and 1238 frames. NoProcessShutdownline. Between the two images, 4[gpu] storage image ... destroyed with GPU results pendinglines.Tests. The 7
guest_system_exittests pass 3/3 each, andloadexec_argumentspasses 3/3. Negative control: with main'sExport.cpp,LoadExec.cpp,Process.cppandShutdown.hppand the new test,guest_system_relaunchfails 3/3 (Guest thread resumed during exit, thenabort()). The non-GPU suite on main + this PR + #5699 (-j8 --timeout 120 -E '^agc_driver_|^video_out_flip') passes 830/830. The host was shared with other jobs, but nothing timed out in this run.tools/check_conventions.py --base origin/mainpasses andgit diff --checkis clean.A Windows note, not caused by this PR. Each failing negative-control run left a suspended
picker.picker.bin.exe(one thread, suspended).Replacecreates the child withCREATE_SUSPENDEDbefore the shutdown. When the old process ends inside the shutdown, neitherResumeThreadnor thecatch'sTerminateProcessruns. The orphan keeps the copied executable open, and the nextguest_system_relaunchthen fails in the driver'scopy_fileuntil the orphan is killed. This PR removes the cause we hit. The same orphan can still come from a LoadExec with an argument list while another thread is already inexit:LibcStartExit_nid_postfixthen parks the LoadExec thread, and the exit ends the process with the child still suspended. That is an edge case for a follow-up, for example a kill-on-close job object that is cleared beforeResumeThread. The changed test covers only the argument-list path. VLC's null-list path is checked only by the title run above.No other concerns.
Written with Claude Code assistance.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-15T14:39:40Z.
Thanks for the Windows run and the negative control. I added it to the description, along with the suspended-child case as a follow-up.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-16T16:20:52Z.
Cross-validation on Windows 11 Pro build 26300, Intel i7-11700F, NVIDIA GeForce RTX 3070, MinGW GCC, PR head a03e02345d merged onto
main82ddae75b6.PPSA99360 (PS5CEMU-HAR 4.0.1) is another launcher that goes through LoadExec. After the pad sequence has let its self-update install into
/app0, it logs[boxart] stopped for the gameand restarts itself. Relinked with--trap-sdk-syscall-stubs, 150 s runs with a fixed controller sequence (Cross, Down, Right, Circle, Up and so on every 1.2 s, from 12 s):main82ddae75b6, the launcher ends right there:[update] 4.0.1 installed in /app0at 45.9 s,[boxart] stopped for the gameat 48.8 s, then[libc] unhandled exception '15ProcessShutdown'andterminate called after throwing an instance of 'ProcessShutdown', exit code 0xC0000409 at 51 s. The same abort showed up in an earlier sweep onmain332b0b1b79(install 75.9 s, abort 78 s).stopped for the game4.8 s and 1.2 s later) end with exit code 0 and a new instance of the launcher starts (it logs[update] 4.0.1 is outagain from 0.3 s, and the harness stopped it at its time limit). NoProcessShutdownand noterminatein either log.Other runs of the same title did not reach the restart (the update did not start in three of them, so they say nothing either way) and are not counted. This is only the Windows path for a relaunch with no list, as screamm already covered for VLC; I did not run the macOS or Linux paths.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-16T16:26:03Z.
Thank you @DavidSamir, a second launcher that relaunches itself is exactly the kind of case this needed, and with the
ProcessShutdownabort onmainnext to it. I added it to the description.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-16T21:32:33Z.
A re-check on current
main0dc15620d, which now has #5904, with MGS4 (PPSA18194) on Linux, RTX 4070 Ti, pad input. Since #5092 the title also needs #6842 to start, and #6814 is merged in all of these builds for the start-up race.main+ #6814 + #6842: 2 of 2 runs end at 42–43 s, at the launcher'sLoadExecintomgs4/mgs4.elf, withterminate called after throwing an instance of 'ProcessShutdown'.mgs4.elfrunning.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T10:43:53Z.
Thanks. GitHub shows this as mergeable, but it conflicts in behaviour with #5978 (
db3b406dd), which landed after your last MGS4 run:On main,
StartExit()no longer runsRunShutdown. With this PR merged as it stands, every LoadExec would skip all shutdown callbacks.Please:
LibcStartExitand thenLibcRunShutdown, the way exit does now.RunShutdownwere skipped.A fresh MGS4 run on the rebased branch would also help.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T10:51:56Z.
Thanks @oneandonlydean, you're right: since #5978,
StartExitno longer runs the shutdown callbacks, so this PR as it stands would skip them on every LoadExec. I've rebased onto currentmainand addedLibcRunShutdownafterLibcStartExitat all three call sites. I'm running the tests and an MGS4 run on the rebased branch now, and I'll push with the results.On the test:
guest_system_relaunchalready expects the exact marker textshutdown / arguments ... / secondary shutdown / relaunched. Both shutdown lines come from callbacks registered before each LoadExec, so it should fail ifRunShutdownis skipped. I'll show that red/green with the push.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T10:56:24Z.
Thanks. That plan sounds right. A red run of
guest_system_relaunchwithRunShutdownskipped, alongside the green run, will cover the test ask. We will re-review when you push.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T11:09:51Z.
Pushed: rebased onto
main864729dc0d(5b072bc2c4), ande71ade0ac3callsLibcStartExit_nid_postfixand thenLibcRunShutdown_nid_postfixat all three call sites.RunShutdown,guest_system_relaunchfails and the other eight pass.^guest_|libc|loadexecpasses 402 of 402.mgs4/mgs4.elfand the game runs to the limit. Without it, the run ends at 43 s with[libc] unhandled exception '15ProcessShutdown'.The description is updated with the same.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-17T14:48:06Z.
Peace Walker (PPSA18216):
e71ade0acfixes the launcher hand-overSystem: Linux, RTX 3080 Ti (NVIDIA 615.71.09)
Build:
mainc9e6c45deplus this PR (e71ade0ac), #6014, #6196, #6842 and #7072, plus two local changes that aren't in any PR (AvPlayer accepting VP9/Opus, and a depth-sample case posted on #6196). Relinked with--executable mgspw/mgspw.elf.mgspw.elfExitAndStartNewProject Path = /app0/mgspw/mgspw.elfterminate called after throwing an instance of 'ProcessShutdown'LibcAwaitExit_nid_postfixcalled fromsceAudioOutOutput'sProcessShutdowncatch. That matches isnisk's audio-path trace.exitRequestedbeforeLibcRunShutdown); I dropped it for this PR.AI-assisted; the runs are from my machine.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T14:50:35Z.
Thanks @mrfelek, that's a useful second title. Peace Walker goes through the same
ExitAndStartNewProjecthand-over as MGS4, and the abort fromsceAudioOutOutput'sProcessShutdowncatch matches the audio-path trace. I added your run to the description.AI-assisted: yes (Claude Code)