fix(libkernel): answer a receive after SHUT_RD or with no buffer without the host #8492

Merged
xXPBSXx merged 3 commits from upstream-pr/8492 into main 2026-10-21 18:39:37 +00:00
xXPBSXx commented 2026-10-20 13:46:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T13:46:59Z.


What

recv and recvfrom handed every receive to the host. On Windows that fails a receive after shutdown(SHUT_RD) with WSAESHUTDOWN (reported as EPIPE) and blocks a zero-length receive on a stream socket until data arrives. FreeBSD 11.4's soreceive_generic returns 0 for a socket whose receive side is shut and for a zero-length receive on a connected stream socket (ENOTCONN when it is not connected), without waiting; TCP uses it in 11.4. Both calls now answer these cases themselves, before any wait. recvfrom also reports a zero address length. recvmsg is unchanged. The Technical debt entry says so.
Fixes #8491
Overlap: git merge-tree has conflicts with #7090 (one), #8448 (one) and #8474 (two) in Udp.cpp; the others (#8392, #8463, #8483, #7511, #7850, #7490, #8312) are clean. Whichever lands second rebases. pr_overlap was not run.

Tested

Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on upstream/main cabd80f472 + this commit:

  • guest_tcp: the new case aborts without the change to Udp.cpp and passes with it. It checks a zero-length receive on an unconnected socket (ENOTCONN) and on a connected one with and without MSG_DONTWAIT (0), recvfrom reporting address length 0, and recv/recvfrom returning 0 after shutdown(SHUT_RD) even though the peer sent data.
  • guest_datagram_recv, guest_sce_net, guest_socket_error, guest_socket_timeouts, guest_udp, guest_poll, guest_socket_pair and guest_recvmsg_memory pass.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux/macOS, a console, a title run, recvmsg.

