fix(libkernel): drop the data a socket held before shutdown(SHUT_RD) in every receive path #10767

Merged
Ragbie merged 6 commits from upstream-pr/10767 into main 2026-10-27 05:14:19 +00:00
Ragbie commented 2026-10-24 23:33:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ragbie on 2026-10-24T23:33:00Z.


What

After shutdown(SHUT_RD) a guest socket still hands out data it held before the call:

  • On Linux and macOS, recv, recvfrom, read and readv return the buffered bytes: the check that answers 0 for a shut receive side was Windows-only.
  • recvmsg never made that check. On Windows it fails with EPIPE (Winsock's WSAESHUTDOWN); on Linux it returns the buffered bytes.
  • fstat (st_size, read bits), FIONREAD and the EVFILT_READ event's data still count them.
  • EVFILT_READ takes EV_EOF from the host's poll flags only. On Windows, with nothing queued, WSAPoll reports nothing after SD_RECEIVE, so the filter never fires, and a kevent or sceKernelWaitEqueue already waiting on the socket sleeps until its timeout.
  • On a datagram socket that is not connected, the host shutdown fails with ENOTCONN (Linux) or succeeds (Windows), and the shut side was not recorded either way, so queued datagrams stayed readable.

FreeBSD 11.4 soshutdown calls sorflush for SHUT_RD. That marks the receive side SBS_CANTRCVMORE, empties the buffer and wakes the socket's readers (socantrcvmore → sorwakeup), so soreceive returns 0, soo_stat reports no bytes and no read bits, and filt_soread reports data 0 with EV_EOF (a listening socket uses filt_solisten, which has no EOF). On a datagram socket that is not connected it still flushes, then returns ENOTCONN.

The change, in core/libs/prx/libkernel/Socket/src/Udp.cpp and core/libs/prx/libkernel/Equeue/:

  • ReceiveShut reads the recorded state. recv, recvfrom (through ReceiveAnsweredHere) and recvmsg return 0 on every host once the receive side is shut; recvmsg skips the host receive and reports msg_namelen 0.
  • GuestSockets::Stat and the FIONREAD ioctl report no queued bytes after SHUT_RD. GuestSockets::Ready answers a read filter on a non-listening socket shut for reading without asking the host: data 0 and EV_EOF.
  • shutdown checks getpeername on a datagram socket; when it has no peer, it records the shut sides whatever the host returned and fails with ENOTCONN. When the receive side is shut, it wakes the equeues and kqueues that hold an EVFILT_READ event for any descriptor of the socket, including ones from dup, dup2 or F_DUPFD (EqueueDescriptorReadShut, KqueueDescriptorReadShut), so a waiter polls the socket again. FreeBSD hangs the read knote on the socket itself, so a shutdown through any descriptor fires it.
  • The technical debt entry is cut to what is still open: on Windows SHUT_RD with unread or later data resets a TCP connection, so the peer's next send fails with EPIPE; and FreeBSD keeps appending a Unix stream peer's later data (uipc_send does not check the receiver's SBS_CANTRCVMORE), which receives here no longer return. The socket error entry no longer lists the EPIPE receive.

Tested

Windows 11, WinLibs GCC 15.2.0, and Linux (Ubuntu 24.04 container, GCC 13.3.0), rebased onto main 6cc5ec671 (after #9625; shutdown keeps its AlreadyShut check beside the unconnected-datagram one):

  • guest_tcp: the Windows-only SHUT_RD block now runs on every host, with a byte queued before the shutdown, FIONREAD and recvmsg.
  • guest_socket_pair: it asserted Linux's behaviour of reading buffered data after SHUT_RD. It now expects FreeBSD's, and checks msg_namelen for a Unix datagram socket.
  • guest_udp: SHUT_RD on an unconnected socket with a queued datagram.
  • guest_kqueue: level-triggered EVFILT_READ after SHUT_RD reports data 0 with EV_EOF, with and without bytes queued before it, and a kevent already waiting when another thread shuts the socket returns within the file's usual bounds (at least 40 ms, under 4 s, 5 s timeout), whether it watches the shut descriptor or a dup of it.
  • With main's Udp.cpp, the first four fail on Linux, and guest_tcp, guest_udp and guest_socket_pair fail on Windows. With the change they pass with --repeat until-fail:5.
  • The EV_EOF and wakeup cases fail on Windows without the last commit: with nothing queued the filter reports no event, and the waiter only returns at its timeout. With bytes queued, Windows already reported EV_EOF, because SD_RECEIVE with unread data resets the loopback connection. Linux passes them before and after, since the host reports POLLRDHUP and wakes the waiter. The dup waiter also failed on Windows until the wake covered every descriptor of the socket. With the change guest_kqueue passes with --repeat until-fail:5, and the 78 tests matching socket|tcp|udp|kqueue|equeue|guest_net|select|poll with --repeat until-fail:3.
  • Windows full build and ctest on main 8a29e0883: 99% tests passed, 5 tests failed out of 2378. agc_write_tracking_metadata_commit, agc_host_import_cut_lifetime, agc_driver_depth_plane_partial_store and agc_driver_image_plane_address_on_line fail on main on this machine too, and so does guest_wide_formatting (wcsftime length matches strftime). check_skips.py: all skips expected.
  • Linux full ctest: 99% tests passed, 24 tests failed out of 2285; the same 24 fail on main in this container (22 agc_* on its lavapipe and kernel without UFFD_FEATURE_WP_ASYNC, pr_overlap_encoding and guest_system_load_exec_standard_error).
  • macOS: not tested.

#7090 rewrites the body of recvmsg and #8605 changes the read-bit line in GuestSockets::Stat; this PR leaves that line as it is, and either order rebases with a small conflict in recvmsg. #7090 and #11006 also add entries next to this PR's in docs/dev/debt/libkernel.md. None of them needs this PR or is needed by it: whichever lands second resolves the conflict and keeps both changes. #11141 declares it lands after this PR.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5000137341 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10767). Originally posted by [Ragbie](https://github.com/Ragbie) on 2026-10-24T23:33:00Z. --- ### What After `shutdown(SHUT_RD)` a guest socket still hands out data it held before the call: - On Linux and macOS, `recv`, `recvfrom`, `read` and `readv` return the buffered bytes: the check that answers 0 for a shut receive side was Windows-only. - `recvmsg` never made that check. On Windows it fails with `EPIPE` (Winsock's `WSAESHUTDOWN`); on Linux it returns the buffered bytes. - `fstat` (`st_size`, read bits), `FIONREAD` and the `EVFILT_READ` event's `data` still count them. - `EVFILT_READ` takes `EV_EOF` from the host's poll flags only. On Windows, with nothing queued, `WSAPoll` reports nothing after `SD_RECEIVE`, so the filter never fires, and a `kevent` or `sceKernelWaitEqueue` already waiting on the socket sleeps until its timeout. - On a datagram socket that is not connected, the host `shutdown` fails with `ENOTCONN` (Linux) or succeeds (Windows), and the shut side was not recorded either way, so queued datagrams stayed readable. FreeBSD 11.4 [`soshutdown`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L2343-L2385) calls [`sorflush`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L2387-L2432) for `SHUT_RD`. That marks the receive side `SBS_CANTRCVMORE`, empties the buffer and wakes the socket's readers (`socantrcvmore` → `sorwakeup`), so `soreceive` returns 0, `soo_stat` reports no bytes and no read bits, and [`filt_soread`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L3317-L3343) reports `data` 0 with `EV_EOF` (a listening socket uses `filt_solisten`, which has no EOF). On a datagram socket that is not connected it still flushes, then returns `ENOTCONN`. The change, in `core/libs/prx/libkernel/Socket/src/Udp.cpp` and `core/libs/prx/libkernel/Equeue/`: - `ReceiveShut` reads the recorded state. `recv`, `recvfrom` (through `ReceiveAnsweredHere`) and `recvmsg` return 0 on every host once the receive side is shut; `recvmsg` skips the host receive and reports `msg_namelen` 0. - `GuestSockets::Stat` and the `FIONREAD` ioctl report no queued bytes after `SHUT_RD`. `GuestSockets::Ready` answers a read filter on a non-listening socket shut for reading without asking the host: `data` 0 and `EV_EOF`. - `shutdown` checks `getpeername` on a datagram socket; when it has no peer, it records the shut sides whatever the host returned and fails with `ENOTCONN`. When the receive side is shut, it wakes the equeues and kqueues that hold an `EVFILT_READ` event for any descriptor of the socket, including ones from `dup`, `dup2` or `F_DUPFD` (`EqueueDescriptorReadShut`, `KqueueDescriptorReadShut`), so a waiter polls the socket again. FreeBSD hangs the read knote on the socket itself, so a shutdown through any descriptor fires it. - The technical debt entry is cut to what is still open: on Windows `SHUT_RD` with unread or later data resets a TCP connection, so the peer's next send fails with `EPIPE`; and FreeBSD keeps appending a Unix stream peer's later data ([`uipc_send`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_usrreq.c#L936-L1001) does not check the receiver's `SBS_CANTRCVMORE`), which receives here no longer return. The socket error entry no longer lists the `EPIPE` receive. ### Tested Windows 11, WinLibs GCC 15.2.0, and Linux (Ubuntu 24.04 container, GCC 13.3.0), rebased onto `main` `6cc5ec671` (after #9625; `shutdown` keeps its `AlreadyShut` check beside the unconnected-datagram one): - `guest_tcp`: the Windows-only `SHUT_RD` block now runs on every host, with a byte queued before the shutdown, `FIONREAD` and `recvmsg`. - `guest_socket_pair`: it asserted Linux's behaviour of reading buffered data after `SHUT_RD`. It now expects FreeBSD's, and checks `msg_namelen` for a Unix datagram socket. - `guest_udp`: `SHUT_RD` on an unconnected socket with a queued datagram. - `guest_kqueue`: level-triggered `EVFILT_READ` after `SHUT_RD` reports `data` 0 with `EV_EOF`, with and without bytes queued before it, and a `kevent` already waiting when another thread shuts the socket returns within the file's usual bounds (at least 40 ms, under 4 s, 5 s timeout), whether it watches the shut descriptor or a `dup` of it. - With `main`'s `Udp.cpp`, the first four fail on Linux, and `guest_tcp`, `guest_udp` and `guest_socket_pair` fail on Windows. With the change they pass with `--repeat until-fail:5`. - The `EV_EOF` and wakeup cases fail on Windows without the last commit: with nothing queued the filter reports no event, and the waiter only returns at its timeout. With bytes queued, Windows already reported `EV_EOF`, because `SD_RECEIVE` with unread data resets the loopback connection. Linux passes them before and after, since the host reports `POLLRDHUP` and wakes the waiter. The `dup` waiter also failed on Windows until the wake covered every descriptor of the socket. With the change `guest_kqueue` passes with `--repeat until-fail:5`, and the 78 tests matching `socket|tcp|udp|kqueue|equeue|guest_net|select|poll` with `--repeat until-fail:3`. - Windows full build and `ctest` on `main` `8a29e0883`: `99% tests passed, 5 tests failed out of 2378`. `agc_write_tracking_metadata_commit`, `agc_host_import_cut_lifetime`, `agc_driver_depth_plane_partial_store` and `agc_driver_image_plane_address_on_line` fail on `main` on this machine too, and so does `guest_wide_formatting` (`wcsftime length matches strftime`). `check_skips.py`: all skips expected. - Linux full `ctest`: `99% tests passed, 24 tests failed out of 2285`; the same 24 fail on `main` in this container (22 `agc_*` on its lavapipe and kernel without `UFFD_FEATURE_WP_ASYNC`, `pr_overlap_encoding` and `guest_system_load_exec_standard_error`). - macOS: not tested. #7090 rewrites the body of `recvmsg` and #8605 changes the read-bit line in `GuestSockets::Stat`; this PR leaves that line as it is, and either order rebases with a small conflict in `recvmsg`. #7090 and #11006 also add entries next to this PR's in `docs/dev/debt/libkernel.md`. None of them needs this PR or is needed by it: whichever lands second resolves the conflict and keeps both changes. #11141 declares it lands after this PR. ### 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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-24 23:40:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T23:40:00Z.

Review: APPROVED


Reviewed the common receive paths and the state updates for SHUT_RD. The same receive-shut state now gates recv/recvfrom/read/readv/recvmsg, queued-byte reporting, and the UDP unconnected case; the new tests exercise queued data, FIONREAD, recvmsg name length, and kqueue data after shutdown. The author reports repeated Linux and Windows focused runs, and accurately records four unrelated failures in the Windows full suite. Conventions and intake pass; hosted build suites were skipped by path filters.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640620033 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10767#pullrequestreview-5640620033). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T23:40:00Z. **Review:** APPROVED --- Reviewed the common receive paths and the state updates for SHUT_RD. The same receive-shut state now gates recv/recvfrom/read/readv/recvmsg, queued-byte reporting, and the UDP unconnected case; the new tests exercise queued data, FIONREAD, recvmsg name length, and kqueue data after shutdown. The author reports repeated Linux and Windows focused runs, and accurately records four unrelated failures in the Windows full suite. Conventions and intake pass; hosted build suites were skipped by path filters.
MarcZX10 commented 2026-10-25 00:17:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T00:17:24Z.

Review: DISMISSED


The receive-side state is now part of the readiness contract too. One kqueue EOF case is still missing:

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640734099 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10767#pullrequestreview-5640734099). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T00:17:24Z. **Review:** DISMISSED --- The receive-side state is now part of the readiness contract too. One kqueue EOF case is still missing:
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!10767
No description provided.