fix(libkernel): fail sends and receives on a socketpair end whose peer closed on Windows #15785

Closed
Dom3005 wants to merge 1 commit from upstream-pr/15785 into main
Dom3005 commented 2026-11-04 14:47:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dom3005 on 2026-11-04T14:47:18Z.


What

On Windows a guest socketpair end is a loopback TCP connection or two connected UDP sockets, so the host did not see the peer's close the way FreeBSD does (#15661). Both ends live in this process and the socket table already links them, and on Windows nothing else can own the host socket (no SCM_RIGHTS), so an expired peer link means the peer is closed.

  • Stream: a send after the peer closed fails with EPIPE from the first call. Before, the loopback connection accepted one more send and only the ones after it reported EPIPE. This follows FreeBSD 11.4's unp_disconnect, which calls soisdisconnected on both ends.
  • Datagram: follows FreeBSD 11.4. unp_drop sets so_error = ECONNRESET on the surviving end. soreceive_dgram returns it once, and only when the queue is empty (queued datagrams still arrive first; MSG_PEEK leaves the error), or sosend_generic takes it on the next send. After that, a send without an address fails with EDESTADDRREQ. The pair's UDP sockets now have SIO_UDP_CONNRESET off, as guest UDP sockets already do, so Winsock's port-unreachable resets (which only arrived after a send, and repeated) no longer leak through.

This answers #15661 differently from what the issue asks for the datagram case. The issue and the old debt entry say the console returns ENOTCONN, but that sentence comes from the original socketpair commit (8f31696450) and cites no measurement, and FreeBSD 11.4 gives ECONNRESET as described above. The debt entry now cites the FreeBSD source and says the console is unmeasured. It also records what is still missing on Windows: poll, kevent and SO_ERROR don't report the pending error, and a receive that is already blocked when the peer closes keeps waiting.

Tested

Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0, Ninja, Release, BUILD_TESTING=ON, on upstream/main b11c7bf84f.

  • guest_socket_pair and guest_recvmsg_memory: passed 5/5 (ctest --repeat until-fail:5).
  • Negative control: the new test against main's Udp.cpp aborts (0xc0000409) in guest_socket_pair.
  • Related tests, ctest -R "socket|sock|recv|send|pipe|poll|select|kevent|kqueue|equeue|net|dup|fstat|udp|tcp": 253/253 passed.
  • Full incremental build: no failures.
  • python tools/check_conventions.py --base upstream/main: 0 errors. The one warning is debt-length on the socketpair entry, which was already 1279 characters on main.