Checklist

  • Based on current main; no other open PR implements the same functions (#8483 and #7090 touch Udp.cpp elsewhere)
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; no silent stubs 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: #8491
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4936709553 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8492). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T13:46:59Z. --- ### What `recv` and `recvfrom` handed every receive to the host. On Windows that fails a receive after `shutdown(SHUT_RD)` with `WSAESHUTDOWN` (reported as `EPIPE`) and blocks a zero-length receive on a stream socket until data arrives. FreeBSD 11.4's [`soreceive_generic`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L1556-L1578) returns 0 for a socket whose receive side is shut and for a zero-length receive on a connected stream socket (`ENOTCONN` when it is not connected), without waiting; TCP uses it in 11.4. Both calls now answer these cases themselves, before any wait. `recvfrom` also reports a zero address length. `recvmsg` is unchanged. The Technical debt entry says so. Fixes #8491 Overlap: `git merge-tree` has conflicts with #7090 (one), #8448 (one) and #8474 (two) in `Udp.cpp`; the others (#8392, #8463, #8483, #7511, #7850, #7490, #8312) are clean. Whichever lands second rebases. `pr_overlap` was not run. ### Tested Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on upstream/main cabd80f472 + this commit: - `guest_tcp`: the new case aborts without the change to `Udp.cpp` and passes with it. It checks a zero-length receive on an unconnected socket (`ENOTCONN`) and on a connected one with and without `MSG_DONTWAIT` (0), `recvfrom` reporting address length 0, and `recv`/`recvfrom` returning 0 after `shutdown(SHUT_RD)` even though the peer sent data. - `guest_datagram_recv`, `guest_sce_net`, `guest_socket_error`, `guest_socket_timeouts`, `guest_udp`, `guest_poll`, `guest_socket_pair` and `guest_recvmsg_memory` pass. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux/macOS, a console, a title run, `recvmsg`. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions (#8483 and #7090 touch `Udp.cpp` elsewhere) - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw; no silent stubs 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: #8491 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-20 13:55:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T13:55:38Z.


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.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 7090 docs/dev/debt/libkernel.md L191

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 4630 docs/dev/debt/libkernel.md
PR 5021 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 6044 core/libs/tests/GuestTcp.cpp
PR 6149 docs/dev/debt/libkernel.md
PR 6469 docs/dev/debt/libkernel.md
PR 6740 docs/dev/debt/libkernel.md
PR 6879 docs/dev/debt/libkernel.md
PR 6936 docs/dev/debt/libkernel.md
PR 7088 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7192 docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 7447 docs/dev/debt/libkernel.md
PR 7509 docs/dev/debt/libkernel.md
PR 7653 docs/dev/debt/libkernel.md
PR 8520 docs/dev/debt/libkernel.md
PR 8547 docs/dev/debt/libkernel.md
PR 8726 docs/dev/debt/libkernel.md
PR 9137 docs/dev/debt/libkernel.md
PR 9169 core/libs/prx/libkernel/Socket/src/Udp.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6292542004 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8492#issuecomment-6292542004). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T13:55:38Z. --- <!-- pr-overlap --> 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**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 7090](https://www.github.com/boykopovar/AnyPS5/pull/7090) | `docs/dev/debt/libkernel.md` [L191](https://github.com/boykopovar/AnyPS5/blob/ea241c01b388b20b22d4586dfeaacef9675704d7/docs/dev/debt/libkernel.md#L191) | | | 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 4630](https://www.github.com/boykopovar/AnyPS5/pull/4630) | `docs/dev/debt/libkernel.md` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/Socket/src/Udp.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestTcp.cpp` | | [PR 6149](https://www.github.com/boykopovar/AnyPS5/pull/6149) | `docs/dev/debt/libkernel.md` | | [PR 6469](https://www.github.com/boykopovar/AnyPS5/pull/6469) | `docs/dev/debt/libkernel.md` | | [PR 6740](https://www.github.com/boykopovar/AnyPS5/pull/6740) | `docs/dev/debt/libkernel.md` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `docs/dev/debt/libkernel.md` | | [PR 6936](https://www.github.com/boykopovar/AnyPS5/pull/6936) | `docs/dev/debt/libkernel.md` | | [PR 7088](https://www.github.com/boykopovar/AnyPS5/pull/7088) | `core/libs/prx/libkernel/Socket/src/Udp.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 7105](https://www.github.com/boykopovar/AnyPS5/pull/7105) | `docs/dev/debt/libkernel.md` | | [PR 7149](https://www.github.com/boykopovar/AnyPS5/pull/7149) | `docs/dev/debt/libkernel.md` | | [PR 7192](https://www.github.com/boykopovar/AnyPS5/pull/7192) | `docs/dev/debt/libkernel.md` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/Socket/src/Udp.cpp` | | [PR 7447](https://www.github.com/boykopovar/AnyPS5/pull/7447) | `docs/dev/debt/libkernel.md` | | [PR 7509](https://www.github.com/boykopovar/AnyPS5/pull/7509) | `docs/dev/debt/libkernel.md` | | [PR 7653](https://www.github.com/boykopovar/AnyPS5/pull/7653) | `docs/dev/debt/libkernel.md` | | [PR 8520](https://www.github.com/boykopovar/AnyPS5/pull/8520) | `docs/dev/debt/libkernel.md` | | [PR 8547](https://www.github.com/boykopovar/AnyPS5/pull/8547) | `docs/dev/debt/libkernel.md` | | [PR 8726](https://www.github.com/boykopovar/AnyPS5/pull/8726) | `docs/dev/debt/libkernel.md` | | [PR 9137](https://www.github.com/boykopovar/AnyPS5/pull/9137) | `docs/dev/debt/libkernel.md` | | [PR 9169](https://www.github.com/boykopovar/AnyPS5/pull/9169) | `core/libs/prx/libkernel/Socket/src/Udp.cpp` |
fogydes commented 2026-10-20 15:09:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fogydes on 2026-10-20T15:09:28Z.

Review: COMMENTED


Tested this change against current main.

