fix(libkernel): return ENOTCONN from getpeername once a socketpair peer closed #8344

Merged
Retr0G28 merged 3 commits from upstream-pr/8344 into main 2026-10-25 00:28:31 +00:00
Retr0G28 commented 2026-10-20 10:44:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Retr0G28 on 2026-10-20T10:44:02Z.


What

getpeername on a socketpair end kept returning the unnamed AF_UNIX address after the other end was closed. On FreeBSD 11, closing an end runs uipc_detach, whose unp_disconnect (datagram) and unp_drop (stream) clear SS_ISCONNECTED on the remaining end (sys/kern/uipc_usrreq.c), and kern_getpeername then fails with ENOTCONN (sys/kern/uipc_syscalls.c).

Each pair end now holds a std::weak_ptr to the other end's socket. The reference expires when the peer's last descriptor is closed, dup'd copies included, and getpeername then fails with ENOTCONN (57). shutdown leaves the ends connected, as soshutdown does. The getpeername clause is removed from the socketpair entry in docs/dev/debt/libkernel.md, and "like on every guest socket" becomes "as on every guest socket" to keep the entry under the 1000-character limit.

guest_socket_pair now checks:

  • a stream end after its peer's shutdown(SHUT_WR) still reports the unnamed peer;
  • after the peer's close, getpeername fails with ENOTCONN and leaves the address and length untouched, while getsockname still succeeds;
  • on a datagram pair, closing one end while a dup'd copy stays open keeps the pair connected, and closing the copy disconnects it.

Tested

Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, -DBUILD_TESTING=ON, rebased on main 9d6adeb550.

  • guest_socket_pair with main's Udp.cpp fails (exit 0xc0000409 from the failed Require); with this PR it passes.
  • ctest -R "socket|dup|descriptor|fcntl|pipe|poll|select|recvmsg" -E "^agc_": 41 of 41 passed after the rebase onto 47fa6eecf; guest_socket_pair passes again after the rebases onto 11f976b27c and 9d6adeb550, whose only conflict each time was the socketpair debt entry.
  • After merging main 6cd36e6517 (head 8f66466e75, clean merge): ctest -R "socket|recvmsg|net_dontwait|^guest_poll$", 40 of 40 pass when run serially, including guest_socket_pair and guest_recvmsg_memory. With main's Udp.cpp on that tree, guest_socket_pair fails again (exit 0xc0000409) and guest_recvmsg_memory still passes.
  • python tools/check_conventions.py --base origin/main reports no errors. Its one warning is the length of the socketpair debt entry, 1382 characters on main, which this PR cuts to 1278.
  • Linux: @xXPBSXx ran the pre-rebase head c4450bc82 on WSL2 Ubuntu 24.04 (GCC 13.3.0): guest_socket_pair and guest_recvmsg_memory pass, and guest_socket_pair fails with main's Udp.cpp (comment below). Not run on bare-metal Linux.
  • No title or homebrew run.

