fix(libSceNet): model the guest listen queue past its backlog on macOS #22623

Merged
011010 merged 1 commit from upstream-pr/22623 into main 2026-11-26 09:18:38 +00:00
011010 commented 2026-11-17 23:26:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by 011010 on 2026-11-17T23:26:25Z.


What

guest_sce_net fails on macOS in CheckConnectTimeoutExpiry:

connect timeout expiry: no connect stayed pending after 64 completed connects (last result 0x0, threw 0, 0 ms)
guest_sce_net check failed at line 1212, sceNet errno 61

The case creates a listener with sceNetListen(listener, 0) and connects until one stays pending, because the console stops completing them once the queue is full. macOS does not bound a listen(0) queue at one: 64 connects to such a listener complete without any accept, measured with a host socket.

The console depth is not a measurement guess. FreeBSD 11.4 derives it: solisten_proto keeps the backlog as so_qlimit (uipc_socket.c:747-749), and sonewconn drops a connection once so_qlen > 3 * so_qlimit / 2 (uipc_socket.c:570). A backlog of 0 completes one connection and drops every later one while none is accepted.

Each guest listener now counts its completed but unaccepted connections. sceNetListen stores the backlog, sceNetAccept releases one, and a blocking sceNetConnect with a connect timeout to a guest listener past 3 * backlog / 2 follows FreeBSD's drop: the host still completes the connection into its own unbounded queue, the guest waits the timeout and gets EAGAIN, and the expired-connect retry keeps returning EADDRINUSE. Everything is under __APPLE__; Linux and Windows build and behave as before.

The debt fragment records the bounds: a non-blocking connect past the bound, a connection from outside the guest socket set (which fills the host queue without the count), a negative backlog clamped to FreeBSD's default somaxconn 128, and the count resetting when the listener closes.