Notes:

  • Conventions check clean (tools/check_conventions.py).
  • Built guest_tcp_tests and ran CTest locally on Windows (MinGW-w64):
    1/1 Test #1534: guest_tcp ........................   Passed    0.09 sec
    100% tests passed, 0 tests failed out of 1
    
  • Verified the FreeBSD 11.4 soreceive_generic emulation matches expectations:
    • Bypassing the host for socket.receiveShut avoids Winsock's WSAESHUTDOWN returning EPIPE to the guest, instead cleanly returning EOF (0).
    • Zero-length stream receive returns 0 when connected (or ENOTCONN when unconnected via getpeername), avoiding Winsock's blocking behavior on 0-length reads.
    • In recvfrom, correctly clears *addressLength = 0 on answered receives where no sender address is produced.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5587093606 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8492#pullrequestreview-5587093606). Originally posted by [fogydes](https://github.com/fogydes) on 2026-10-20T15:09:28Z. **Review:** COMMENTED --- Tested this change against current `main`. Notes: - Conventions check clean (`tools/check_conventions.py`). - Built `guest_tcp_tests` and ran CTest locally on Windows (MinGW-w64): ``` 1/1 Test #1534: guest_tcp ........................ Passed 0.09 sec 100% tests passed, 0 tests failed out of 1 ``` - Verified the FreeBSD 11.4 `soreceive_generic` emulation matches expectations: - Bypassing the host for `socket.receiveShut` avoids Winsock's `WSAESHUTDOWN` returning `EPIPE` to the guest, instead cleanly returning EOF (`0`). - Zero-length stream receive returns `0` when connected (or `ENOTCONN` when unconnected via `getpeername`), avoiding Winsock's blocking behavior on 0-length reads. - In `recvfrom`, correctly clears `*addressLength = 0` on answered receives where no sender address is produced.
xXPBSXx commented 2026-10-21 10:42:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T10:42:56Z.


On Linux (WSL2 Ubuntu 24.04, GCC 13.3, Release; not a bare-metal CI) guest_socket_pair aborts at head bd895d132 and passes 30 of 30 runs at the merge base cabd80f47 (ctest --repeat until-fail:30). GuestSocketPair.cpp has a non-Windows branch that calls shutdown(pair[1], SHUT_RD) and then expects recv(pair[1]) to return the 5 buffered bytes and fstat to report st_size == 5 (lines 227-229). With ReceiveAnsweredHere a receive after receiveShut now returns 0 on every host, so that recv returns 0 instead of 5. FreeBSD discards the receive buffer on SHUT_RD (soshutdown calls sorflush), so 0 is what the console does, but the Linux/macOS branch of the test and the host behaviour pin the buffered data. Either the receiveShut shortcut stays Windows-only (the PR title says Windows), or the non-Windows branch of guest_socket_pair changes to the FreeBSD result in the same PR; I did not try either. Not tested: macOS, a console.
AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6314009695 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8492#issuecomment-6314009695). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T10:42:56Z. --- On Linux (WSL2 Ubuntu 24.04, GCC 13.3, Release; not a bare-metal CI) `guest_socket_pair` aborts at head bd895d132 and passes 30 of 30 runs at the merge base cabd80f47 (`ctest --repeat until-fail:30`). `GuestSocketPair.cpp` has a non-Windows branch that calls `shutdown(pair[1], SHUT_RD)` and then expects `recv(pair[1])` to return the 5 buffered bytes and `fstat` to report `st_size == 5` (lines 227-229). With `ReceiveAnsweredHere` a receive after `receiveShut` now returns 0 on every host, so that `recv` returns 0 instead of 5. FreeBSD discards the receive buffer on `SHUT_RD` (`soshutdown` calls `sorflush`), so 0 is what the console does, but the Linux/macOS branch of the test and the host behaviour pin the buffered data. Either the `receiveShut` shortcut stays Windows-only (the PR title says Windows), or the non-Windows branch of `guest_socket_pair` changes to the FreeBSD result in the same PR; I did not try either. Not tested: macOS, a console. AI-assisted (Claude Code).
xXPBSXx commented 2026-10-21 10:54:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T10:54:13Z.


