Skip to content

fix(libkernel): answer a zero-length recvmsg on a stream socket as recv does - #24944

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Dr-Coconut:fix/libkernel-recvmsg-zero-length-stream
Nov 27, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Dr-Coconut:fix/libkernel-recvmsg-zero-length-stream

Conversation

@Dr-Coconut

@Dr-Coconut Dr-Coconut commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

What

recv and recvfrom answer a zero-length receive on a stream socket without the host: 0 when it is connected, ENOTCONN when it is not, as FreeBSD 11.4's soreceive_generic does when nothing is queued. recvmsg asked the host instead, after WaitForTransfer, so on an empty connected socket it failed with EAGAIN under MSG_DONTWAIT or on a non-blocking socket and blocked otherwise, and an unconnected socket did not get ENOTCONN; the libkernel debt file records this ("asks the host in the stream cases").

  • recvmsg takes the same ReceiveAnsweredHere answer before waiting when a stream socket's iovecs total zero bytes.
  • A Unix stream socket with a control buffer for passed descriptors still asks the host: with data queued, soreceive_generic goes on to deliver the queued control messages even for a zero-length request.

GuestTcp.cpp's ReceiveAnsweredWithoutTheHost gets the recvmsg cases beside the recv and recvfrom ones: ENOTCONN on an unconnected socket with and without MSG_DONTWAIT, 0 on an empty connected socket with and without it, and 0 with data queued, the data still there afterwards. The debt fragment rewrites the entry to cover recvmsg and records the control-buffer case and the pending socket error these answers do not return.

Tested

Commit 14bebd0 on main b1ef52c, Release, in a workflow on my fork: https://github.com/Dr-Coconut/AnyPS5/actions/runs/45036318072 (the run on the first version, 7db64ac on 26194f0, gave the same results: https://github.com/Dr-Coconut/AnyPS5/actions/runs/44901177525)

  • Windows (GitHub-hosted windows-latest, image windows-2025-vs2026, WinLibs GCC 15.2.0 15.2.0posix-14.0.0-ucrt-r7) and Linux (GitHub-hosted ubuntu-latest, image ubuntu-26.04, GCC 15.2.0), with the change: guest_tcp, guest_udp, guest_socket_pair and guest_kernel_socket_dontwait pass on both.
  • With Udp.cpp restored to main and the new cases kept, on both: guest_tcp aborts (Windows 0xc0000409, Linux Subprocess aborted); the other three pass.

Not run on macOS. No title tested. tools/check_conventions.py passes, and tools/debt_fold.py --check accepts the fragment. Rebased onto main after #23341 changed ReceiveShut to ReceiveEnded on the line before this change; the branch keeps ReceiveEnded. tools/pr_overlap.py --branch merged 26 open pull requests that share files with this branch and reported #18197, which adds the arrival-time setup between stamps and the condition changed here, so they overlap textually. It could not compare seven that conflict with main; compared by hand, #8605 changes GuestSockets::Stat, #11896 replaces the host call inside the block this skips, #22048 adds a check inside that block, and #12682, #12688, #12700 and #12965 rework blocking connects and waits on Windows without touching these lines.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

@MarcZX10 MarcZX10 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.

Reviewed — looks good

Nothing blocks this change; the review supports merging based on the inspected implementation and tests.

What I ran

Reviewed head 7db64acc9a13096674e23d64bc18c3e19fa49877. No commands or tests were run during this review; author-reported CI results are not treated as review runs.

Results

Check Result Note
Changed-line review passed All added lines inspected
Test execution not run No local test run
Author-reported CI not run Windows and Linux results in the description

Findings

Blocking

None.

Non-blocking

None.

Not verified

No tests were run; macOS and physical-console behavior were not independently verified. All three changed files listed in the manifest were read, and none were excluded. Targeted context in Udp.cpp and GuestTcp.cpp was also read.

To get this to a good state

Nothing is required.

@PrivacyOnion PrivacyOnion 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.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 7db64acc9a:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_tcp_tests built cleanly.
  • CTest (guest_tcp):
    1/1 Test #5962: guest_tcp ........................   Passed    0.61 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/Socket/src/Udp.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5962: guest_tcp ........................Subprocess aborted***Exception:   0.56 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified core/libs/prx/libkernel/Socket/src/Udp.cpp recvmsg_nid_postfix handles empty stream sockets (type == 1 && total == 0 && !passing) via ReceiveAnsweredHere, returning ENOTCONN (57) when unconnected and 0 when connected.
    • Verified that when control messages are passed (passing is true) on Unix stream sockets, emptyStream is false and control delivery proceeds via the host.
    • Verified that shut sockets (!shut check) bypass ReceiveAnsweredHere and proceed through existing shutdown handling returning 0.
    • Verified core/libs/tests/GuestTcp.cpp exercises recvmsg with zero-length iovec on unconnected and connected stream sockets with and without MSG_DONTWAIT, and after queueing data with MSG_PEEK.
    • Verified docs/dev/debt/changes/fix-libkernel-recvmsg-zero-length-stream.md technical debt fragment format, documentation of FreeBSD 11.4 soreceive_generic semantics, and strict AGENTS.md compliance.

Looks Good To Me

@github-actions github-actions Bot added libkernel docs needs rebase Conflicts with main, rebase needed labels Nov 21, 2026
…cv does

recv and recvfrom answer a zero-length receive on a stream socket
without the host: 0 when it is connected, ENOTCONN when it is not, as
FreeBSD 11.4's soreceive_generic does when nothing is queued. recvmsg
asked the host instead, after waiting for the socket to be readable, so
on an empty connected socket it failed with EAGAIN under MSG_DONTWAIT or
a non-blocking socket and blocked otherwise, and an unconnected socket
did not get ENOTCONN. recvmsg now takes the same answer before waiting,
except on a Unix stream socket with a control buffer, where FreeBSD
delivers queued control messages and the host still answers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Dr-Coconut
Dr-Coconut force-pushed the fix/libkernel-recvmsg-zero-length-stream branch from 7db64ac to 14bebd0 Compare November 22, 2026 15:16
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 22, 2026

@MarcZX10 MarcZX10 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.

Reviewed — looks good

The reviewed change at head 14bebd06fc136c93c5943daf514f11c9ab9c5b84 skips the host receive for zero-length stream recvmsg cases that can be answered locally, while preserving the host path when Unix control messages may be delivered; the inspected code and added cases support that behavior.

What I ran

No commands or tests were run. I read the prepared description, commits, manifest and diff, and inspected the changed code, its helper, test cases and debt fragment through repo_read.py.

Results

Check Result Note
Source review passed Three changed files read
Tests not run Author reported Windows and Linux runs

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not run tests, verify the author-reported workflow, or test macOS or console behavior. All three files marked reviewed in the manifest were read; no files were excluded.

To get this to a good state

Nothing is required.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

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 8605 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 11896 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12682 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12688 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12700 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12965 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 17113 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 18197 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 22398 core/libs/prx/libkernel/Socket/src/Udp.cpp

@oneandonlydean
oneandonlydean merged commit a043f58 into boykopovar:main Nov 27, 2026
15 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants