fix(libSceNet): let a datagram receive on an unbound socket wait on Windows #5966

Merged
EagleBearerYS merged 7 commits from upstream-pr/5966 into main 2026-10-17 04:51:30 +00:00
EagleBearerYS commented 2026-10-14 17:23:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-14T17:23:38Z.


What

Steam-Link-PS5 (PPSA99350), a homebrew Steam Remote Play client, never finds a PC on Windows. It stays on its first screen while Steam on the same PC answers discovery:

[SLP5]     0s state: LOOKING FOR A PC RUNNING STEAM

No discovered ... at ... line follows.

On Windows, sceNetRecv, sceNetRecvfrom and sceNetRecvmsg fail at once with EINVAL on a datagram socket that has no local address yet. Winsock's receive returns WSAEINVAL for a socket that isn't bound (recvfrom).

On the console the receive waits. UDP receives through FreeBSD 11.4's soreceive_dgram (udp_usrreq.c L1780), which sleeps on the receive buffer whether or not the socket has an address, and returns EWOULDBLOCK for a non-blocking socket. A send from another thread then binds the socket, and the reply arrives. Linux waits the same way.

The title's stream library, ihslib, relies on that:

  • Its worker opens the socket and starts receiving at once (base.c L199-L213). Any error other than EAGAIN, EWOULDBLOCK or ETIMEDOUT ends the worker, which closes the socket (ihs_udp_posix.c L68-L78).
  • The discovery broadcast goes out from a timer thread (discovery.c L37-L47), and the title asks for a single one at start (main.cpp L920). Its BSD socket calls go straight to libSceNet (ps5_sockets.c L192-L207).
  • On Windows the worker got EINVAL and closed the socket before the broadcast went out, so no PC could answer.

With this change, on Windows a receive on a datagram socket with no local address waits until a bind, connect or send gives it one. Those calls wake the wait at once, and the wait also checks the host socket every 10 ms:

  • a non-blocking socket fails with EAGAIN at once;
  • the receive timeout ends the wait with EAGAIN, and when the address comes first, the receive waits for a datagram only for what is left of the timeout;
  • a sceNetShutdown of the receive side (SHUT_RD or SHUT_RDWR) ends the wait, and the receive returns 0, as do later receives while the socket has no address. That's what soreceive_dgram does once the socket can't receive more (L2199-L2203). FreeBSD's soshutdown flushes the receive side of an unconnected datagram socket and returns ENOTCONN, keeping the old behaviour that programs use to release a thread blocked in a receive (L2352-L2383). Without this, a title that stops its receiver thread that way would hang on Windows;
  • a receive of zero bytes returns 0 without waiting, as soreceive_dgram does when nothing was asked for (same lines);
  • an abort or close ends the wait, and the host receive then fails as before;
  • once the socket has an address it is noted as bound, so later receives skip the check, and a later sceNetBind fails with EINVAL, as on the console.

Commits:

  1. fix: the wait.
  2. docs: records the wait in docs/dev/debt/libSceNet.md.
  3. refactor: in sceNetRecvmsg the wait comes after the socket type check. No change in behaviour.
  4. fix: shutdown and zero-byte receives return 0, with their test and the debt line.
  5. test: the bind and connect paths, next to the send path the first test covers.
  6. perf: bind, connect and send wake the wait instead of leaving it to the next 10 ms check. On Windows a connect or send marks the socket bound only once the host socket has its address.
  7. fix: one receive timeout covers the wait and the receive. Before, when the address came during the wait, the host receive waited its whole timeout again, so a call could take up to twice the timeout; now it polls (WSAPoll) only for what is left and fails with EAGAIN when that runs out. The debt line says so.

Scope: sockets a title opens through libkernel rather than libSceNet (socket, then recv, recvfrom, recvmsg or read on them) still fail such a receive with EINVAL on Windows. That's libkernel's socket layer, which this PR doesn't change.

Related: #4614, which gave an unbound socket's sceNetGetsockname the zero address on Windows.