Not tested: Linux and macOS. The code change only takes effect on Windows. The test change that also runs there is the stream case, which now expects EPIPE on the first two sends after the peer closed, where it used to loop up to 100 times.

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: #15661
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5156256989 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15785). Originally posted by [Dom3005](https://github.com/Dom3005) on 2026-11-04T14:47:18Z. --- ### What On Windows a guest `socketpair` end is a loopback TCP connection or two connected UDP sockets, so the host did not see the peer's close the way FreeBSD does (#15661). Both ends live in this process and the socket table already links them, and on Windows nothing else can own the host socket (no `SCM_RIGHTS`), so an expired peer link means the peer is closed. - **Stream:** a `send` after the peer closed fails with `EPIPE` from the first call. Before, the loopback connection accepted one more send and only the ones after it reported `EPIPE`. This follows FreeBSD 11.4's `unp_disconnect`, which calls `soisdisconnected` on both ends. - **Datagram:** follows FreeBSD 11.4. [`unp_drop`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_usrreq.c#L1726-L1747) sets `so_error = ECONNRESET` on the surviving end. [`soreceive_dgram`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L2193-L2198) returns it once, and only when the queue is empty (queued datagrams still arrive first; `MSG_PEEK` leaves the error), or `sosend_generic` takes it on the next send. After that, a send without an address fails with `EDESTADDRREQ`. The pair's UDP sockets now have `SIO_UDP_CONNRESET` off, as guest UDP sockets already do, so Winsock's port-unreachable resets (which only arrived after a send, and repeated) no longer leak through. This answers #15661 differently from what the issue asks for the datagram case. The issue and the old debt entry say the console returns `ENOTCONN`, but that sentence comes from the original socketpair commit (8f31696450) and cites no measurement, and FreeBSD 11.4 gives `ECONNRESET` as described above. The debt entry now cites the FreeBSD source and says the console is unmeasured. It also records what is still missing on Windows: `poll`, `kevent` and `SO_ERROR` don't report the pending error, and a receive that is already blocked when the peer closes keeps waiting. ### Tested Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0, Ninja, Release, `BUILD_TESTING=ON`, on `upstream/main` `b11c7bf84f`. - `guest_socket_pair` and `guest_recvmsg_memory`: passed 5/5 (`ctest --repeat until-fail:5`). - Negative control: the new test against `main`'s `Udp.cpp` aborts (`0xc0000409`) in `guest_socket_pair`. - Related tests, `ctest -R "socket|sock|recv|send|pipe|poll|select|kevent|kqueue|equeue|net|dup|fstat|udp|tcp"`: 253/253 passed. - Full incremental build: no failures. - `python tools/check_conventions.py --base upstream/main`: 0 errors. The one warning is `debt-length` on the socketpair entry, which was already 1279 characters on `main`. Not tested: Linux and macOS. The code change only takes effect on Windows. The test change that also runs there is the stream case, which now expects `EPIPE` on the first two sends after the peer closed, where it used to loop up to 100 times. ### 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: #15661 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
adriadam10 commented 2026-11-07 15:57:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-11-07T15:57:14Z.

Review: CHANGES_REQUESTED


Thanks for the work. #16081 merged yesterday and already covers most of this: a stream send after the peer closed fails with EPIPE from the first call, and an empty datagram recv after the peer closed fails with ECONNRESET (54), both measured on a PS5 (13.60 and 11.60), with the debt entry updated. This PR now conflicts with main in Udp.cpp and docs/dev/debt/libkernel.md.

What is left of the diff is behaviour main does not have, and it rests on reading FreeBSD, not on the console:

  • One-shot ECONNRESET (peerResetTaken, Udp.cpp:133, PendingPeerReset), where main keeps answering 54 on every empty receive. The console measurement only covers the first receive after the close. Please measure a second recv and a send on the console; if it differs from main, add that measurement to the PR.
  • EDESTADDRREQ (39) for a send without an address after the reset (ClosedPeerSendError, Udp.cpp:919-929), and ECONNRESET on the first send. Same: unmeasured, and the debt entry says so.
  • SIO_UDP_CONNRESET off on the pair's UDP sockets (Udp.cpp:1025-1030). Separate fix and independent of the above; fine as its own small PR with a test that fails without it.

Please rebase onto current main and keep only the parts the console measurements support (a stream EPIPE and the empty-queue recv are done, so drop the duplicates, including the test and the debt rewrite of those). The debt entry should then add only what remains.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5813420116 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15785#pullrequestreview-5813420116). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-11-07T15:57:14Z. **Review:** CHANGES_REQUESTED --- Thanks for the work. #16081 merged yesterday and already covers most of this: a stream `send` after the peer closed fails with `EPIPE` from the first call, and an empty datagram `recv` after the peer closed fails with `ECONNRESET` (54), both measured on a PS5 (13.60 and 11.60), with the debt entry updated. This PR now conflicts with `main` in `Udp.cpp` and `docs/dev/debt/libkernel.md`. What is left of the diff is behaviour main does not have, and it rests on reading FreeBSD, not on the console: - One-shot `ECONNRESET` (`peerResetTaken`, `Udp.cpp:133`, `PendingPeerReset`), where main keeps answering 54 on every empty receive. The console measurement only covers the first receive after the close. Please measure a second `recv` and a `send` on the console; if it differs from main, add that measurement to the PR. - `EDESTADDRREQ` (39) for a `send` without an address after the reset (`ClosedPeerSendError`, `Udp.cpp:919-929`), and `ECONNRESET` on the first `send`. Same: unmeasured, and the debt entry says so. - `SIO_UDP_CONNRESET` off on the pair's UDP sockets (`Udp.cpp:1025-1030`). Separate fix and independent of the above; fine as its own small PR with a test that fails without it. Please rebase onto current `main` and keep only the parts the console measurements support (a stream `EPIPE` and the empty-queue `recv` are done, so drop the duplicates, including the test and the debt rewrite of those). The debt entry should then add only what remains.

Pull request closed

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!15785
No description provided.