fix(libkernel): clear fstat read bits on a socket after the peer closes #8605

Open
ebarr24 wants to merge 2 commits from upstream-pr/8605 into main
ebarr24 commented 2026-10-20 18:11:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ebarr24 on 2026-10-20T18:11:52Z.


What

fstat on a guest socket kept the read permission bits after the peer shut down its send side, closed or reset the connection, because only this process's own receive shutdown was consulted. FreeBSD 11.4's soo_stat reports no read bits once socantrcvmore is set and the receive buffer is empty. Stat now also asks the host for the peer's end of stream and clears the bits when nothing is queued, so they stay while data is still readable. https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_socket.c

Tested

Windows 11, MinGW-w64 GCC 15.2, Release build with tests. guest_socket_pair passes with this change; the new test fails on main.

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: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4940260182 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8605). Originally posted by [ebarr24](https://github.com/ebarr24) on 2026-10-20T18:11:52Z. --- ### What fstat on a guest socket kept the read permission bits after the peer shut down its send side, closed or reset the connection, because only this process's own receive shutdown was consulted. FreeBSD 11.4's soo_stat reports no read bits once socantrcvmore is set and the receive buffer is empty. Stat now also asks the host for the peer's end of stream and clears the bits when nothing is queued, so they stay while data is still readable. https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_socket.c ### Tested Windows 11, MinGW-w64 GCC 15.2, Release build with tests. `guest_socket_pair` passes with this change; the new test fails on `main`. ### 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: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
xXPBSXx commented 2026-10-20 22:27:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T22:27:11Z.


Independent Windows run of this PR (data only).

Environment: Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against main 339217ebc9 is clean. I built only guest_socket_pair_tests; ctest -N -R socket_pair lists exactly one test.

Results (3 runs each):

  • PR head: guest_socket_pair passes 3/3.
  • Baseline: I kept the PR's test and replaced only core/libs/prx/libkernel/Socket/src/Udp.cpp with its main version, then rebuilt. The test fails 3/3 (exit 0xc0000409, a failed Require). I restored the PR's Udp.cpp afterwards.

Not run: other platforms, the other socket tests, the full repository CTest, and any title or console run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6303170929 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8605#issuecomment-6303170929). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T22:27:11Z. --- Independent Windows run of this PR (data only). **Environment:** Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against `main` 339217ebc9 is clean. I built only `guest_socket_pair_tests`; `ctest -N -R socket_pair` lists exactly one test. **Results (3 runs each):** - PR head: `guest_socket_pair` passes 3/3. - Baseline: I kept the PR's test and replaced only `core/libs/prx/libkernel/Socket/src/Udp.cpp` with its `main` version, then rebuilt. The test fails 3/3 (exit 0xc0000409, a failed `Require`). I restored the PR's `Udp.cpp` afterwards. **Not run:** other platforms, the other socket tests, the full repository CTest, and any title or console run. AI-assisted (Claude Code).
xXPBSXx commented 2026-10-21 09:45:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T09:45:21Z.


Linux data point for 064990ba6e: WSL2 Ubuntu 24.04 (a virtualised kernel, not bare-metal Linux), GCC 13.3.0, Release, BUILD_TESTING=ON, PR head alone (not merged onto main; its merge-base is 73b6a4cc7).

  • With the PR: ctest -R '^(guest_socket_pair|guest_recvmsg_memory)$' passes 3 of 3 (both tests).
  • With core/libs/prx/libkernel/Socket/src/Udp.cpp restored to the merge-base (new test kept): guest_socket_pair fails (Subprocess aborted); guest_recvmsg_memory still passes.
    Not tested: macOS, Windows, a title run.
    AI-assisted (Claude Code).
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6312962854 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8605#issuecomment-6312962854). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T09:45:21Z. --- Linux data point for `064990ba6e`: WSL2 Ubuntu 24.04 (a virtualised kernel, not bare-metal Linux), GCC 13.3.0, Release, `BUILD_TESTING=ON`, PR head alone (not merged onto `main`; its merge-base is `73b6a4cc7`). - With the PR: `ctest -R '^(guest_socket_pair|guest_recvmsg_memory)$'` passes 3 of 3 (both tests). - With `core/libs/prx/libkernel/Socket/src/Udp.cpp` restored to the merge-base (new test kept): `guest_socket_pair` fails (`Subprocess aborted`); `guest_recvmsg_memory` still passes. Not tested: macOS, Windows, a title run. AI-assisted (Claude Code).
harisibrar1 commented 2026-10-24 13:39:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:39:07Z.

Review: COMMENTED


Checked against main 522e32e3b6 and FreeBSD 11.4 sys/kern/sys_socket.c (soo_stat), sys/kern/uipc_usrreq.c (unp_disconnect), sys/kern/uipc_socket.c (soisdisconnected):

  • This no longer merges: docs/dev/debt/libkernel.md conflicts with main, which has since rewritten the poll entry next to the fstat line and added a Windows "poll … peer closed" entry. The code files merge cleanly, and main's Stat (Udp.cpp:866-899) still has the old rule, so the fix itself is still needed.
  • Since this branch was cut, main added macOS socket support (7701a9b9da), and Ready() now has an __APPLE__ branch (Udp.cpp:930-936) that peeks with recv(MSG_PEEK) instead of using POLLRDHUP, which is a Linux extension. The new #else branch in Stat uses POLLRDHUP directly, so it probably needs the same split. I couldn't build on macOS to confirm, and CI doesn't build libkernel there.
  • In the debt text: on a Unix-domain stream, unp_disconnect calls soisdisconnected on both ends (uipc_usrreq.c:1548-1552), which also sets SBS_CANTSENDMORE. So after close(pair[0]), FreeBSD reports pair[1] as 0140000, while this reports 0140222. Your third test case only checks the read bits, so it passes either way. The entry could name a peer close as well as a reset for the write bits.
  • The read-bit rule itself matches soo_stat (sys_socket.c:302-304), and the SHUT_WR case in the new test matches uipc_shutdown/unp_shutdown.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638441623 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8605#pullrequestreview-5638441623). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T13:39:07Z. **Review:** COMMENTED --- Checked against main 522e32e3b6 and FreeBSD 11.4 sys/kern/sys_socket.c (soo_stat), sys/kern/uipc_usrreq.c (unp_disconnect), sys/kern/uipc_socket.c (soisdisconnected): - This no longer merges: `docs/dev/debt/libkernel.md` conflicts with main, which has since rewritten the poll entry next to the fstat line and added a Windows "poll … peer closed" entry. The code files merge cleanly, and main's `Stat` (Udp.cpp:866-899) still has the old rule, so the fix itself is still needed. - Since this branch was cut, main added macOS socket support (7701a9b9da), and `Ready()` now has an `__APPLE__` branch (Udp.cpp:930-936) that peeks with `recv(MSG_PEEK)` instead of using `POLLRDHUP`, which is a Linux extension. The new `#else` branch in `Stat` uses `POLLRDHUP` directly, so it probably needs the same split. I couldn't build on macOS to confirm, and CI doesn't build libkernel there. - In the debt text: on a Unix-domain stream, `unp_disconnect` calls `soisdisconnected` on both ends (uipc_usrreq.c:1548-1552), which also sets `SBS_CANTSENDMORE`. So after `close(pair[0])`, FreeBSD reports `pair[1]` as 0140000, while this reports 0140222. Your third test case only checks the read bits, so it passes either way. The entry could name a peer close as well as a reset for the write bits. - The read-bit rule itself matches `soo_stat` (sys_socket.c:302-304), and the SHUT_WR case in the new test matches `uipc_shutdown`/`unp_shutdown`. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
MarcZX10 commented 2026-11-17 10:34:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T10:34:04Z.

Review: COMMENTED


Reviewed — no blocking findings

Nothing blocks this; the points below are optional. The code matches the description on read, but I ran no checks and the Linux path has no test, so this is a comment, not an approval.

What I ran

  • Head: 633b9a5bae221cc859363135daf69e00e91c4c3b (base main)
  • Read the description, commits, manifest, diff and the three changed files from the head with repo_read.py:
python3 repo_read.py cat core/libs/prx/libkernel/Socket/src/Udp.cpp 1010 1070
python3 repo_read.py search POLLRDHUP core/libs/prx/libkernel
python3 repo_read.py search "bool Readable" core/libs/tests
  • No build and no test run in this review. The Windows test run in the description is the author's report, not mine.

Results

Check Result Note
Changed files read passed 3 of 3
Windows test run (author) not run author-reported only
Linux build and test not run none in this run
Required hosted checks not run status not read

Findings

Blocking

None.

Non-blocking

  • docs/dev/debt/libkernel.md:272 — "where FreeBSD's socantsendmore also follows a peer reset" is hard to parse. The code clears write bits only from sendShut (Udp.cpp:1057), so a plainer sentence would say: write bits clear only on this process's shutdown, also on a peer reset.

Sources

Not verified

  • Windows test run: author-reported (Windows 11, MinGW-w64 GCC 15.2). I did not run it.
  • Linux path (Udp.cpp:1046-1047, POLLRDHUP): the PR's tests were run on Windows only. I read that the Linux branch matches the existing POLLRDHUP use at Udp.cpp:1105, but I did not run it.
  • Idle-socket assertion (GuestSocketPair.cpp:249-250): relies on an unconnected socket not reporting a peer close on either platform. Checked by reading only.
  • Required hosted checks for this head: not read.
  • Files read: Udp.cpp, GuestSocketPair.cpp, docs/dev/debt/libkernel.md. Excluded by rule: none.

To get this to a good state

Nothing is required.

Optional:

  • Reword the libkernel.md sentence noted above.
  • Run guest_socket_pair on Linux so the POLLRDHUP branch is covered.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5936855415 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8605#pullrequestreview-5936855415). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-17T10:34:04Z. **Review:** COMMENTED --- # Reviewed — no blocking findings Nothing blocks this; the points below are optional. The code matches the description on read, but I ran no checks and the Linux path has no test, so this is a comment, not an approval. ## What I ran - Head: `633b9a5bae221cc859363135daf69e00e91c4c3b` (base `main`) - Read the description, commits, manifest, diff and the three changed files from the head with `repo_read.py`: ``` python3 repo_read.py cat core/libs/prx/libkernel/Socket/src/Udp.cpp 1010 1070 python3 repo_read.py search POLLRDHUP core/libs/prx/libkernel python3 repo_read.py search "bool Readable" core/libs/tests ``` - No build and no test run in this review. The Windows test run in the description is the author's report, not mine. ## Results | Check | Result | Note | |---|---|---| | Changed files read | passed | 3 of 3 | | Windows test run (author) | not run | author-reported only | | Linux build and test | not run | none in this run | | Required hosted checks | not run | status not read | ## Findings ### Blocking None. ### Non-blocking - `docs/dev/debt/libkernel.md:272` — "where FreeBSD's socantsendmore also follows a peer reset" is hard to parse. The code clears write bits only from `sendShut` (`Udp.cpp:1057`), so a plainer sentence would say: write bits clear only on this process's shutdown, also on a peer reset. ## Sources - [Udp.cpp, Stat](https://github.com/boykopovar/AnyPS5/blob/633b9a5bae221cc859363135daf69e00e91c4c3b/core/libs/prx/libkernel/Socket/src/Udp.cpp#L1040-L1057) - [GuestSocketPair.cpp, new assertions](https://github.com/boykopovar/AnyPS5/blob/633b9a5bae221cc859363135daf69e00e91c4c3b/core/libs/tests/GuestSocketPair.cpp#L249-L266) ## Not verified - Windows test run: author-reported (Windows 11, MinGW-w64 GCC 15.2). I did not run it. - Linux path (`Udp.cpp:1046-1047`, `POLLRDHUP`): the PR's tests were run on Windows only. I read that the Linux branch matches the existing `POLLRDHUP` use at `Udp.cpp:1105`, but I did not run it. - Idle-socket assertion (`GuestSocketPair.cpp:249-250`): relies on an unconnected socket not reporting a peer close on either platform. Checked by reading only. - Required hosted checks for this head: not read. - Files read: `Udp.cpp`, `GuestSocketPair.cpp`, `docs/dev/debt/libkernel.md`. Excluded by rule: none. ## To get this to a good state Nothing is required. Optional: - Reword the `libkernel.md` sentence noted above. - Run `guest_socket_pair` on Linux so the `POLLRDHUP` branch is covered.
ligmaSec commented 2026-12-03 18:11:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T18:11:08Z.

Review: COMMENTED


The new Stat branch references POLLRDHUP on every non-Windows host. Apple's poll.h does not define it, so this prevents libkernel from compiling on macOS. The existing read-event function in the same file already has a macOS branch that checks stream EOF after POLLIN. Can Stat use the same platform distinction? Apple's poll header

Source review at 633b9a5bae. A native macOS build remains unverified.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6175702912 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8605#pullrequestreview-6175702912). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T18:11:08Z. **Review:** COMMENTED --- The new `Stat` branch references `POLLRDHUP` on every non-Windows host. Apple's `poll.h` does not define it, so this prevents libkernel from compiling on macOS. The existing read-event function in the same file already has a macOS branch that checks stream EOF after `POLLIN`. Can `Stat` use the same platform distinction? [Apple's poll header](https://github.com/apple-oss-distributions/xnu/blob/main/bsd/sys/poll.h#L61-L88) Source review at `633b9a5bae`. A native macOS build remains unverified. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/8605:upstream-pr/8605'
git switch 'upstream-pr/8605'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/8605'
git switch 'upstream-pr/8605'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/8605'
git switch 'upstream-pr/8605'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/8605'
git switch 'main'
git merge --squash 'upstream-pr/8605'
git switch 'main'
git merge --ff-only 'upstream-pr/8605'
git switch 'main'
git merge 'upstream-pr/8605'
git push origin 'main'
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!8605
No description provided.