fix(libkernel): answer EPIPE and ECONNRESET after a socketpair peer closes #16081

Merged
Opensource55 merged 1 commit from upstream-pr/16081 into main 2026-11-06 20:24:10 +00:00
Opensource55 commented 2026-11-05 15:51:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Opensource55 on 2026-11-05T15:51:47Z.


What

Closing one end of a guest socketpair did not reach the other end on Windows, because the Windows pair is a loopback TCP connection for SOCK_STREAM and two connected UDP sockets for SOCK_DGRAM, and the host has no counterpart of the console's unp_drop. Two guest calls came back wrong: the first stream send after the peer closed was accepted by the loopback connection and succeeded, where the console returns EPIPE (the sends after it already reported EPIPE), and an empty datagram recv after the peer closed waited on a socket the host never signals, where the console fails it at once with ECONNRESET.

The pair ends already link to each other through a weak peer reference, the same link getpeername uses to answer ENOTCONN once the peer closed. send now consults that link before touching the loopback connection: a stream send on a pair end whose peer is gone fails with EPIPE instead of accepting bytes into the send buffer. recv consults it before waiting: a datagram receive on a pair end whose peer is gone answers ECONNRESET once nothing is queued, checked by asking the socket whether a datagram is queued rather than how many bytes it holds, so datagrams sent before the close are still delivered first, as on the console, including one sent with no payload. Both checks are gated on the AF_UNIX pair family and run on Windows only: FreeBSD 11.4's close path (uipc_detach to unp_disconnect) sets no error on the surviving end of a datagram pair, so the POSIX hosts keep their native AF_UNIX answers and only the Windows loopback pair is brought to the console behaviour. QueuedBytes gained the Windows arm it needed (ioctlsocket FIONREAD), which the other callers keep; the receive guard uses select with a zero timeout instead, because a byte count answers for the payload and a datagram may carry none. Guest TCP and UDP sockets do not take either path.

The answers are the ones measured on hardware, not the ones the original report read out of the FreeBSD source. On 13.60 and 11.60 a payload probe finds the stream send failing with EPIPE (32) and the datagram receive after the peer closed failing with ECONNRESET (54), with a datagram queued before the close still delivered first; that measurement is on the issue. The issue's ENOTCONN (57) was a source reading, and the test now pins 54 instead.

The socketpair entry in docs/dev/debt/libkernel.md no longer lists the two answers; the remaining gaps there (the ENOBUFS drop, SOCK_SEQPACKET, the address-family errors and the missing POLLHUP) are untouched.

Tested

Windows 10 22H2 (19045.6456), MinGW-W64 GCC 15.2.0 (ucrt, Release build), head 9859dada50 on top of main 82fcf46d25; no title was run.

Three cases were added to GuestSocketPair.cpp: closing one end of a stream pair makes the first send on the other end fail with EPIPE (32); on Windows, a datagram sent before one end closes is delivered, after which recv on the other end fails with ECONNRESET (54); and on Windows a datagram sent with no payload before the close is delivered too, recv answering 0, after which the next recv answers 54.

