fix(libSceNet): register libkernel sockets with sceNetEpollControl #11242

Merged
parl0v merged 2 commits from upstream-pr/11242 into main 2026-10-26 22:05:09 +00:00
parl0v commented 2026-10-25 22:28:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by parl0v on 2026-10-25T22:28:42Z.


What

sceNetEpollControl and sceNetEpollWait now take sockets made with libkernel's socket(), not only sceNetSocket ones.

  • sceNetEpollControl accepts a descriptor that libSceNet doesn't own but libkernel has open as a socket. The libkernel check (GuestSocketIsOpen) runs outside g_mutex. Error order is unchanged: an unknown epoll id still gives EBADF, a bad op or a missing event EINVAL, a descriptor neither library has open EBADF, and a duplicate ADD EADDRINUSE.
  • sceNetEpollWait looks up the host socket of a kernel registration with KernelLookupSocketNative after g_mutex is released. That lookup asks libSceNet's own table (FindSocketNativeForKernel, which takes g_mutex) for descriptors libkernel doesn't own. The returned owner is held across WSAPoll/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.
  • libkernel's close() already asks libSceNet's socket closer first, and sceKernelClose now does the same. For a descriptor libSceNet doesn't own, that closer drops the descriptor's epoll registrations, the same way sceNetSocketClose does 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.
  • One new debt entry in docs/dev/debt/libSceNet.md.

Why

Human Fall Flat (PPSA02429) opens its sockets with libkernel's socket() and registers them with sceNetEpollControl. libSceNet only looked in its own table, so every ADD and DEL failed with SCE_NET_ERROR_EBADF (0x80410109). The game retries right away, from several threads, each with its own socket.

I measured it on upstream main 26ba9b6b71 on my test box (i5-12400F, RTX 3060, Windows): 1,674,902 failing calls in the first 30 s, half of them DEL. A temporary counter in sceNetEpollControl showed what the descriptors are. At 1.5 M calls there were 14 descriptors (17, 18, 20 to 30, 32), each in ADD/DEL pairs, 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, and KernelLookupSocketNative returned a host socket for it. There were no calls on sceNetSocket sockets.

