fix(libkernel): drop the data a socket held before shutdown(SHUT_RD) in every receive path #10767
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10767"
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 Ragbie on 2026-10-24T23:33:00Z.
What
After
shutdown(SHUT_RD)a guest socket still hands out data it held before the call:recv,recvfrom,readandreadvreturn the buffered bytes: the check that answers 0 for a shut receive side was Windows-only.recvmsgnever made that check. On Windows it fails withEPIPE(Winsock'sWSAESHUTDOWN); on Linux it returns the buffered bytes.fstat(st_size, read bits),FIONREADand theEVFILT_READevent'sdatastill count them.EVFILT_READtakesEV_EOFfrom the host's poll flags only. On Windows, with nothing queued,WSAPollreports nothing afterSD_RECEIVE, so the filter never fires, and akeventorsceKernelWaitEqueuealready waiting on the socket sleeps until its timeout.shutdownfails withENOTCONN(Linux) or succeeds (Windows), and the shut side was not recorded either way, so queued datagrams stayed readable.FreeBSD 11.4
soshutdowncallssorflushforSHUT_RD. That marks the receive sideSBS_CANTRCVMORE, empties the buffer and wakes the socket's readers (socantrcvmore→sorwakeup), sosoreceivereturns 0,soo_statreports no bytes and no read bits, andfilt_soreadreportsdata0 withEV_EOF(a listening socket usesfilt_solisten, which has no EOF). On a datagram socket that is not connected it still flushes, then returnsENOTCONN.The change, in
core/libs/prx/libkernel/Socket/src/Udp.cppandcore/libs/prx/libkernel/Equeue/:ReceiveShutreads the recorded state.recv,recvfrom(throughReceiveAnsweredHere) andrecvmsgreturn 0 on every host once the receive side is shut;recvmsgskips the host receive and reportsmsg_namelen0.GuestSockets::Statand theFIONREADioctl report no queued bytes afterSHUT_RD.GuestSockets::Readyanswers a read filter on a non-listening socket shut for reading without asking the host:data0 andEV_EOF.shutdownchecksgetpeernameon a datagram socket; when it has no peer, it records the shut sides whatever the host returned and fails withENOTCONN. When the receive side is shut, it wakes the equeues and kqueues that hold anEVFILT_READevent for any descriptor of the socket, including ones fromdup,dup2orF_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.SHUT_RDwith unread or later data resets a TCP connection, so the peer's next send fails withEPIPE; and FreeBSD keeps appending a Unix stream peer's later data (uipc_senddoes not check the receiver'sSBS_CANTRCVMORE), which receives here no longer return. The socket error entry no longer lists theEPIPEreceive.Tested
Windows 11, WinLibs GCC 15.2.0, and Linux (Ubuntu 24.04 container, GCC 13.3.0), rebased onto
main6cc5ec671(after #9625;shutdownkeeps itsAlreadyShutcheck beside the unconnected-datagram one):guest_tcp: the Windows-onlySHUT_RDblock now runs on every host, with a byte queued before the shutdown,FIONREADandrecvmsg.guest_socket_pair: it asserted Linux's behaviour of reading buffered data afterSHUT_RD. It now expects FreeBSD's, and checksmsg_namelenfor a Unix datagram socket.guest_udp:SHUT_RDon an unconnected socket with a queued datagram.guest_kqueue: level-triggeredEVFILT_READafterSHUT_RDreportsdata0 withEV_EOF, with and without bytes queued before it, and akeventalready 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 adupof it.main'sUdp.cpp, the first four fail on Linux, andguest_tcp,guest_udpandguest_socket_pairfail on Windows. With the change they pass with--repeat until-fail:5.EV_EOFand 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 reportedEV_EOF, becauseSD_RECEIVEwith unread data resets the loopback connection. Linux passes them before and after, since the host reportsPOLLRDHUPand wakes the waiter. Thedupwaiter also failed on Windows until the wake covered every descriptor of the socket. With the changeguest_kqueuepasses with--repeat until-fail:5, and the 78 tests matchingsocket|tcp|udp|kqueue|equeue|guest_net|select|pollwith--repeat until-fail:3.ctestonmain8a29e0883:99% tests passed, 5 tests failed out of 2378.agc_write_tracking_metadata_commit,agc_host_import_cut_lifetime,agc_driver_depth_plane_partial_storeandagc_driver_image_plane_address_on_linefail onmainon this machine too, and so doesguest_wide_formatting(wcsftime length matches strftime).check_skips.py: all skips expected.ctest:99% tests passed, 24 tests failed out of 2285; the same 24 fail onmainin this container (22agc_*on its lavapipe and kernel withoutUFFD_FEATURE_WP_ASYNC,pr_overlap_encodingandguest_system_load_exec_standard_error).#7090 rewrites the body of
recvmsgand #8605 changes the read-bit line inGuestSockets::Stat; this PR leaves that line as it is, and either order rebases with a small conflict inrecvmsg. #7090 and #11006 also add entries next to this PR's indocs/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
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 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.
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: