fix(libkernel): answer EPIPE and ECONNRESET after a socketpair peer closes #16081
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16081"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Opensource55 on 2026-11-05T15:51:47Z.
What
Closing one end of a guest
socketpairdid not reach the other end on Windows, because the Windows pair is a loopback TCP connection forSOCK_STREAMand two connected UDP sockets forSOCK_DGRAM, and the host has no counterpart of the console'sunp_drop. Two guest calls came back wrong: the first streamsendafter the peer closed was accepted by the loopback connection and succeeded, where the console returnsEPIPE(the sends after it already reportedEPIPE), and an empty datagramrecvafter the peer closed waited on a socket the host never signals, where the console fails it at once withECONNRESET.The pair ends already link to each other through a weak peer reference, the same link
getpeernameuses to answerENOTCONNonce the peer closed.sendnow consults that link before touching the loopback connection: a stream send on a pair end whose peer is gone fails withEPIPEinstead of accepting bytes into the send buffer.recvconsults it before waiting: a datagram receive on a pair end whose peer is gone answersECONNRESETonce 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 theAF_UNIXpair family and run on Windows only: FreeBSD 11.4's close path (uipc_detachtounp_disconnect) sets no error on the surviving end of a datagram pair, so the POSIX hosts keep their nativeAF_UNIXanswers and only the Windows loopback pair is brought to the console behaviour.QueuedBytesgained the Windows arm it needed (ioctlsocketFIONREAD), which the other callers keep; the receive guard usesselectwith 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 withECONNRESET(54), with a datagram queued before the close still delivered first; that measurement is on the issue. The issue'sENOTCONN(57) was a source reading, and the test now pins 54 instead.The
socketpairentry indocs/dev/debt/libkernel.mdno longer lists the two answers; the remaining gaps there (theENOBUFSdrop,SOCK_SEQPACKET, the address-family errors and the missingPOLLHUP) are untouched.Tested
Windows 10 22H2 (19045.6456), MinGW-W64 GCC 15.2.0 (ucrt, Release build), head
9859dada50on top ofmain82fcf46d25; no title was run.Three cases were added to
GuestSocketPair.cpp: closing one end of a stream pair makes the firstsendon the other end fail withEPIPE(32); on Windows, a datagram sent before one end closes is delivered, after whichrecvon the other end fails withECONNRESET(54); and on Windows a datagram sent with no payload before the close is delivered too,recvanswering 0, after which the nextrecvanswers 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
FIONREAD1,MSG_PEEK0, thenrecv0 - 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.Fail(57)in place ofFail(54):guest_socket_pairaborts on the datagram case (exit0xc0000409), so the assertion separates the two answers.Udp.cpphunks removed:guest_socket_pairruns 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.guest_socket_pairpasses in 0.03 s, andctest -R "socket|recvmsg"passes 40 of 40 in 7.6 s, including the pre-existing loop that expects a later streamsendto reportEPIPE, the datagram flows that queue before closing, and thepollexpectations after a peershutdown(SHUT_WR).Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-11-06T06:30:37Z.
Review: COMMENTED
Checked head
5260486634a0d28f2075708e6a058b4db329e005, basef53ddbc377bac92137eed439914b72c850f20649. Read the patch and issue #15661, checked Winsock/FreeBSD sources, and ran six native Linux socket controls.recv_nid_postfixtreatsQueuedBytes == 0as 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 nativerecvcan 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.4soreceive_dgramchecks 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.