This is the same split that MXGP 26 (PPSA30839) ran into in 8b81fa09bf (sceNetSocketClose) and 3573289f2f (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

  • New CheckEpollKernelSocket in guest_sce_net, on libkernel UDP sockets:
    • ADD returns 0, and a second ADD gives EADDRINUSE.
    • A wait with timeout 0 returns 0.
    • After a sendto, the socket is reported readable, with ident and data intact.
    • MOD to writable is reported as writable.
    • DEL returns 0, a second DEL gives ENOENT, and MOD after DEL gives ENOENT.
    • After close() on one registered socket and sceKernelClose on another, waits return 0 without a crash. Then DEL and ADD on those descriptors give EBADF.
    • A descriptor that was never open still gives EBADF.
    • A socket registered and closed with sceKernelClose, whose number is then reused by a new socket: the new socket starts unregistered (ADD returns 0), and a datagram sent to it isn't reported through the old registration. Without the sceKernelClose change, guest_sce_net fails there.
  • On main with only the test applied, guest_sce_net aborts at the first ADD (0x80410109, errno 9). With the fix it passes. MinGW Release build, Windows 10.
  • Human Fall Flat for 3 minutes with the fix: 0 failing sceNetEpollControl calls, against 1,674,902 on main.
  • The game still stays on the black loading screen. That happens on main too, so this PR doesn't fix it, and I haven't found the cause yet.
  • python tools/check_conventions.py --base upstream/main passes.
  • #9515 adds the reverse direction for getsockopt (libkernel getsockopt on libSceNet sockets). It doesn't overlap with these hunks.
  • #9948 lists socket() sockets in sceNetGetSockInfo. Its debt line says the other sceNet calls still answer EBADF for such sockets, which is no longer true for epoll once this lands. There's no textual conflict.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file (none added)
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 3060 12 GB, NVIDIA 617.42, Windows 10 (title run); the change itself is CPU-side
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5009356403 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11242). Originally posted by [parl0v](https://github.com/parl0v) on 2026-10-25T22:28:42Z. --- ### What `sceNetEpollControl` and `sceNetEpollWait` now take sockets made with libkernel's `socket()`, not only `sceNetSocket` ones. - `sceNetEpollControl` accepts a descriptor that libSceNet doesn't own but libkernel has open as a socket. The libkernel check (`GuestSocketIsOpen`) runs outside `g_mutex`. Error order is unchanged: an unknown epoll id still gives `EBADF`, a bad op or a missing event `EINVAL`, a descriptor neither library has open `EBADF`, and a duplicate `ADD` `EADDRINUSE`. - `sceNetEpollWait` looks up the host socket of a kernel registration with `KernelLookupSocketNative` after `g_mutex` is released. That lookup asks libSceNet's own table (`FindSocketNativeForKernel`, which takes `g_mutex`) for descriptors libkernel doesn't own. The returned owner is held across `WSAPoll`/`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. - libkernel's `close()` already asks libSceNet's socket closer first, and `sceKernelClose` now does the same. For a descriptor libSceNet doesn't own, that closer drops the descriptor's epoll registrations, the same way `sceNetSocketClose` does 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. - One new debt entry in `docs/dev/debt/libSceNet.md`. ### Why Human Fall Flat (PPSA02429) opens its sockets with libkernel's `socket()` and registers them with `sceNetEpollControl`. libSceNet only looked in its own table, so every `ADD` and `DEL` failed with `SCE_NET_ERROR_EBADF` (0x80410109). The game retries right away, from several threads, each with its own socket. I measured it on upstream main 26ba9b6b71 on my test box (i5-12400F, RTX 3060, Windows): 1,674,902 failing calls in the first 30 s, half of them `DEL`. A temporary counter in `sceNetEpollControl` showed what the descriptors are. At 1.5 M calls there were 14 descriptors (17, 18, 20 to 30, 32), each in `ADD`/`DEL` pairs, 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, and `KernelLookupSocketNative` returned a host socket for it. There were no calls on `sceNetSocket` sockets. This is the same split that MXGP 26 (PPSA30839) ran into in 8b81fa09bf (`sceNetSocketClose`) and 3573289f2f (`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 - New `CheckEpollKernelSocket` in `guest_sce_net`, on libkernel UDP sockets: - `ADD` returns 0, and a second `ADD` gives `EADDRINUSE`. - A wait with timeout 0 returns 0. - After a `sendto`, the socket is reported readable, with ident and data intact. - `MOD` to writable is reported as writable. - `DEL` returns 0, a second `DEL` gives `ENOENT`, and `MOD` after `DEL` gives `ENOENT`. - After `close()` on one registered socket and `sceKernelClose` on another, waits return 0 without a crash. Then `DEL` and `ADD` on those descriptors give `EBADF`. - A descriptor that was never open still gives `EBADF`. - A socket registered and closed with `sceKernelClose`, whose number is then reused by a new socket: the new socket starts unregistered (`ADD` returns 0), and a datagram sent to it isn't reported through the old registration. Without the `sceKernelClose` change, `guest_sce_net` fails there. - On main with only the test applied, `guest_sce_net` aborts at the first `ADD` (`0x80410109`, errno 9). With the fix it passes. MinGW Release build, Windows 10. - Human Fall Flat for 3 minutes with the fix: 0 failing `sceNetEpollControl` calls, against 1,674,902 on main. - The game still stays on the black loading screen. That happens on main too, so this PR doesn't fix it, and I haven't found the cause yet. - `python tools/check_conventions.py --base upstream/main` passes. ### Related - #9515 adds the reverse direction for getsockopt (libkernel `getsockopt` on libSceNet sockets). It doesn't overlap with these hunks. - #9948 lists `socket()` sockets in `sceNetGetSockInfo`. Its debt line says the other sceNet calls still answer `EBADF` for such sockets, which is no longer true for epoll once this lands. There's no textual conflict. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file (none added) - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 3060 12 GB, NVIDIA 617.42, Windows 10 (title run); the change itself is CPU-side - [x] AI-assisted: yes
MarcZX10 commented 2026-10-25 22:32:11 +00:00 (Migrated from github.com)

Imported 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: sceKernelClose bypasses 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645691369 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11242#pullrequestreview-5645691369). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T22:32:11Z. **Review:** CHANGES_REQUESTED --- I found one correctness issue in close handling for the new kernel-socket registrations: `sceKernelClose` bypasses 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.
MarcZX10 commented 2026-10-25 23:30:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T23:30:56Z.

Review: APPROVED


Re-reviewed exact head 3cbd5a4be3366ab9798c495f94809e03dc11dd05. The new sceKernelClose path calls the registered libSceNet closer; for a libkernel-owned socket, CloseSocketForKernel retires its epoll entry under g_mutex, then the existing GuestSockets::Close path closes the descriptor. The regression registers a libkernel socket, closes it with sceKernelClose, 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-head git diff --check upstream/main...HEAD and tools/check_conventions.py --base upstream/main pass. Hosted conventions, labels, eligibility and report pass; product/relinker jobs skip by path policy. The focused Windows guest_sce_net run is author-reported and I did not rerun a local product build. No remaining blocker found.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645892103 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11242#pullrequestreview-5645892103). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T23:30:56Z. **Review:** APPROVED --- Re-reviewed exact head `3cbd5a4be3366ab9798c495f94809e03dc11dd05`. The new `sceKernelClose` path calls the registered libSceNet closer; for a libkernel-owned socket, `CloseSocketForKernel` retires its epoll entry under `g_mutex`, then the existing `GuestSockets::Close` path closes the descriptor. The regression registers a libkernel socket, closes it with `sceKernelClose`, 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-head `git diff --check upstream/main...HEAD` and `tools/check_conventions.py --base upstream/main` pass. Hosted conventions, labels, eligibility and report pass; product/relinker jobs skip by path policy. The focused Windows `guest_sce_net` run is author-reported and I did not rerun a local product build. No remaining blocker found.
MarcZX10 commented 2026-10-26 06:39:48 +00:00 (Migrated from github.com)

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 sceNetEpollControl still has a close/reuse race for libkernel-owned sockets: GuestSocketIsOpen_nid_no_patch(id) checks GuestSockets::sockets under socketsMutex, releases that lock, and only afterwards does this function reacquire g_mutex and insert the numeric descriptor into g_epoll_socks. If another thread closes id and 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 racing ADD. 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5648242160 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11242#pullrequestreview-5648242160). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T06:39:48Z. **Review:** CHANGES_REQUESTED --- The sequential close/reuse regression covers cleanup after a completed close, but `sceNetEpollControl` still has a close/reuse race for libkernel-owned sockets: `GuestSocketIsOpen_nid_no_patch(id)` checks `GuestSockets::sockets` under `socketsMutex`, releases that lock, and only afterwards does this function reacquire `g_mutex` and insert the numeric descriptor into `g_epoll_socks`. If another thread closes `id` and 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 racing `ADD`. 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!11242
No description provided.