fix(libkernel): treat a socket direction that is already shut as shut on macOS hosts #9625

Merged
rooklz merged 1 commit from upstream-pr/9625 into main 2026-10-25 21:23:53 +00:00
rooklz commented 2026-10-22 04:53:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T04:53:15Z.


What

guest_socket_pair fails on macOS hosts at GuestSocketPair.cpp:238: after the peer's shutdown(SHUT_WR), shutdown(pair[1], SHUT_RD) returns -1 with ENOTCONN. A small host program shows the same thing: on a connected AF_UNIX stream pair, shutdown(b, SHUT_RD) succeeds on a fresh pair, fails with ENOTCONN (57) once the peer has shut its sending side, and the 5 queued bytes are still readable afterwards.

The two kernels differ here:

  • XNU's soshutdownlock_final (bsd/kern/uipc_socket.c) returns ENOTCONN when the direction asked for is already shut (/* read already shut down */, /* write already shut down */), and for SHUT_RDWR it jumps to done before the write side when the read side was already shut.
  • FreeBSD 11.4's soshutdown (sys/kern/uipc_socket.c) returns ENOTCONN only when the socket isn't connected; for a connected socket it shuts what was asked and returns 0, whatever was shut before.

On macOS, shutdown now treats ENOTCONN from a socket that still has a peer (getpeername succeeds) as a direction that was already shut: the call succeeds and the guest's shut state is recorded as before. For SHUT_RDWR the write side is then shut explicitly, since XNU skipped it. A socket without a peer still fails with ENOTCONN, as on FreeBSD. Linux and Windows behave as before.

@iamthehimansh's #8658 and @marion909's #7201 make the same change as part of their macOS ports (#8658 for SHUT_RD only). This is that change on its own with the test it fixes, as #8920 did for the fflush guard. Whichever lands first, the other drops these lines.

Tested

  • macOS 26.2, Apple M5, Apple clang 21, x86-64 build under Rosetta 2. The libraries don't link on macOS from main alone, so this ran on @mugurc's ci/macos-series-2 at 53db0badb with #7524, the code of #7937 and the libSceSsl change of #7332. guest_socket_pair without this change aborts at GuestSocketPair.cpp:238 (found with lldb); with it, it passes.
  • Linux x86-64 container (Ubuntu 24.04, GCC 13.3, Release), this branch: guest_socket_pair passes.
  • Apple M1 Pro, macOS 27.0.1, Apple clang 21, x86-64 Release under Rosetta 2, by @maximilianfv on ci/macos-series-2 at 27e968974 with this change and six other macOS PRs cherry-picked: guest_socket_pair aborts at GuestSocketPair.cpp:238 without it and passes 2 of 2 with it; no test that passed before fails with the batch applied.
  • check_conventions.py passes. pr_overlap.py reports the same lines in #8658, as described above.

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:4962302002 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9625). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-22T04:53:15Z. --- ### What `guest_socket_pair` fails on macOS hosts at `GuestSocketPair.cpp:238`: after the peer's `shutdown(SHUT_WR)`, `shutdown(pair[1], SHUT_RD)` returns -1 with `ENOTCONN`. A small host program shows the same thing: on a connected `AF_UNIX` stream pair, `shutdown(b, SHUT_RD)` succeeds on a fresh pair, fails with `ENOTCONN` (57) once the peer has shut its sending side, and the 5 queued bytes are still readable afterwards. The two kernels differ here: - XNU's `soshutdownlock_final` (`bsd/kern/uipc_socket.c`) returns `ENOTCONN` when the direction asked for is already shut (`/* read already shut down */`, `/* write already shut down */`), and for `SHUT_RDWR` it jumps to `done` before the write side when the read side was already shut. - FreeBSD 11.4's `soshutdown` (`sys/kern/uipc_socket.c`) returns `ENOTCONN` only when the socket isn't connected; for a connected socket it shuts what was asked and returns 0, whatever was shut before. On macOS, `shutdown` now treats `ENOTCONN` from a socket that still has a peer (`getpeername` succeeds) as a direction that was already shut: the call succeeds and the guest's shut state is recorded as before. For `SHUT_RDWR` the write side is then shut explicitly, since XNU skipped it. A socket without a peer still fails with `ENOTCONN`, as on FreeBSD. Linux and Windows behave as before. @iamthehimansh's #8658 and @marion909's #7201 make the same change as part of their macOS ports (#8658 for `SHUT_RD` only). This is that change on its own with the test it fixes, as #8920 did for the `fflush` guard. Whichever lands first, the other drops these lines. ### Tested - macOS 26.2, Apple M5, Apple clang 21, x86-64 build under Rosetta 2. The libraries don't link on macOS from `main` alone, so this ran on @mugurc's `ci/macos-series-2` at `53db0badb` with #7524, the code of #7937 and the libSceSsl change of #7332. `guest_socket_pair` without this change aborts at `GuestSocketPair.cpp:238` (found with lldb); with it, it passes. - Linux x86-64 container (Ubuntu 24.04, GCC 13.3, Release), this branch: `guest_socket_pair` passes. - Apple M1 Pro, macOS 27.0.1, Apple clang 21, x86-64 Release under Rosetta 2, by @maximilianfv on `ci/macos-series-2` at `27e968974` with this change and six other macOS PRs cherry-picked: `guest_socket_pair` aborts at `GuestSocketPair.cpp:238` without it and passes 2 of 2 with it; no test that passed before fails with the batch applied. - `check_conventions.py` passes. `pr_overlap.py` reports the same lines in #8658, as described above. <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> ### 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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
MarcZX10 commented 2026-10-22 09:23:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T09:23:51Z.