Follow-up for the Linux failure reported above. The receiveShut shortcut in ReceiveAnsweredHere now runs on Windows only (#ifdef _WIN32), so Linux keeps its host behaviour: data buffered before shutdown(SHUT_RD) is still readable, which guest_socket_pair expects. The post-shutdown receives in ReceiveAnsweredWithoutTheHost (guest_tcp) are Windows-only for the same reason; the no-buffer part (zero-length receive, ENOTCONN) still runs everywhere. I chose this over making the shortcut FreeBSD-exact on Linux because the PR title and the reported case are Windows, and it leaves the Linux branch of guest_socket_pair as it is.

Checked: Linux (WSL2 Ubuntu 24.04, GCC 13.3, Release; not a bare-metal CI): guest_socket_pair, guest_udp and guest_tcp pass 30 consecutive runs each at the new head (ctest --repeat until-fail:30); at the old head bd895d1325 guest_socket_pair aborts and guest_tcp passes, and with only the Udp.cpp change guest_tcp aborts on its post-shutdown receives, which is why the test is Windows-only now. Windows 10 (MinGW GCC 15.2, Release): the same three tests pass 5 of 5 runs. check_conventions passes. Not tested: macOS, a console.
AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6314201234 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8492#issuecomment-6314201234). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T10:54:13Z. --- Follow-up for the Linux failure reported above. The `receiveShut` shortcut in `ReceiveAnsweredHere` now runs on Windows only (`#ifdef _WIN32`), so Linux keeps its host behaviour: data buffered before `shutdown(SHUT_RD)` is still readable, which `guest_socket_pair` expects. The post-shutdown receives in `ReceiveAnsweredWithoutTheHost` (`guest_tcp`) are Windows-only for the same reason; the no-buffer part (zero-length receive, `ENOTCONN`) still runs everywhere. I chose this over making the shortcut FreeBSD-exact on Linux because the PR title and the reported case are Windows, and it leaves the Linux branch of `guest_socket_pair` as it is. Checked: Linux (WSL2 Ubuntu 24.04, GCC 13.3, Release; not a bare-metal CI): `guest_socket_pair`, `guest_udp` and `guest_tcp` pass 30 consecutive runs each at the new head (`ctest --repeat until-fail:30`); at the old head bd895d1325 `guest_socket_pair` aborts and `guest_tcp` passes, and with only the `Udp.cpp` change `guest_tcp` aborts on its post-shutdown receives, which is why the test is Windows-only now. Windows 10 (MinGW GCC 15.2, Release): the same three tests pass 5 of 5 runs. `check_conventions` passes. Not tested: macOS, a console. AI-assisted (Claude Code).
adriadam10 commented 2026-10-21 18:40:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-21T18:40:31Z.


Checked the diff against FreeBSD 11.4 soreceive_generic (SBS_CANTRCVMORE returns 0, zero-length stream receive returns 0 or ENOTCONN), the new guest_tcp case (the zero-length rows fail without the change) and the Linux guest_socket_pair follow-up. Rebased onto main (conflicts with #8474 in GuestTcp.cpp and docs/dev/debt/libkernel.md, both kept) and pushed one commit: the debt entry now says the post-SHUT_RD answer is Windows only and that Linux/macOS still return data buffered before the shutdown; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6323716100 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8492#issuecomment-6323716100). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-21T18:40:31Z. --- Checked the diff against FreeBSD 11.4 `soreceive_generic` (SBS_CANTRCVMORE returns 0, zero-length stream receive returns 0 or `ENOTCONN`), the new `guest_tcp` case (the zero-length rows fail without the change) and the Linux `guest_socket_pair` follow-up. Rebased onto `main` (conflicts with #8474 in `GuestTcp.cpp` and `docs/dev/debt/libkernel.md`, both kept) and pushed one commit: the debt entry now says the post-`SHUT_RD` answer is Windows only and that Linux/macOS still return data buffered before the shutdown; merged.
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!8492
No description provided.