Overlap: #5141 adds a stream-socket loopback check right above sceNetConnect's ::connect line, which commit 6 turned into a block for the datagram wake-up. Whichever merges second keeps both.

Tested

On GitHub's runners from my fork:

  • Linux: Ubuntu 24.04.5, GCC 13.3.0, Release with CI's configure flags and -DANYPS5_ENABLE_SPIRV_TOOLS=ON.
  • Windows: Windows Server 2025, WinLibs MinGW-w64 GCC 15.2.0 r7 (UCRT), Release with CI's configure flags.

In guest_sce_net:

  • CheckUnboundDatagramReceive:
    • On an unbound UDP socket with a 50 ms receive timeout (0x1106), sceNetRecv, sceNetRecvfrom and sceNetRecvmsg each fail with EAGAIN after at least 40 ms. Made non-blocking (0x1200), sceNetRecvfrom fails with EAGAIN at once.
    • A thread waiting in sceNetRecvfrom on an unbound socket gets the reply to that socket's first sceNetSendto, sent from the main thread. A sceNetBind on that socket then fails with EINVAL.
    • On Windows, an abort or a close of the socket ends a receive that waits for an address.
  • CheckUnboundDatagramShutdown:
    • A thread waiting in sceNetRecvfrom on an unbound socket returns 0 within a second of sceNetShutdown(socket, SHUT_RD). After that, sceNetRecv returns 0, and sceNetRecvmsg returns 0 with no address.
    • On Windows, sceNetRecvfrom with an address buffer returns 0 with size 0. A zero-byte sceNetRecv and sceNetRecvfrom on a new unbound socket return 0 at once.
  • CheckUnboundDatagramLaterAddress: a thread waits in sceNetRecv on an unbound socket while the main thread binds it, or connects it to a peer. In both cases the peer's datagram to the new address reaches the waiting receive.
  • CheckUnboundDatagramTimeoutAfterAddress: with a 500 ms receive timeout, another thread binds the unbound socket 300 ms into sceNetRecv and nothing is sent. The receive fails with EAGAIN after 450-750 ms.

Results:

  • The first three commits, on main at a453108cf (run):
    • with main's Export.cpp the test aborts on Windows. On Linux it passes, since Linux waits as FreeBSD does;
    • with the change it passes 20 runs in a row on Linux and Windows.
  • Commits 4-6 (run):
    • with commit 3's Export.cpp and the new tests, Windows aborts: the waiting receive doesn't return after the shutdown;
    • with the three commits, guest_sce_net passes 20 runs in a row on Windows and Linux. On Linux it also passes with commit 3's code, since Linux returns 0 after the shutdown, as FreeBSD does.
  • Commit 7 (run):
    • with commit 6's Export.cpp, the new case aborts on Windows: that code waits the whole 500 ms timeout again after the 300 ms wait;
    • with commit 7, guest_sce_net passes 20 runs in a row on Windows and Linux. Linux passes either way, since its receive timeout runs from the start of the call.
  • In all three runs, every test registered under core/libs/prx/libc and core/libs/prx/libkernel passes: 71 and 136 on Linux, 93 and 140 on Windows.
  • tools/check_conventions.py passes for every commit.
  • The full Build workflow's Windows job at the head, merged into main 8856f180ca: 1265 of 1265 tests pass, 473 skipped, all expected by check_skips.py (run). The workflow carried the runner-image CA-bundle fix (CI only, no source change).

Steam-Link-PS5 (PPSA99350, homebrew, 1.0.0) on Windows 11 Home 10.0.26300, RTX 3070 Laptop (driver 617.42), Ryzen 9 5900HS, 15.4 GB RAM, with Steam running on the same PC and Remote Play on:

  • Built from main at a453108cf, once with main's Export.cpp and once with the first commit's; nothing pressed, so Steam got no stream request.
  • main: the title stays on "LOOKING FOR A PC RUNNING STEAM" for the whole 75 s run, with no discovered line.
  • With the change, 3 s after start:
