fix(libkernel): keep host standard streams open on guest close #3295
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3295"
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 SynnZer0 on 2026-10-11T06:04:01Z.
What
closeandsceKernelCloseno longer close descriptors 0 to 2 that were open when libkernel loaded. They fail withEBADFand leave the descriptor open.Guest descriptors are host C runtime descriptors. When a title closed descriptor 0, the host process's stdin was closed, and the C runtime then gave descriptor 0 to the next file opened anywhere in the process, including files libkernel opens itself, such as the
std::ifstreamof the APR reader (Apr.cpp_readFile). The title's next close of descriptor 0 then closed that file during the read.PPSA21703 (Vampire Survivors, Unity) calls
sceKernelClose(0)repeatedly while loading its asset bundles: 27 times in one run, every call after the first failing withEBADF(temporary logging, not part of this PR). The symptoms were different on every run:APR: read of …\vs_local_assets_character_<name>.bundle at offset 0 returned 0 of N bytes, and partial reads such as13833 of 15745;APR: read failed for …;FATAL: UCRT invalid parameterfromclose_nid_postfix, which, unlikesceKernelClose, calls_closewithout suppressing the invalid-parameter handler.When a read failed, the destination memory was committed
PAGE_READWRITEand the files were complete on disk, so this is not write-watch protection or the APR execution order. APR itself is unchanged.File/src/HostStreams.cpp(new):File::IsHostStream(fd)records at load whether 0 to 2 are open (_get_osfhandlewith the invalid-parameter handler suppressed on Windows,fcntl(F_GETFD)elsewhere). A standard descriptor that was not open at load (no console) can still be returned byopenand closed normally.sceKernelClose→SCE_KERNEL_ERROR_EBADF;close/_close→-1,errnoEBADF, for those descriptors.docs/dev/TechnicalDebt.md: the console's result for closing 0 to 2 is unknown; recorded with the title that needs it.tests/GuestHostStreams.cpp(new,guest_host_streams): for each of 0 to 2 that is open, both calls fail withEBADFand the descriptor stays usable; a new pipe does not reuse those numbers.Tested
Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs 15.2.0posix-14.0.0-ucrt-r7), full build with
BUILD_TESTING=ON:ctest: 768/769 passed. The failure isguest_filesystem(CreateSymbolicLinkWwithout Developer Mode), which also fails on the base on this machine.python tools/check_conventions.py --base origin/main: passed.HostStreams.cppusesfcntlthere).Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.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-11T06:04:11Z.
Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T07:39:48Z.
Needs a rebase: it conflicts with main in
core/libs/prx/libkernel/CMakeLists.txt,File/src/Open.cppandFile/src/Stdio.cpp, anddocs/dev/TechnicalDebt.mdhas moved todocs/dev/debt/<library>.md(tools/debt_split.py resolvemoves the entries).Imported from boykopovar/AnyPS5. Originally posted by Nesyth on 2026-10-13T16:26:52Z.
Data point from Vampire Survivors (PPSA21703) on today's
main(9d0a41d5a), Windows 11, RX 7900 XTX:APR: read failed for ...\vs_local_assets_character_gennaro.bundle, after twoFATAL: UCRT invalid parameterreportsAPR: read of ...\vs_local_assets_character_imelda.bundle at offset 0 returned 12023 of 18326 bytesError writing save data: Invalid handle to path "/savedata0/SaveData". The title opens that file with truncate, soSaveDatawas left at 0 bytes (21,557 before).mainplus a simpler guard for the same bug (closeandsceKernelCloserefuse descriptors 0 to 2), the title loads and plays, and the save stays intact.Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-14T17:12:45Z.
@SynnZer0 @oneandonlydean The rebase asked for above is ready on
nikowolf123/AnyPS5help/S1-host-streams. It is one commit onmaina453108cf, 802942849, with SynnZer0 still the author and the message unchanged, and it merges cleanly with today'smain(e34c47921). This PR allows maintainer edits, so the branch can replace its head as it is:Only the conflicting lines changed;
git range-diffagainst 9c30f0e4a shows nothing else:sceKernelCloseandclose, theFile::IsHostStream(d)check is still the first line, andmain's socket andEqueueDescriptorClosedlines follow unchanged. The oldd >= GuestSockets::FirstDescriptorcheck from this PR's base is gone onmain.CMakeLists.txt,guest_host_streamsis registered aftermain's newguest_kqueueandguest_socket_descriptors.tools/debt_split.py resolvemoved the debt entry todocs/dev/debt/libkernel.md.Tested:
maina453108cfwith only the test added,guest_host_streamsaborts. On the branch it passes, as do all 137 tests registered incore/libs/prx/libkernel, andtools/check_test_registration.pypasses.mainwith only the test added,guest_host_streamsaborts (0xc0000409). On the branch it passes.guest_dup,guest_filesystem,guest_terminal,guest_kqueue,guest_socket_descriptorsandguest_socket_pairpass on both.main'stools/check_conventions.py --base origin/mainpasses for the branch with this PR's current title and description.Besides Blasphemous 2 (#4223) and Vampire Survivors (@Nesyth's runs above), there is now a third title: @WallisSkill reports in #5892 that Cat Quest III (PPSA15065) stays on the loading screen (#4909) without that PR's commit 1, 4485d02f6. That commit is a narrower copy of this fix: it changes
sceKernelCloseonly, returnsEBADFfor descriptors 0 to 2 whether or not the host has them open, and adds no test or debt entry. Once this PR lands, #5892 can drop it.#5892 and @xXPBSXx's #5938 (opened today) also edit
sceKernelClose, and #5938 editsclosetoo.git merge-treemerges the rebased branch cleanly with each of them.AI-assisted: written with Claude Code; I checked the conflict resolution with
git range-diff, the merges withmaine34c47921, #5892 and #5938 withgit merge-tree, and the title evidence in #4223, #5892 and the comment linked above. I ran the WSL and Windows builds and tests listed above,check_test_registration.pyandcheck_conventions.py.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T17:45:56Z.
Pushed @nikowolf123's rebase (
802942849) here; it merges cleanly with current main.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T17:48:45Z.
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)core/libs/prx/libkernel/File/src/Open.cppL13core/libs/prx/libkernel/File/src/Stdio.cppL18