Review: DISMISSED


The macOS behavior is narrowly scoped: an ENOTCONN result is treated as an already-shut direction only when getpeername confirms the socket still has a peer; SHUT_RDWR explicitly attempts the write shutdown after XNU’s read-side early exit. The reported macOS control (same integration tree without this change fails, with it passes) and Linux test pass are useful, but the hosted product jobs are skipped and I did not rerun runtime tests here. Exact-head conventions and whitespace pass. Please coordinate the shared Udp.cpp hunk with #8658 and #7201 before merge; the description says either port PR must drop these lines if this one lands first, so record the agreed order/dependency in the checklist or thread.

I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5612004244 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9625#pullrequestreview-5612004244). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T09:23:51Z. **Review:** DISMISSED --- The macOS behavior is narrowly scoped: an `ENOTCONN` result is treated as an already-shut direction only when `getpeername` confirms the socket still has a peer; `SHUT_RDWR` explicitly attempts the write shutdown after XNU’s read-side early exit. The reported macOS control (same integration tree without this change fails, with it passes) and Linux test pass are useful, but the hosted product jobs are skipped and I did not rerun runtime tests here. Exact-head conventions and whitespace pass. Please coordinate the shared `Udp.cpp` hunk with #8658 and #7201 before merge; the description says either port PR must drop these lines if this one lands first, so record the agreed order/dependency in the checklist or thread. I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.
rooklz commented 2026-10-22 11:31:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T11:31:07Z.


On the #5239 direction: this came from running the suite on the macOS port tree discussed on #1, where the shutdown tests fail without it, not from a title. I am fine holding it until the queue drops or a maintainer says otherwise, or closing it in favour of #8658 if that is preferred.

Merge order, recorded here: if #8658 or #7201 lands first, I rebase this and drop whatever their Udp.cpp hunk already covers. If this lands first, their hunk at those lines conflicts and the resolution is to keep this version of shutdown_nid_postfix.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6339677615 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9625#issuecomment-6339677615). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-22T11:31:07Z. --- On the #5239 direction: this came from running the suite on the macOS port tree discussed on #1, where the shutdown tests fail without it, not from a title. I am fine holding it until the queue drops or a maintainer says otherwise, or closing it in favour of #8658 if that is preferred. Merge order, recorded here: if #8658 or #7201 lands first, I rebase this and drop whatever their `Udp.cpp` hunk already covers. If this lands first, their hunk at those lines conflicts and the resolution is to keep this version of `shutdown_nid_postfix`.
rooklz commented 2026-10-22 13:31:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T13:31:14Z.


Rebased onto current main (8b3e39832a): head 7c213787ac, same diff. check_conventions.py passes on it.

Refreshed results on this head: guest_socket_pair passes 3 of 3 on the macOS integration tree (the same tree as in the description, carrying exactly this Udp.cpp), and a hosted Linux run of the socket tests is at https://github.com/rooklz/AnyPS5/actions/runs/39787396014 (in progress; I'll note the result).

Landing order with the two port PRs is now written on both of them, #8658 and #7201: whichever lands first, the other drops its copy of the shutdown_nid_postfix hunk, and if a port lands first I rebase this onto it. I've asked both authors to say if they'd rather fold it in, in which case I close this.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6342009893 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9625#issuecomment-6342009893). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-22T13:31:14Z. --- Rebased onto current `main` (`8b3e39832a`): head `7c213787ac`, same diff. `check_conventions.py` passes on it. Refreshed results on this head: `guest_socket_pair` passes 3 of 3 on the macOS integration tree (the same tree as in the description, carrying exactly this `Udp.cpp`), and a hosted Linux run of the socket tests is at https://github.com/rooklz/AnyPS5/actions/runs/39787396014 (in progress; I'll note the result). Landing order with the two port PRs is now written on both of them, #8658 and #7201: whichever lands first, the other drops its copy of the `shutdown_nid_postfix` hunk, and if a port lands first I rebase this onto it. I've asked both authors to say if they'd rather fold it in, in which case I close this.
rooklz commented 2026-10-22 13:59:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T13:59:40Z.


The hosted Linux run on head 7c213787ac finished: the socket tests, guest_socket_pair among them, pass (https://github.com/rooklz/AnyPS5/actions/runs/39787396014, Ubuntu 24.04, GCC, Release).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6342613892 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9625#issuecomment-6342613892). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-22T13:59:40Z. --- The hosted Linux run on head `7c213787ac` finished: the socket tests, `guest_socket_pair` among them, pass (https://github.com/rooklz/AnyPS5/actions/runs/39787396014, Ubuntu 24.04, GCC, Release).
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!9625
No description provided.