[SLP5]     0s state: LOOKING FOR A PC RUNNING STEAM
[SLP5]     0s discovered Computer at 169.254.30.114:27036

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
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: PPSA99350 (Steam-Link-PS5, homebrew: finds the PC running Steam)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4861740774 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5966). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-14T17:23:38Z. --- ### What Steam-Link-PS5 (PPSA99350), a homebrew Steam Remote Play client, never finds a PC on Windows. It stays on its first screen while Steam on the same PC answers discovery: ``` [SLP5] 0s state: LOOKING FOR A PC RUNNING STEAM ``` No `discovered ... at ...` line follows. On Windows, `sceNetRecv`, `sceNetRecvfrom` and `sceNetRecvmsg` fail at once with `EINVAL` on a datagram socket that has no local address yet. Winsock's receive returns `WSAEINVAL` for a socket that isn't bound ([`recvfrom`](https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recvfrom)). On the console the receive waits. UDP receives through FreeBSD 11.4's `soreceive_dgram` ([`udp_usrreq.c` L1780](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/sys/netinet/udp_usrreq.c#L1780)), which [sleeps on the receive buffer](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/sys/kern/uipc_socket.c#L2185-L2216) whether or not the socket has an address, and returns `EWOULDBLOCK` for a non-blocking socket. A send from another thread then binds the socket, and the reply arrives. Linux waits the same way. The title's stream library, ihslib, relies on that: - Its worker opens the socket and starts receiving at once ([`base.c` L199-L213](https://github.com/scaronni/ihslib/blob/f68de5f07e0c27ccea9f47f9f5e38fd92c99919e/src/base.c#L199-L213)). Any error other than `EAGAIN`, `EWOULDBLOCK` or `ETIMEDOUT` ends the worker, which closes the socket ([`ihs_udp_posix.c` L68-L78](https://github.com/scaronni/ihslib/blob/f68de5f07e0c27ccea9f47f9f5e38fd92c99919e/src/platforms/ihs_udp_posix.c#L68-L78)). - The discovery broadcast goes out from a timer thread ([`discovery.c` L37-L47](https://github.com/scaronni/ihslib/blob/f68de5f07e0c27ccea9f47f9f5e38fd92c99919e/src/client/discovery.c#L37-L47)), and the title asks for a single one at start ([`main.cpp` L920](https://github.com/jmelovich/Steam-Link-PS5/blob/ab1d8c0d28691b37200fd43f9a50eefc7a7e02de/src/main.cpp#L920)). Its BSD socket calls go straight to libSceNet ([`ps5_sockets.c` L192-L207](https://github.com/jmelovich/Steam-Link-PS5/blob/ab1d8c0d28691b37200fd43f9a50eefc7a7e02de/platform/ps5/ps5_sockets.c#L192-L207)). - On Windows the worker got `EINVAL` and closed the socket before the broadcast went out, so no PC could answer. With this change, on Windows a receive on a datagram socket with no local address waits until a bind, connect or send gives it one. Those calls wake the wait at once, and the wait also checks the host socket every 10 ms: - a non-blocking socket fails with `EAGAIN` at once; - the receive timeout ends the wait with `EAGAIN`, and when the address comes first, the receive waits for a datagram only for what is left of the timeout; - a `sceNetShutdown` of the receive side (`SHUT_RD` or `SHUT_RDWR`) ends the wait, and the receive returns 0, as do later receives while the socket has no address. That's what `soreceive_dgram` does once the socket can't receive more ([L2199-L2203](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/sys/kern/uipc_socket.c#L2199-L2203)). FreeBSD's `soshutdown` flushes the receive side of an unconnected datagram socket and returns `ENOTCONN`, keeping the old behaviour that programs use to release a thread blocked in a receive ([L2352-L2383](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/sys/kern/uipc_socket.c#L2352-L2383)). Without this, a title that stops its receiver thread that way would hang on Windows; - a receive of zero bytes returns 0 without waiting, as `soreceive_dgram` does when nothing was asked for (same lines); - an abort or close ends the wait, and the host receive then fails as before; - once the socket has an address it is noted as bound, so later receives skip the check, and a later `sceNetBind` fails with `EINVAL`, as on the console. Commits: 1. `fix`: the wait. 2. `docs`: records the wait in `docs/dev/debt/libSceNet.md`. 3. `refactor`: in `sceNetRecvmsg` the wait comes after the socket type check. No change in behaviour. 4. `fix`: shutdown and zero-byte receives return 0, with their test and the debt line. 5. `test`: the bind and connect paths, next to the send path the first test covers. 6. `perf`: bind, connect and send wake the wait instead of leaving it to the next 10 ms check. On Windows a connect or send marks the socket bound only once the host socket has its address. 7. `fix`: one receive timeout covers the wait and the receive. Before, when the address came during the wait, the host receive waited its whole timeout again, so a call could take up to twice the timeout; now it polls (`WSAPoll`) only for what is left and fails with `EAGAIN` when that runs out. The debt line says so. Scope: sockets a title opens through libkernel rather than libSceNet (`socket`, then `recv`, `recvfrom`, `recvmsg` or `read` on them) still fail such a receive with `EINVAL` on Windows. That's libkernel's socket layer, which this PR doesn't change. Related: #4614, which gave an unbound socket's `sceNetGetsockname` the zero address on Windows. Overlap: #5141 adds a stream-socket loopback check right above `sceNetConnect`'s `::connect` line, which commit 6 turned into a block for the datagram wake-up. Whichever merges second keeps both. ### Tested On GitHub's runners from my fork: - Linux: Ubuntu 24.04.5, GCC 13.3.0, Release with CI's configure flags and `-DANYPS5_ENABLE_SPIRV_TOOLS=ON`. - Windows: Windows Server 2025, WinLibs MinGW-w64 GCC 15.2.0 r7 (UCRT), Release with CI's configure flags. In `guest_sce_net`: - `CheckUnboundDatagramReceive`: - On an unbound UDP socket with a 50 ms receive timeout (0x1106), `sceNetRecv`, `sceNetRecvfrom` and `sceNetRecvmsg` each fail with `EAGAIN` after at least 40 ms. Made non-blocking (0x1200), `sceNetRecvfrom` fails with `EAGAIN` at once. - A thread waiting in `sceNetRecvfrom` on an unbound socket gets the reply to that socket's first `sceNetSendto`, sent from the main thread. A `sceNetBind` on that socket then fails with `EINVAL`. - On Windows, an abort or a close of the socket ends a receive that waits for an address. - `CheckUnboundDatagramShutdown`: - A thread waiting in `sceNetRecvfrom` on an unbound socket returns 0 within a second of `sceNetShutdown(socket, SHUT_RD)`. After that, `sceNetRecv` returns 0, and `sceNetRecvmsg` returns 0 with no address. - On Windows, `sceNetRecvfrom` with an address buffer returns 0 with size 0. A zero-byte `sceNetRecv` and `sceNetRecvfrom` on a new unbound socket return 0 at once. - `CheckUnboundDatagramLaterAddress`: a thread waits in `sceNetRecv` on an unbound socket while the main thread binds it, or connects it to a peer. In both cases the peer's datagram to the new address reaches the waiting receive. - `CheckUnboundDatagramTimeoutAfterAddress`: with a 500 ms receive timeout, another thread binds the unbound socket 300 ms into `sceNetRecv` and nothing is sent. The receive fails with `EAGAIN` after 450-750 ms. Results: - The first three commits, on `main` at a453108cf ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/38595590661)): - with `main`'s `Export.cpp` the test aborts on Windows. On Linux it passes, since Linux waits as FreeBSD does; - with the change it passes 20 runs in a row on Linux and Windows. - Commits 4-6 ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/38711479945)): - with commit 3's `Export.cpp` and the new tests, Windows aborts: the waiting receive doesn't return after the shutdown; - with the three commits, `guest_sce_net` passes 20 runs in a row on Windows and Linux. On Linux it also passes with commit 3's code, since Linux returns 0 after the shutdown, as FreeBSD does. - Commit 7 ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/38713936727)): - with commit 6's `Export.cpp`, the new case aborts on Windows: that code waits the whole 500 ms timeout again after the 300 ms wait; - with commit 7, `guest_sce_net` passes 20 runs in a row on Windows and Linux. Linux passes either way, since its receive timeout runs from the start of the call. - In all three runs, every test registered under `core/libs/prx/libc` and `core/libs/prx/libkernel` passes: 71 and 136 on Linux, 93 and 140 on Windows. - `tools/check_conventions.py` passes for every commit. - The full Build workflow's Windows job at the head, merged into `main` 8856f180ca: 1265 of 1265 tests pass, 473 skipped, all expected by `check_skips.py` ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/38721544306)). The workflow carried the runner-image CA-bundle fix (CI only, no source change). Steam-Link-PS5 (PPSA99350, homebrew, 1.0.0) on Windows 11 Home 10.0.26300, RTX 3070 Laptop (driver 617.42), Ryzen 9 5900HS, 15.4 GB RAM, with Steam running on the same PC and Remote Play on: - Built from `main` at a453108cf, once with `main`'s `Export.cpp` and once with the first commit's; nothing pressed, so Steam got no stream request. - `main`: the title stays on "LOOKING FOR A PC RUNNING STEAM" for the whole 75 s run, with no `discovered` line. - With the change, 3 s after start: ``` [SLP5] 0s state: LOOKING FOR A PC RUNNING STEAM [SLP5] 0s discovered Computer at 169.254.30.114:27036 ``` ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: PPSA99350 (Steam-Link-PS5, homebrew: finds the PC running Steam) - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-15 09:26:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T09:26:47Z.


