fix(libSceNet): register libkernel sockets with sceNetEpollControl #11242
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11242"
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 parl0v on 2026-10-25T22:28:42Z.
What
sceNetEpollControlandsceNetEpollWaitnow take sockets made with libkernel'ssocket(), not onlysceNetSocketones.sceNetEpollControlaccepts a descriptor that libSceNet doesn't own but libkernel has open as a socket. The libkernel check (GuestSocketIsOpen) runs outsideg_mutex. Error order is unchanged: an unknown epoll id still givesEBADF, a bad op or a missing eventEINVAL, a descriptor neither library has openEBADF, and a duplicateADDEADDRINUSE.sceNetEpollWaitlooks up the host socket of a kernel registration withKernelLookupSocketNativeafterg_mutexis released. That lookup asks libSceNet's own table (FindSocketNativeForKernel, which takesg_mutex) for descriptors libkernel doesn't own. The returned owner is held acrossWSAPoll/poll, so the host socket stays alive even if the guest closes it during the wait. The Windows closed-stream adjustment still applies only to libSceNet sockets, since it needs libSceNet's per-socket state.close()already asks libSceNet's socket closer first, andsceKernelClosenow does the same. For a descriptor libSceNet doesn't own, that closer drops the descriptor's epoll registrations, the same waysceNetSocketClosedoes at close time. So a socket that reuses the number starts unregistered. A wait still drops a kernel registration whose socket is gone, as a fallback.docs/dev/debt/libSceNet.md.Why
Human Fall Flat (PPSA02429) opens its sockets with libkernel's
socket()and registers them withsceNetEpollControl. libSceNet only looked in its own table, so everyADDandDELfailed withSCE_NET_ERROR_EBADF(0x80410109). The game retries right away, from several threads, each with its own socket.I measured it on upstream main
26ba9b6b71on my test box (i5-12400F, RTX 3060, Windows): 1,674,902 failing calls in the first 30 s, half of themDEL. A temporary counter insceNetEpollControlshowed what the descriptors are. At 1.5 M calls there were 14 descriptors (17, 18, 20 to 30, 32), each inADD/DELpairs, and every call had the same result: the epoll id was valid, the descriptor wasn't in libSceNet's table, libkernel had it open as a socket, andKernelLookupSocketNativereturned a host socket for it. There were no calls onsceNetSocketsockets.This is the same split that MXGP 26 (PPSA30839) ran into in
8b81fa09bf(sceNetSocketClose) and3573289f2f(sceNetSetsockopt). I follow the position taken there: on the console both libraries work on the same kernel sockets. Epoll on libkernel sockets is not measured on a console, and the debt entry says so.Tested
CheckEpollKernelSocketinguest_sce_net, on libkernel UDP sockets:ADDreturns 0, and a secondADDgivesEADDRINUSE.sendto, the socket is reported readable, with ident and data intact.MODto writable is reported as writable.DELreturns 0, a secondDELgivesENOENT, andMODafterDELgivesENOENT.close()on one registered socket andsceKernelCloseon another, waits return 0 without a crash. ThenDELandADDon those descriptors giveEBADF.EBADF.sceKernelClose, whose number is then reused by a new socket: the new socket starts unregistered (ADDreturns 0), and a datagram sent to it isn't reported through the old registration. Without thesceKernelClosechange,guest_sce_netfails there.guest_sce_netaborts at the firstADD(0x80410109, errno 9). With the fix it passes. MinGW Release build, Windows 10.sceNetEpollControlcalls, against 1,674,902 on main.python tools/check_conventions.py --base upstream/mainpasses.Related
getsockopton libSceNet sockets). It doesn't overlap with these hunks.socket()sockets insceNetGetSockInfo. Its debt line says the other sceNet calls still answerEBADFfor such sockets, which is no longer true for epoll once this lands. There's no textual conflict.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 (none added).mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T22:32:11Z.
Review: CHANGES_REQUESTED
I found one correctness issue in close handling for the new kernel-socket registrations:
sceKernelClosebypasses the cleanup callback, so a reused descriptor can inherit the old epoll registration before the next wait prunes it. I requested cleanup on that path and a reuse regression. Current-main conventions and diff-check pass; hosted product jobs were skipped by path policy. The author reports Windows tests, which I did not rerun locally.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T23:30:56Z.
Review: APPROVED
Re-reviewed exact head
3cbd5a4be3366ab9798c495f94809e03dc11dd05. The newsceKernelClosepath calls the registered libSceNet closer; for a libkernel-owned socket,CloseSocketForKernelretires its epoll entry underg_mutex, then the existingGuestSockets::Closepath closes the descriptor. The regression registers a libkernel socket, closes it withsceKernelClose, reuses the descriptor, verifies the new socket is not reported through the old registration, then registers it and confirms readiness/data. This addresses my prior stale-registration finding. Exact-headgit diff --check upstream/main...HEADandtools/check_conventions.py --base upstream/mainpass. Hosted conventions, labels, eligibility and report pass; product/relinker jobs skip by path policy. The focused Windowsguest_sce_netrun is author-reported and I did not rerun a local product build. No remaining blocker found.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T06:39:48Z.
Review: CHANGES_REQUESTED
The sequential close/reuse regression covers cleanup after a completed close, but
sceNetEpollControlstill has a close/reuse race for libkernel-owned sockets:GuestSocketIsOpen_nid_no_patch(id)checksGuestSockets::socketsundersocketsMutex, releases that lock, and only afterwards does this function reacquireg_mutexand insert the numeric descriptor intog_epoll_socks. If another thread closesidand opens a replacement in that gap, the close callback can erase no registration, then this call can register the replacement descriptor without validating its identity. A subsequent wait can therefore deliver readiness for the replacement under the old registration. Please make validation and registration retain/verify the socket lifetime (or otherwise serialize close/reuse with this insertion), and add a deterministic regression for close/reuse racingADD. The existing sequential reuse test does not exercise this ordering. I did not run a product build locally; the focused Windows test is author-reported. Exact-head diff check and current-main conventions pass; hosted product jobs were skipped by path policy.