fix(libkernel): recognize listening sockets on macOS hosts #7559

Merged
mugurc merged 1 commit from upstream-pr/7559 into main 2026-10-20 10:56:13 +00:00
mugurc commented 2026-10-18 14:00:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mugurc on 2026-10-18T14:00:00Z.


What

On macOS, getsockopt with SO_ACCEPTCONN fails with ENOPROTOOPT. libkernel asks it whether a socket is listening in three places, so on macOS on main:

  • the guest's own getsockopt of SO_ACCEPTCONN fails (guest_socket_options: getsockopt of an integer option failed (errno 42));
  • connect, which since #6950 refuses a listening stream socket with EOPNOTSUPP, fails with EAFNOSUPPORT for an address of another family (guest_socket_connect_listening);
  • the read event of a listening socket, which since #7140 reports the connections waiting to be accepted, takes FIONREAD instead and reports 0 where guest_socket_events expects 1;
  • the read event of a listening socket waits for its low watermark, which #7092 lets it ignore. No test covers this one on macOS.

On macOS the listening state is now read from the SO_ACCEPTCONN flag of the socket options that proc_pidfdinfo reports (PROC_PIDFDSOCKETINFO). The three places go through one NativeListening function; other hosts keep the getsockopt call.

#7332, which brings the open macOS pull requests together, reads the flag the same way among its other fixes; this pull request is that change on its own.