Some 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.

Pull request Shared files not compared
PR 6044 core/libs/tests/GuestSceNet.cpp
PR 6286 core/libs/prx/libSceNet/Export.cpp
docs/dev/debt/libSceNet.md
PR 6447 core/libs/prx/libSceNet/Export.cpp
docs/dev/debt/libSceNet.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6188878954 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5966#issuecomment-6188878954). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T09:26:47Z. --- <!-- pr-overlap --> Some 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. | Pull request | Shared files not compared | |---|---| | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestSceNet.cpp` | | [PR 6286](https://www.github.com/boykopovar/AnyPS5/pull/6286) | `core/libs/prx/libSceNet/Export.cpp`<br>`docs/dev/debt/libSceNet.md` | | [PR 6447](https://www.github.com/boykopovar/AnyPS5/pull/6447) | `core/libs/prx/libSceNet/Export.cpp`<br>`docs/dev/debt/libSceNet.md` |
adriadam10 commented 2026-10-16 19:05:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T19:05:52Z.


Checked the diff, guest_sce_net and the libSceNet/libs scope on Linux (37/37). The wait is Windows-only, so the Windows behaviour rests on the full Build's windows job and your run. The locking in wait_for_local_address and the bind/connect/send wake-ups look right; merging once that is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6222735327 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5966#issuecomment-6222735327). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-16T19:05:52Z. --- Checked the diff, `guest_sce_net` and the libSceNet/libs scope on Linux (37/37). The wait is Windows-only, so the Windows behaviour rests on the full Build's windows job and your run. The locking in `wait_for_local_address` and the bind/connect/send wake-ups look right; merging once that is green.
adriadam10 commented 2026-10-16 21:35:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T21:35:57Z.


Re-checked against the current main: it merges cleanly and guest_sce_net passes on the merged tree. The wait is Windows-only, so the Windows behaviour rests on the full Build's windows job and your run; merging once that is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6225157057 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5966#issuecomment-6225157057). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-16T21:35:57Z. --- Re-checked against the current `main`: it merges cleanly and `guest_sce_net` passes on the merged tree. The wait is Windows-only, so the Windows behaviour rests on the full Build's windows job and your run; merging once that is green.
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!5966
No description provided.