Skip to content

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

Merged
adriadam10 merged 3 commits into
boykopovar:mainfrom
xXPBSXx:fix/recv-after-shutdown-and-empty
Oct 21, 2026
Merged

adriadam10 merged 3 commits into
boykopovar:mainfrom
xXPBSXx:fix/recv-after-shutdown-and-empty

Conversation

@xXPBSXx

@xXPBSXx xXPBSXx commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

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 cabd80f + 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

AI-assisted (Claude Code).

🤖 Generated with Claude Code

@github-actions github-actions Bot added the fix error Closes an issue (Fixes #N) label Oct 20, 2026
@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

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

@fogydes fogydes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

xXPBSXx commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

On Linux (WSL2 Ubuntu 24.04, GCC 13.3, Release; not a bare-metal CI) guest_socket_pair aborts at head bd895d1 and passes 30 of 30 runs at the merge base cabd80f (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

xXPBSXx commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

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 bd895d1 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 adriadam10 self-assigned this Oct 21, 2026
xXPBSXx and others added 3 commits October 21, 2026 20:01
…out the host

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. Winsock fails the first with WSAESHUTDOWN, which
reached the guest as EPIPE, and blocks the second until data arrives. recv and
recvfrom now answer both themselves.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
On Linux the host keeps data that was buffered before shutdown(SHUT_RD) and
guest_socket_pair expects to read it, so the shortcut that answers 0 for any
receive after SHUT_RD broke that test there. Limit the shortcut to Windows,
where the host cannot answer it, and run the post-shutdown receives of
guest_tcp on Windows only.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
@adriadam10
adriadam10 force-pushed the fix/recv-after-shutdown-and-empty branch from 643c064 to 32f8205 Compare October 21, 2026 18:03
@adriadam10
adriadam10 merged commit eaff876 into boykopovar:main Oct 21, 2026
10 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs fix error Closes an issue (Fixes #N) libkernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

libkernel: recv after shutdown(SHUT_RD) fails with EPIPE and a zero-length recv on a stream socket blocks (Windows)

3 participants