Tested

  • macOS 26.5.1, Apple M5 Max, Apple clang 21, x86-64 build under Rosetta 2, main 385ed71f2 with the macOS series and the open macOS fixes #6928, #6667, #6668, #6969, #6853, #6836, #6620 and #7524 (the libraries do not link on macOS without the series): guest_socket_options, guest_socket_connect_listening and guest_socket_events fail without this change and pass with it; guest_tcp and guest_udp pass.
  • A standalone program on the same Mac read the flag for a stream socket before and after bind, after listen with a backlog of 0, and for a UDP socket: it is set only after listen.
  • Linux and Windows: not run. There NativeListening makes the same getsockopt call as before.

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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4909696945 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7559). Originally posted by [mugurc](https://github.com/mugurc) on 2026-10-18T14:00:00Z. --- ### What On macOS, `getsockopt` with `SO_ACCEPTCONN` fails with `ENOPROTOOPT`. libkernel asks it whether a socket is listening in three places, so on macOS on `main`: - the guest's own `getsockopt` of `SO_ACCEPTCONN` fails (`guest_socket_options`: `getsockopt of an integer option failed (errno 42)`); - `connect`, which since #6950 refuses a listening stream socket with `EOPNOTSUPP`, fails with `EAFNOSUPPORT` for an address of another family (`guest_socket_connect_listening`); - the read event of a listening socket, which since #7140 reports the connections waiting to be accepted, takes `FIONREAD` instead and reports 0 where `guest_socket_events` expects 1; - the read event of a listening socket waits for its low watermark, which #7092 lets it ignore. No test covers this one on macOS. On macOS the listening state is now read from the `SO_ACCEPTCONN` flag of the socket options that `proc_pidfdinfo` reports (`PROC_PIDFDSOCKETINFO`). The three places go through one `NativeListening` function; other hosts keep the `getsockopt` call. #7332, which brings the open macOS pull requests together, reads the flag the same way among its other fixes; this pull request is that change on its own. ### Tested - macOS 26.5.1, Apple M5 Max, Apple clang 21, x86-64 build under Rosetta 2, `main` 385ed71f2 with the macOS series and the open macOS fixes #6928, #6667, #6668, #6969, #6853, #6836, #6620 and #7524 (the libraries do not link on macOS without the series): `guest_socket_options`, `guest_socket_connect_listening` and `guest_socket_events` fail without this change and pass with it; `guest_tcp` and `guest_udp` pass. - A standalone program on the same Mac read the flag for a stream socket before and after `bind`, after `listen` with a backlog of 0, and for a UDP socket: it is set only after `listen`. - Linux and Windows: not run. There `NativeListening` makes the same `getsockopt` call as before. ### Checklist - [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 (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-18 14:13:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T14:13:40Z.


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 5021 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 6977 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 7201 core/libs/prx/libkernel/Socket/src/SocketOptions.cpp
core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 7437 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 7605 core/libs/prx/libkernel/Socket/src/Udp.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6250931917 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7559#issuecomment-6250931917). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-18T14:13:40Z. --- <!-- pr-overlap --> 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 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/Socket/src/Udp.cpp` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `core/libs/prx/libkernel/Socket/src/Udp.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/Socket/src/SocketOptions.cpp`<br>`core/libs/prx/libkernel/Socket/src/Udp.cpp` | | [PR 7437](https://www.github.com/boykopovar/AnyPS5/pull/7437) | `core/libs/prx/libkernel/Socket/src/Udp.cpp` | | [PR 7605](https://www.github.com/boykopovar/AnyPS5/pull/7605) | `core/libs/prx/libkernel/Socket/src/Udp.cpp` |
MarcZX10 commented 2026-10-18 14:24:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T14:24:04Z.

Review: APPROVED


Reviewed the shared NativeListening path and its three call sites. On Apple hosts it reads SO_ACCEPTCONN from proc_pidfdinfo; on other hosts it retains the existing getsockopt behavior. I found no correctness issue in the diff.

The PR reports focused macOS fail/pass results for the affected socket-option, connect, and event cases. I did not run the macOS reproduction locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5563371166 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7559#pullrequestreview-5563371166). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T14:24:04Z. **Review:** APPROVED --- Reviewed the shared `NativeListening` path and its three call sites. On Apple hosts it reads `SO_ACCEPTCONN` from `proc_pidfdinfo`; on other hosts it retains the existing `getsockopt` behavior. I found no correctness issue in the diff. The PR reports focused macOS fail/pass results for the affected socket-option, connect, and event cases. I did not run the macOS reproduction locally.
jotitadev commented 2026-10-18 14:44:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-18T14:44:04Z.


Checked the host side of this on a newer macOS and on native arm64, head 6f2a61ade.

Machine: Apple M2 Max, macOS 27.2 (26B5091g), Xcode 27 clang. A standalone C program that does exactly what NativeListening does on Apple hosts (proc_pidfdinfo(getpid(), fd, PROC_PIDFDSOCKETINFO, ...), then soi_options & SO_ACCEPTCONN), built for arm64 and for x86_64 (run under Rosetta 2). Both builds print the same table:

Socket getsockopt(SO_ACCEPTCONN) proc_pidfdinfo flag
TCP/IPv4, fresh -1, errno 42 (ENOPROTOOPT) 0
TCP/IPv4, after bind -1, errno 42 0
TCP/IPv4, after listen(0) -1, errno 42 1
TCP/IPv4, connected client -1, errno 42 0
TCP/IPv4, socket returned by accept -1, errno 42 0
TCP/IPv6, after listen(5) -1, errno 42 1
UDP/IPv4 -1, errno 42 0
AF_UNIX stream, after listen -1, errno 42 1
  • macOS 27.2 still rejects SO_ACCEPTCONN in getsockopt, so the change is still needed there.
  • The flag is set only on the listening socket. The socket that accept returns does not inherit it, so the EOPNOTSUPP check in connect (#6950) and the read-event path only take the listener branch for the listener.
  • proc_pidfdinfo returns 0 with the same errno getsockopt gives: EBADF for a closed descriptor, ENOTSOCK for a pipe. So HostError() after a failed NativeListening reports the same guest error as before.
  • Cost: about 1.4 µs per proc_pidfdinfo call against 0.4 µs for a getsockopt (200,000 calls, arm64 and Rosetta alike). The Equeue caller only reaches it when the data is below the low watermark.

Not run: the guest_socket_* tests. main does not link the libraries on macOS without the macOS series, and your run already covers them under Rosetta.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6251238591 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7559#issuecomment-6251238591). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-18T14:44:04Z. --- Checked the host side of this on a newer macOS and on native arm64, head `6f2a61ade`. Machine: Apple M2 Max, macOS 27.2 (26B5091g), Xcode 27 clang. A standalone C program that does exactly what `NativeListening` does on Apple hosts (`proc_pidfdinfo(getpid(), fd, PROC_PIDFDSOCKETINFO, ...)`, then `soi_options & SO_ACCEPTCONN`), built for arm64 and for x86_64 (run under Rosetta 2). Both builds print the same table: | Socket | `getsockopt(SO_ACCEPTCONN)` | `proc_pidfdinfo` flag | |---|---|---| | TCP/IPv4, fresh | -1, errno 42 (`ENOPROTOOPT`) | 0 | | TCP/IPv4, after `bind` | -1, errno 42 | 0 | | TCP/IPv4, after `listen(0)` | -1, errno 42 | 1 | | TCP/IPv4, connected client | -1, errno 42 | 0 | | TCP/IPv4, socket returned by `accept` | -1, errno 42 | 0 | | TCP/IPv6, after `listen(5)` | -1, errno 42 | 1 | | UDP/IPv4 | -1, errno 42 | 0 | | AF_UNIX stream, after `listen` | -1, errno 42 | 1 | - macOS 27.2 still rejects `SO_ACCEPTCONN` in `getsockopt`, so the change is still needed there. - The flag is set only on the listening socket. The socket that `accept` returns does not inherit it, so the `EOPNOTSUPP` check in `connect` (#6950) and the read-event path only take the listener branch for the listener. - `proc_pidfdinfo` returns 0 with the same errno `getsockopt` gives: `EBADF` for a closed descriptor, `ENOTSOCK` for a pipe. So `HostError()` after a failed `NativeListening` reports the same guest error as before. - Cost: about 1.4 µs per `proc_pidfdinfo` call against 0.4 µs for a `getsockopt` (200,000 calls, arm64 and Rosetta alike). The `Equeue` caller only reaches it when the data is below the low watermark. Not run: the `guest_socket_*` tests. `main` does not link the libraries on macOS without the macOS series, and your run already covers them under Rosetta. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
MarcZX10 commented 2026-10-18 14:46:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T14:46:09Z.


Thanks for checking both native arm64 and x86-64 under Rosetta on macOS 27.2. The accepted-listener-only flag, matching EBADF/ENOTSOCK behavior, and the roughly 1.4 µs call cost support the implementation’s scope; I have not run this newer host-side probe locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6251259020 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7559#issuecomment-6251259020). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T14:46:09Z. --- Thanks for checking both native arm64 and x86-64 under Rosetta on macOS 27.2. The accepted-listener-only flag, matching EBADF/ENOTSOCK behavior, and the roughly 1.4 µs call cost support the implementation’s scope; I have not run this newer host-side probe locally.
jotitadev commented 2026-10-18 15:45:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-18T15:45:20Z.


I also ran the library tests in a full x86-64 build under Rosetta: main e5f18ea99 with ci/macos-series-2 (fa6d8ae0b) and the open macOS fixes it uses. guest_socket_options, guest_socket_connect_listening and guest_socket_events pass. With this PR's three files reverted on the same tree, all three fail: getsockopt of an integer option failed (errno 42), and connect on a listening socket with an address of another family must fail with EOPNOTSUPP (45) (errno 47).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6251855740 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7559#issuecomment-6251855740). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-18T15:45:20Z. --- I also ran the library tests in a full x86-64 build under Rosetta: `main` e5f18ea99 with `ci/macos-series-2` (fa6d8ae0b) and the open macOS fixes it uses. `guest_socket_options`, `guest_socket_connect_listening` and `guest_socket_events` pass. With this PR's three files reverted on the same tree, all three fail: `getsockopt of an integer option failed (errno 42)`, and `connect on a listening socket with an address of another family must fail with EOPNOTSUPP (45) (errno 47)`.
LeandroLP23 commented 2026-10-18 16:44:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-18T16:44:30Z.


Windows run, since the description lists Linux and Windows as not run. Windows 11 26300, Ryzen AI 7 350, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. Build: main 6efb7261b with #6648, #7559, #7496, #7497 and #7498 merged together (each merge clean, built once).

The build is clean. guest_socket_connect_listening, guest_socket_events, guest_socket_options, guest_tcp and guest_udp pass 3 runs of 3 on Winsock. The SocketOptions.cpp change is under #ifdef __APPLE__, so this confirms the shared path is unchanged here.

AI-assisted

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6252461312 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7559#issuecomment-6252461312). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-10-18T16:44:30Z. --- Windows run, since the description lists Linux and Windows as not run. Windows 11 26300, Ryzen AI 7 350, WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`. Build: `main` `6efb7261b` with #6648, #7559, #7496, #7497 and #7498 merged together (each merge clean, built once). The build is clean. `guest_socket_connect_listening`, `guest_socket_events`, `guest_socket_options`, `guest_tcp` and `guest_udp` pass 3 runs of 3 on Winsock. The `SocketOptions.cpp` change is under `#ifdef __APPLE__`, so this confirms the shared path is unchanged here. AI-assisted
adriadam10 commented 2026-10-20 10:57:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-20T10:57:56Z.


Checked the diff against current main: NativeListening is the only reader of the listening state, every call site in Udp.cpp and SocketOptions.cpp goes through it, and non-Apple hosts keep the same getsockopt call. The recent socket changes on main don't touch it. Confirmed on macOS by @jotitadev and on Windows by @LeandroLP23; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6289323958 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7559#issuecomment-6289323958). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-20T10:57:56Z. --- Checked the diff against current main: `NativeListening` is the only reader of the listening state, every call site in `Udp.cpp` and `SocketOptions.cpp` goes through it, and non-Apple hosts keep the same `getsockopt` call. The recent socket changes on main don't touch it. Confirmed on macOS by @jotitadev and on Windows by @LeandroLP23; merged.
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!7559
No description provided.