Tested

  • macOS 27.0.1 (26A434), Apple M4 Mac mini, Apple clang 21.0.0, x86-64 Release under Rosetta 2, main 286ee48fb9 (which already has #22442, so the connect-refusal case passes and the test reaches this one).
  • Before: connect timeout expiry: no connect stayed pending after 64 completed connects, then check failed at line 1212, sceNet errno 61.
  • After: guest_sce_net passes end to end, 2.20 s. The expiry case finds its pending connect on the second attempt, the expired retry returns 0x80410123 after at least the 100 ms timeout, and the follow-up retry returns EADDRINUSE (48) and then throws.
  • Host probe establishing the macOS behaviour, x86-64 under Rosetta 2: a listener created with listen(0) completes 64 of 64 connects with none refused.
  • guest_socket_pair, guest_tcp, guest_udp and guest_net_ctl pass. guest_socket_options fails with the same message with and without this change (a connect after a refused nonblocking one must fail with ECONNREFUSED (61), not start another attempt (errno 56)), the non-blocking gap the debt entry names.
  • python3 tools/check_conventions.py --base origin/main: all conventions checks passed.
  • python3 tools/pr_overlap.py --names GuestLoopbackSocket and --names NoteGuestListenerConnect: not declared on main and on no open pull request.
  • python3 tools/pr_overlap.py --branch fix/macos-guest-listen-queue: the same five open pull requests share libSceNet/Export.cpp, the libkernel cancellation series; none of them edits sceNetConnect, sceNetListen or sceNetAccept.
  • Not tested: a console, and a title.

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: #22443
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5366847475 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22623). Originally posted by [011010](https://github.com/011010) on 2026-11-17T23:26:25Z. --- ### What `guest_sce_net` fails on macOS in `CheckConnectTimeoutExpiry`: ``` connect timeout expiry: no connect stayed pending after 64 completed connects (last result 0x0, threw 0, 0 ms) guest_sce_net check failed at line 1212, sceNet errno 61 ``` The case creates a listener with `sceNetListen(listener, 0)` and connects until one stays pending, because the console stops completing them once the queue is full. macOS does not bound a `listen(0)` queue at one: 64 connects to such a listener complete without any accept, measured with a host socket. The console depth is not a measurement guess. FreeBSD 11.4 derives it: `solisten_proto` keeps the backlog as `so_qlimit` ([uipc_socket.c:747-749](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_socket.c#L747-L749)), and `sonewconn` drops a connection once `so_qlen > 3 * so_qlimit / 2` ([uipc_socket.c:570](https://github.com/freebsd/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_socket.c#L570)). A backlog of 0 completes one connection and drops every later one while none is accepted. Each guest listener now counts its completed but unaccepted connections. `sceNetListen` stores the backlog, `sceNetAccept` releases one, and a blocking `sceNetConnect` with a connect timeout to a guest listener past `3 * backlog / 2` follows FreeBSD's drop: the host still completes the connection into its own unbounded queue, the guest waits the timeout and gets `EAGAIN`, and the expired-connect retry keeps returning `EADDRINUSE`. Everything is under `__APPLE__`; Linux and Windows build and behave as before. The debt fragment records the bounds: a non-blocking connect past the bound, a connection from outside the guest socket set (which fills the host queue without the count), a negative backlog clamped to FreeBSD's default `somaxconn` 128, and the count resetting when the listener closes. ### Tested - macOS 27.0.1 (26A434), Apple M4 Mac mini, Apple clang 21.0.0, x86-64 Release under Rosetta 2, `main` `286ee48fb9` (which already has #22442, so the connect-refusal case passes and the test reaches this one). - Before: `connect timeout expiry: no connect stayed pending after 64 completed connects`, then `check failed at line 1212, sceNet errno 61`. - After: `guest_sce_net` **passes end to end, 2.20 s**. The expiry case finds its pending connect on the second attempt, the expired retry returns `0x80410123` after at least the 100 ms timeout, and the follow-up retry returns `EADDRINUSE` (48) and then throws. - Host probe establishing the macOS behaviour, x86-64 under Rosetta 2: a listener created with `listen(0)` completes 64 of 64 connects with none refused. - `guest_socket_pair`, `guest_tcp`, `guest_udp` and `guest_net_ctl` pass. `guest_socket_options` fails with the same message with and without this change (`a connect after a refused nonblocking one must fail with ECONNREFUSED (61), not start another attempt (errno 56)`), the non-blocking gap the debt entry names. - `python3 tools/check_conventions.py --base origin/main`: all conventions checks passed. - `python3 tools/pr_overlap.py --names GuestLoopbackSocket` and `--names NoteGuestListenerConnect`: not declared on `main` and on no open pull request. - `python3 tools/pr_overlap.py --branch fix/macos-guest-listen-queue`: the same five open pull requests share `libSceNet/Export.cpp`, the libkernel cancellation series; none of them edits `sceNetConnect`, `sceNetListen` or `sceNetAccept`. - Not tested: a console, and a title. ### 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 - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: #22443 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-11-17 23:30:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T23:30:06Z.

Review: DISMISSED


Reviewed — changes requested

Changes are required before merge: ensure a failed sceNetAccept setup does not leave the guest pending-accept count out of sync with the host queue.

What I ran

  • Reviewed head 29ab20ffe7b95c1cd9f78ec0a73ac145457a920f; base is main.
  • Read review/description.md, review/commits.md, review/manifest.json, review/diff.patch, and the full text of both changed files. No commands or tests were run.

Results

Check Result Note
Diff review failed One verified accounting defect
Tests not run No independent test run

Findings

Blocking

  • core/libs/prx/libSceNet/Export.cpp:1530: The listener's pendingAccepts count is decremented immediately after the host accept succeeds, before reserving a guest descriptor and before copying the peer address to guest memory. If either later step fails, the accepted socket is closed or discarded, but the host connection has already left the accept queue and the guest count has been released. Subsequent connects can therefore be treated as below the guest backlog limit while the host queue still has no corresponding guest-accepted connection, breaking the modeled queue behavior. Decrement only once the accept operation is successfully committed, or restore the count on every post-accept failure.

Non-blocking

  • None.

Not verified

  • No tests or build were run independently; the described macOS and CI results are author-reported.
  • No physical console or title behavior was verified.
  • Both changed files were read; files.json is not available in the prepared review folder.

To get this to a good state

  • Keep pendingAccepts consistent when descriptor reservation or guest address copying fails; then rerun the relevant guest_sce_net checks.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5948399677 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22623#pullrequestreview-5948399677). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-17T23:30:06Z. **Review:** DISMISSED --- # Reviewed — changes requested Changes are required before merge: ensure a failed `sceNetAccept` setup does not leave the guest pending-accept count out of sync with the host queue. ## What I ran - Reviewed head `29ab20ffe7b95c1cd9f78ec0a73ac145457a920f`; base is `main`. - Read `review/description.md`, `review/commits.md`, `review/manifest.json`, `review/diff.patch`, and the full text of both changed files. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Diff review | failed | One verified accounting defect | | Tests | not run | No independent test run | ## Findings ### Blocking - `core/libs/prx/libSceNet/Export.cpp:1530`: The listener's `pendingAccepts` count is decremented immediately after the host `accept` succeeds, before reserving a guest descriptor and before copying the peer address to guest memory. If either later step fails, the accepted socket is closed or discarded, but the host connection has already left the accept queue and the guest count has been released. Subsequent connects can therefore be treated as below the guest backlog limit while the host queue still has no corresponding guest-accepted connection, breaking the modeled queue behavior. Decrement only once the accept operation is successfully committed, or restore the count on every post-accept failure. ### Non-blocking - None. ## Not verified - No tests or build were run independently; the described macOS and CI results are author-reported. - No physical console or title behavior was verified. - Both changed files were read; `files.json` is not available in the prepared review folder. ## To get this to a good state - Keep `pendingAccepts` consistent when descriptor reservation or guest address copying fails; then rerun the relevant `guest_sce_net` checks.
Jazeupoi commented 2026-11-18 10:04:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Jazeupoi on 2026-11-18T10:04:23Z.

Review: COMMENTED


Tested on a second Apple M4 (MacBook Air, macOS 27.0.1), x86-64 Release under Rosetta 2, main bdc1343e5f with #17425 and #11049 (needed for the macOS build), head 29ab20ffe7.

ctest -R '^guest_sce_net$' --output-on-failure, 3 runs each:

run 1 run 2 run 3
main fails, 3.8 s fails, 1.7 s timeout, 164 s
this PR passes, 3.5 s passes, 1.8 s passes, 1.8 s

Every main run reports connect timeout expiry: no connect stayed pending after 64 completed connects (last result 0x0, threw 0, 0 ms); the third one then hung until the ctest timeout. With this PR the test passes end to end in every run.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5954421641 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22623#pullrequestreview-5954421641). Originally posted by [Jazeupoi](https://github.com/Jazeupoi) on 2026-11-18T10:04:23Z. **Review:** COMMENTED --- Tested on a second Apple M4 (MacBook Air, macOS 27.0.1), x86-64 Release under Rosetta 2, `main` `bdc1343e5f` with #17425 and #11049 (needed for the macOS build), head `29ab20ffe7`. `ctest -R '^guest_sce_net$' --output-on-failure`, 3 runs each: | | run 1 | run 2 | run 3 | |---|---|---|---| | `main` | fails, 3.8 s | fails, 1.7 s | timeout, 164 s | | this PR | passes, 3.5 s | passes, 1.8 s | passes, 1.8 s | Every `main` run reports `connect timeout expiry: no connect stayed pending after 64 completed connects (last result 0x0, threw 0, 0 ms)`; the third one then hung until the ctest timeout. With this PR the test passes end to end in every run. AI-assisted: yes (Claude Code)
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!22623
No description provided.