Overlap:

  • #7502 changes the null-length check on the line next to the new one in getpeername, so whichever merges second needs a one-line rebase.
  • #10621 (mine) adds the inode member to Socket next to this PR's member. This PR goes first, and #10621 declares Depends on: #8344.
  • #7850 (macOS sockets) merged first and rewrote the same socketpair debt entry; this PR is rebased onto it and cuts only the getpeername sentence.
  • tools/pr_overlap.py could not compare some PRs. I checked those diffs by hand: none changes getpeername or the socketpair debt entry.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4934592450 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8344). Originally posted by [Retr0G28](https://github.com/Retr0G28) on 2026-10-20T10:44:02Z. --- ### What `getpeername` on a `socketpair` end kept returning the unnamed `AF_UNIX` address after the other end was closed. On FreeBSD 11, closing an end runs `uipc_detach`, whose `unp_disconnect` (datagram) and `unp_drop` (stream) clear `SS_ISCONNECTED` on the remaining end (`sys/kern/uipc_usrreq.c`), and `kern_getpeername` then fails with `ENOTCONN` (`sys/kern/uipc_syscalls.c`). Each pair end now holds a `std::weak_ptr` to the other end's socket. The reference expires when the peer's last descriptor is closed, `dup`'d copies included, and `getpeername` then fails with `ENOTCONN` (57). `shutdown` leaves the ends connected, as `soshutdown` does. The getpeername clause is removed from the socketpair entry in `docs/dev/debt/libkernel.md`, and "like on every guest socket" becomes "as on every guest socket" to keep the entry under the 1000-character limit. `guest_socket_pair` now checks: - a stream end after its peer's `shutdown(SHUT_WR)` still reports the unnamed peer; - after the peer's `close`, `getpeername` fails with `ENOTCONN` and leaves the address and length untouched, while `getsockname` still succeeds; - on a datagram pair, closing one end while a `dup`'d copy stays open keeps the pair connected, and closing the copy disconnects it. ### Tested Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, `-DBUILD_TESTING=ON`, rebased on `main` 9d6adeb550. - `guest_socket_pair` with `main`'s `Udp.cpp` fails (exit 0xc0000409 from the failed `Require`); with this PR it passes. - ctest `-R "socket|dup|descriptor|fcntl|pipe|poll|select|recvmsg" -E "^agc_"`: 41 of 41 passed after the rebase onto 47fa6eecf; `guest_socket_pair` passes again after the rebases onto 11f976b27c and 9d6adeb550, whose only conflict each time was the socketpair debt entry. - After merging `main` 6cd36e6517 (head 8f66466e75, clean merge): ctest `-R "socket|recvmsg|net_dontwait|^guest_poll$"`, 40 of 40 pass when run serially, including `guest_socket_pair` and `guest_recvmsg_memory`. With `main`'s `Udp.cpp` on that tree, `guest_socket_pair` fails again (exit 0xc0000409) and `guest_recvmsg_memory` still passes. - `python tools/check_conventions.py --base origin/main` reports no errors. Its one warning is the length of the socketpair debt entry, 1382 characters on `main`, which this PR cuts to 1278. - Linux: @xXPBSXx ran the pre-rebase head c4450bc82 on WSL2 Ubuntu 24.04 (GCC 13.3.0): `guest_socket_pair` and `guest_recvmsg_memory` pass, and `guest_socket_pair` fails with `main`'s `Udp.cpp` (comment below). Not run on bare-metal Linux. - No title or homebrew run. Overlap: - #7502 changes the null-length check on the line next to the new one in `getpeername`, so whichever merges second needs a one-line rebase. - #10621 (mine) adds the `inode` member to `Socket` next to this PR's member. This PR goes first, and #10621 declares `Depends on: #8344`. - #7850 (macOS sockets) merged first and rewrote the same socketpair debt entry; this PR is rebased onto it and cuts only the `getpeername` sentence. - `tools/pr_overlap.py` could not compare some PRs. I checked those diffs by hand: none changes `getpeername` or the socketpair debt entry. ### 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
xXPBSXx commented 2026-10-21 08:45:39 +00:00 (Migrated from github.com)

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


Linux data point for c4450bc82b: WSL2 Ubuntu 24.04 (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 6bd02dd91).

  • 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:6311875450 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8344#issuecomment-6311875450). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T08:45:39Z. --- Linux data point for `c4450bc82b`: WSL2 Ubuntu 24.04 (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 `6bd02dd91`). - 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).
MarcZX10 commented 2026-10-23 05:25:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T05:25:36Z.

Review: DISMISSED


I reviewed the peer-lifetime change and its focused tests. Holding only a weak_ptr to the peer lets dup share the same socket object while ensuring the peer expires after the last descriptor owner closes; shutdown leaves the object alive, matching the behavior described. The tests cover stream shutdown/close, untouched getpeername outputs, and a datagram peer retained by a duplicated descriptor. The PR reports a Windows control that fails on main and passes with the fix, plus WSL2 results; I have not rerun the native tests locally.

One integration item remains: against current upstream main 16319642a4c84e861188b75583ce853dfbe0a101, tools/check_main_sync.py --base upstream/main --head upstream/pr-8344-current reports 1 commit ahead / 10 behind (merge base 9d6adeb55), with a clean merge. Please merge current main and rerun guest_socket_pair plus the focused socket tests. Preserve the adjacent null-length ordering change from open #7502 in Udp.cpp; that PR changes neighboring lines but has a separate behavior. This request is for branch freshness and integration, not PR volume.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5624269148 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8344#pullrequestreview-5624269148). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T05:25:36Z. **Review:** DISMISSED --- I reviewed the peer-lifetime change and its focused tests. Holding only a `weak_ptr` to the peer lets `dup` share the same socket object while ensuring the peer expires after the last descriptor owner closes; `shutdown` leaves the object alive, matching the behavior described. The tests cover stream shutdown/close, untouched `getpeername` outputs, and a datagram peer retained by a duplicated descriptor. The PR reports a Windows control that fails on main and passes with the fix, plus WSL2 results; I have not rerun the native tests locally. One integration item remains: against current upstream `main` `16319642a4c84e861188b75583ce853dfbe0a101`, `tools/check_main_sync.py --base upstream/main --head upstream/pr-8344-current` reports 1 commit ahead / 10 behind (merge base `9d6adeb55`), with a clean merge. Please merge current main and rerun `guest_socket_pair` plus the focused socket tests. Preserve the adjacent null-length ordering change from open #7502 in `Udp.cpp`; that PR changes neighboring lines but has a separate behavior. This request is for branch freshness and integration, not PR volume.
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!8344
No description provided.