The empty datagram is the case ahmadmuj raised in the review: a byte count cannot tell a queued empty datagram from an empty queue. On this host Winsock reports one for it anyway - a standalone probe sends 0 bytes between connected UDP sockets and reads FIONREAD 1, MSG_PEEK 0, then recv 0 - so the added case passes with the byte-count guard as well, and it does not distinguish the two implementations here. The guard is record-based now so that the answer does not rest on that count.

  • Red side, Fail(57) in place of Fail(54): guest_socket_pair aborts on the datagram case (exit 0xc0000409), so the assertion separates the two answers.
  • Red side, both Udp.cpp hunks removed: guest_socket_pair runs into its 20 s timeout on that case, because the host never signals the empty datagram socket. That is the gap the receive check closes.
  • The empty datagram case also passes with the byte-count guard in place, for the reason above; it does not separate the two.
  • Green side: guest_socket_pair passes in 0.03 s, and ctest -R "socket|recvmsg" passes 40 of 40 in 7.6 s, including the pre-existing loop that expects a later stream send to report EPIPE, the datagram flows that queue before closing, and the poll expectations after a peer shutdown(SHUT_WR).

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5174475155 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16081). Originally posted by [Opensource55](https://github.com/Opensource55) on 2026-11-05T15:51:47Z. --- ### What Closing one end of a guest `socketpair` did not reach the other end on Windows, because the Windows pair is a loopback TCP connection for `SOCK_STREAM` and two connected UDP sockets for `SOCK_DGRAM`, and the host has no counterpart of the console's `unp_drop`. Two guest calls came back wrong: the first stream `send` after the peer closed was accepted by the loopback connection and succeeded, where the console returns `EPIPE` (the sends after it already reported `EPIPE`), and an empty datagram `recv` after the peer closed waited on a socket the host never signals, where the console fails it at once with `ECONNRESET`. The pair ends already link to each other through a weak peer reference, the same link `getpeername` uses to answer `ENOTCONN` once the peer closed. `send` now consults that link before touching the loopback connection: a stream send on a pair end whose peer is gone fails with `EPIPE` instead of accepting bytes into the send buffer. `recv` consults it before waiting: a datagram receive on a pair end whose peer is gone answers `ECONNRESET` once nothing is queued, checked by asking the socket whether a datagram is queued rather than how many bytes it holds, so datagrams sent before the close are still delivered first, as on the console, including one sent with no payload. Both checks are gated on the `AF_UNIX` pair family and run on Windows only: FreeBSD 11.4's close path (`uipc_detach` to `unp_disconnect`) sets no error on the surviving end of a datagram pair, so the POSIX hosts keep their native `AF_UNIX` answers and only the Windows loopback pair is brought to the console behaviour. `QueuedBytes` gained the Windows arm it needed (`ioctlsocket` `FIONREAD`), which the other callers keep; the receive guard uses `select` with a zero timeout instead, because a byte count answers for the payload and a datagram may carry none. Guest TCP and UDP sockets do not take either path. The answers are the ones measured on hardware, not the ones the original report read out of the FreeBSD source. On 13.60 and 11.60 a payload probe finds the stream send failing with `EPIPE` (32) and the datagram receive after the peer closed failing with `ECONNRESET` (54), with a datagram queued before the close still delivered first; that measurement is [on the issue](https://github.com/boykopovar/AnyPS5/issues/15661#issuecomment-6663966028). The issue's `ENOTCONN` (57) was a source reading, and the test now pins 54 instead. The `socketpair` entry in `docs/dev/debt/libkernel.md` no longer lists the two answers; the remaining gaps there (the `ENOBUFS` drop, `SOCK_SEQPACKET`, the address-family errors and the missing `POLLHUP`) are untouched. ### Tested Windows 10 22H2 (19045.6456), MinGW-W64 GCC 15.2.0 (ucrt, Release build), head `9859dada50` on top of `main` `82fcf46d25`; no title was run. Three cases were added to `GuestSocketPair.cpp`: closing one end of a stream pair makes the first `send` on the other end fail with `EPIPE` (32); on Windows, a datagram sent before one end closes is delivered, after which `recv` on the other end fails with `ECONNRESET` (54); and on Windows a datagram sent with no payload before the close is delivered too, `recv` answering 0, after which the next `recv` answers 54. The empty datagram is the case ahmadmuj raised in [the review](https://github.com/boykopovar/AnyPS5/pull/16081#pullrequestreview-5796590475): a byte count cannot tell a queued empty datagram from an empty queue. On this host Winsock reports one for it anyway - a standalone probe sends 0 bytes between connected UDP sockets and reads `FIONREAD` 1, `MSG_PEEK` 0, then `recv` 0 - so the added case passes with the byte-count guard as well, and it does not distinguish the two implementations here. The guard is record-based now so that the answer does not rest on that count. - Red side, `Fail(57)` in place of `Fail(54)`: `guest_socket_pair` aborts on the datagram case (exit `0xc0000409`), so the assertion separates the two answers. - Red side, both `Udp.cpp` hunks removed: `guest_socket_pair` runs into its 20 s timeout on that case, because the host never signals the empty datagram socket. That is the gap the receive check closes. - The empty datagram case also passes with the byte-count guard in place, for the reason above; it does not separate the two. - Green side: `guest_socket_pair` passes in 0.03 s, and `ctest -R "socket|recvmsg"` passes 40 of 40 in 7.6 s, including the pre-existing loop that expects a later stream `send` to report `EPIPE`, the datagram flows that queue before closing, and the `poll` expectations after a peer `shutdown(SHUT_WR)`. ### 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
ahmadmuj commented 2026-11-06 06:30:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-11-06T06:30:37Z.

Review: COMMENTED


Checked head 5260486634a0d28f2075708e6a058b4db329e005, base f53ddbc377bac92137eed439914b72c850f20649. Read the patch and issue #15661, checked Winsock/FreeBSD sources, and ran six native Linux socket controls.

recv_nid_postfix treats QueuedBytes == 0 as an empty queue, but this also describes a queued zero-byte datagram. Winsock send sends an empty datagram for length zero; FIONREAD counts bytes, not records. After the peer closes, the new guard therefore returns 54 before native recv can consume that record.

Could we distinguish record availability from byte count and cover an empty datagram in the Windows test? For example: send "" with length 0, confirm the receiver is readable, close the sender, then receive with an 8-byte buffer. The current test only sends "q".

Both Linux AF_UNIX and connected-UDP controls receive the empty datagram despite FIONREAD == 0; never-sent and one-byte controls also pass. FreeBSD 11.4 soreceive_dgram checks queued records before pending errors. These checks support the queue distinction; they do not execute the Windows branch or establish console behavior for empty datagrams. The issue's hardware probe sends one byte. I did not run a project build.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5796590475 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16081#pullrequestreview-5796590475). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-11-06T06:30:37Z. **Review:** COMMENTED --- Checked head `5260486634a0d28f2075708e6a058b4db329e005`, base `f53ddbc377bac92137eed439914b72c850f20649`. Read the patch and issue #15661, checked Winsock/FreeBSD sources, and ran six native Linux socket controls. `recv_nid_postfix` treats `QueuedBytes == 0` as an empty queue, but this also describes a queued zero-byte datagram. Winsock [send](https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-send#remarks) sends an empty datagram for length zero; [FIONREAD](https://learn.microsoft.com/en-us/windows/win32/winsock/winsock-ioctls#fionread) counts bytes, not records. After the peer closes, the new guard therefore returns 54 before native `recv` can consume that record. Could we distinguish record availability from byte count and cover an empty datagram in the Windows test? For example: send `""` with length 0, confirm the receiver is readable, close the sender, then receive with an 8-byte buffer. The current test only sends `"q"`. Both Linux AF_UNIX and connected-UDP controls receive the empty datagram despite `FIONREAD == 0`; never-sent and one-byte controls also pass. [FreeBSD 11.4 `soreceive_dgram`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L2189-L2330) checks queued records before pending errors. These checks support the queue distinction; they do not execute the Windows branch or establish console behavior for empty datagrams. The issue's hardware probe sends one byte. I did not run a project build.
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!16081
No description provided.