fix(libkernel): refuse a reconnect after a failed nonblocking connect on Windows #14183

Merged
JJenkx merged 2 commits from upstream-pr/14183 into main 2026-11-07 13:36:18 +00:00
JJenkx commented 2026-10-31 22:39:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-31T22:39:35Z.


What

On Windows, calling connect again after a refused nonblocking stream connect started a fresh connection attempt.

Each repeat returned EINPROGRESS (36), while SO_ERROR held ECONNREFUSED (61). So a title that keeps calling connect until it stops reporting EINPROGRESS or EALREADY never sees the refusal and keeps retrying the dead server.

FreeBSD 11.4's tcp_usr_connect answers a socket whose connection was dropped (INP_DROPPED) with ECONNREFUSED, whether a reset or a timeout dropped it.

Measured on main with a probe in guest_socket_options (nonblocking socket, four connect calls, poll after the second):

Target connect 0 connect 1 after settling SO_ERROR
closed loopback port, main 36 36 36, 36 61
closed loopback port, this PR 36 36 61, 61 61
listener 36 56 56, 56 0
unanswered 192.0.2.1:9 (attempt still in flight) 36 37 — —

The in-flight case already returns EALREADY, because Winsock reports WSAEALREADY there. Only the case after a failed attempt was wrong.

The fix applies on Windows to stream sockets:

  • A WSAEWOULDBLOCK from connect marks the socket as having a pending attempt.
  • The next connect reads the host SO_ERROR. Winsock does not clear it on read, so this does not consume the error the guest reads later through SO_ERROR.
  • If the pending attempt failed, that connect and every later one return ECONNREFUSED without calling Winsock again.
  • A completed connection clears the mark.

Linux and macOS are unchanged.

sceNetConnect had the same gap, so a second commit applies the same rule there:

  • The socket's Sock entry remembers a pending connect (WSAEWOULDBLOCK on a guest nonblocking socket, or WSAEALREADY).
  • While an attempt is pending, sceNetConnect reads the host SO_ERROR before it resets reported_error. Once the attempt has failed, it returns 0x80410100 | ECONNREFUSED from then on.

The Windows sentence of the "Socket error numbers" entry in docs/dev/debt/libkernel.md described this wrongly: it said a repeat "keeps reporting EALREADY". It now describes the new behaviour.

Tested

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, full build with BUILD_TESTING=ON, on main 7a5d9bf02a.

  • New check: guest_socket_options ReconnectAfterARefusalFails. It makes a nonblocking connect to a closed loopback port, polls until the attempt settles, and requires the next connect to fail with ECONNREFUSED.
    • With this change: passes 3/3.
    • With Udp.cpp restored to main (the control build relinked libkernel.prx): fails with a connect after a refused nonblocking one must fail with ECONNREFUSED (61), not start another attempt (errno 36).
  • New check for sceNetConnect in guest_refused_connect: a nonblocking sceNetSocket (SO_NBIO) connects to the closed loopback port. It retries while the answer is EINPROGRESS or EALREADY, for at most 3 s, and must end with 0x80410100 | 61 and errno 61.
    • With the change: passes 3/3.
    • With libSceNet/Export.cpp restored (the control build relinked libSceNet.prx): aborts after the 3 s deadline.
  • Regression set: ctest -R "socket|refused|udp|tcp|net_|_net|poll|select|connect|http|ssl" -j8 --repeat until-fail:3: 207/207 pass with both commits.
  • Not run: Linux and macOS.
    • The test asserts only the first connect after the refusal. As I read Linux's __inet_stream_connect, a socket still marked connecting gets the pending socket error from that call. I have not run it there, and CI will.
    • No title was tested.

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:5102208550 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14183). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-31T22:39:35Z. --- ### What On Windows, calling `connect` again after a refused nonblocking stream `connect` started a fresh connection attempt. Each repeat returned `EINPROGRESS` (36), while `SO_ERROR` held `ECONNREFUSED` (61). So a title that keeps calling `connect` until it stops reporting `EINPROGRESS` or `EALREADY` never sees the refusal and keeps retrying the dead server. FreeBSD 11.4's [`tcp_usr_connect`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/netinet/tcp_usrreq.c#L511-L558) answers a socket whose connection was dropped (`INP_DROPPED`) with `ECONNREFUSED`, whether a reset or a timeout dropped it. Measured on `main` with a probe in `guest_socket_options` (nonblocking socket, four `connect` calls, poll after the second): | Target | `connect` 0 | `connect` 1 | after settling | `SO_ERROR` | |---|---|---|---|---| | closed loopback port, `main` | 36 | 36 | 36, 36 | 61 | | closed loopback port, this PR | 36 | 36 | **61, 61** | 61 | | listener | 36 | 56 | 56, 56 | 0 | | unanswered `192.0.2.1:9` (attempt still in flight) | 36 | 37 | — | — | The in-flight case already returns `EALREADY`, because Winsock reports `WSAEALREADY` there. Only the case after a failed attempt was wrong. The fix applies on Windows to stream sockets: - A `WSAEWOULDBLOCK` from `connect` marks the socket as having a pending attempt. - The next `connect` reads the host `SO_ERROR`. Winsock does not clear it on read, so this does not consume the error the guest reads later through `SO_ERROR`. - If the pending attempt failed, that `connect` and every later one return `ECONNREFUSED` without calling Winsock again. - A completed connection clears the mark. Linux and macOS are unchanged. `sceNetConnect` had the same gap, so a second commit applies the same rule there: - The socket's `Sock` entry remembers a pending connect (`WSAEWOULDBLOCK` on a guest nonblocking socket, or `WSAEALREADY`). - While an attempt is pending, `sceNetConnect` reads the host `SO_ERROR` before it resets `reported_error`. Once the attempt has failed, it returns `0x80410100 | ECONNREFUSED` from then on. The Windows sentence of the "Socket error numbers" entry in `docs/dev/debt/libkernel.md` described this wrongly: it said a repeat "keeps reporting `EALREADY`". It now describes the new behaviour. ### Tested Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, full build with `BUILD_TESTING=ON`, on `main` `7a5d9bf02a`. - **New check:** `guest_socket_options` `ReconnectAfterARefusalFails`. It makes a nonblocking connect to a closed loopback port, polls until the attempt settles, and requires the next `connect` to fail with `ECONNREFUSED`. - With this change: passes 3/3. - With `Udp.cpp` restored to `main` (the control build relinked `libkernel.prx`): fails with `a connect after a refused nonblocking one must fail with ECONNREFUSED (61), not start another attempt (errno 36)`. - **New check for `sceNetConnect` in `guest_refused_connect`:** a nonblocking `sceNetSocket` (`SO_NBIO`) connects to the closed loopback port. It retries while the answer is `EINPROGRESS` or `EALREADY`, for at most 3 s, and must end with `0x80410100 | 61` and errno 61. - With the change: passes 3/3. - With `libSceNet/Export.cpp` restored (the control build relinked `libSceNet.prx`): aborts after the 3 s deadline. - **Regression set:** `ctest -R "socket|refused|udp|tcp|net_|_net|poll|select|connect|http|ssl" -j8 --repeat until-fail:3`: 207/207 pass with both commits. - **Not run:** Linux and macOS. - The test asserts only the first `connect` after the refusal. As I read Linux's `__inet_stream_connect`, a socket still marked connecting gets the pending socket error from that call. I have not run it there, and CI will. - No title was tested. ### 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
MarcZX10 commented 2026-10-31 23:13:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T23:13:07Z.

Review: APPROVED


Reviewed — looks good

The Windows-only state tracking preserves a failed nonblocking stream connect and makes a later connect report ECONNREFUSED; the new tests exercise both guest socket APIs.

What I ran

Reviewed head 80c8ea873ee9adfc5f6e9861cc34ada1e0607b25 against PR base 7a5d9bf02af636382aec927593b1a5867899ed0f; merge preview was clean. Environment: Linux x86_64, Python 3.11.2. No local C++ build or CTest was run.

python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 14183 --output-dir /tmp/anyps5-pr-precheck-14183
gh pr checks 14183 --repo boykopovar/AnyPS5

Results

Check Result
Diff check and repository conventions Passed
Dependency/merge preview Passed; clean
Hosted Windows job Passed
Hosted eligibility, library portability, report Passed
Hosted labels Pending at review time
Linux and specialized hosted jobs Skipped by workflow
Local build and CTest Not run
ABI/test-quality helper checks Not executable: helper scripts are absent from the extracted PR head
Raw hosted check summary
conventions	pass	28s	https://github.com/boykopovar/AnyPS5/actions/runs/41249819968/job/124680802526
windows	pass	4m39s	https://github.com/boykopovar/AnyPS5/actions/runs/41249807957/job/124682460781
labels	pending	0	https://github.com/boykopovar/AnyPS5/actions/runs/41249819990/job/124683721469
library-portability	pass	1m15s	https://github.com/boykopovar/AnyPS5/actions/runs/41249807923/job/124680763937
report	pass	37s	https://github.com/boykopovar/AnyPS5/actions/runs/41249807787/job/124680763189
linux	skipping	0	https://github.com/boykopovar/AnyPS5/actions/runs/41249807957/job/124682461864
register-context	skipping	0	https://github.com/boykopovar/AnyPS5/actions/runs/41249807957/job/124682461939
relinker	skipping	0	https://github.com/boykopovar/AnyPS5/actions/runs/41249807957/job/124682461681
relinker-asan	skipping	0	https://github.com/boykopovar/AnyPS5/actions/runs/41249807957/job/124682462129
write-watch	skipping	0	https://github.com/boykopovar/AnyPS5/actions/runs/41249807957/job/124682462015
eligibility	pass	6s	https://github.com/boykopovar/AnyPS5/actions/runs/41249807957/job/124680781983

Negative control

No negative control was run. The regression is timing-dependent and Windows-specific; I relied on the hosted Windows job and the new repeated-connect tests rather than altering the implementation locally.

Sources

Not verified

  • No local build, CTest, Linux/macOS execution, or console behavior.
  • The hosted Windows check passed, but I did not inspect its full log in this review.

To get this to a good state

No blocking change is needed based on the reviewed head and available checks. The only outstanding hosted result I saw was the labels check.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5725396837 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14183#pullrequestreview-5725396837). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-31T23:13:07Z. **Review:** APPROVED --- # Reviewed — looks good The Windows-only state tracking preserves a failed nonblocking stream connect and makes a later connect report `ECONNREFUSED`; the new tests exercise both guest socket APIs. ## What I ran Reviewed head `80c8ea873ee9adfc5f6e9861cc34ada1e0607b25` against PR base `7a5d9bf02af636382aec927593b1a5867899ed0f`; merge preview was clean. Environment: Linux x86_64, Python 3.11.2. No local C++ build or CTest was run. ```sh python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 14183 --output-dir /tmp/anyps5-pr-precheck-14183 gh pr checks 14183 --repo boykopovar/AnyPS5 ``` ## Results | Check | Result | |---|---| | Diff check and repository conventions | Passed | | Dependency/merge preview | Passed; clean | | Hosted Windows job | Passed | | Hosted eligibility, library portability, report | Passed | | Hosted labels | Pending at review time | | Linux and specialized hosted jobs | Skipped by workflow | | Local build and CTest | Not run | | ABI/test-quality helper checks | Not executable: helper scripts are absent from the extracted PR head | <details><summary>Raw hosted check summary</summary> ```text conventions pass 28s https://github.com/boykopovar/AnyPS5/actions/runs/41249819968/job/124680802526 windows pass 4m39s https://github.com/boykopovar/AnyPS5/actions/runs/41249807957/job/124682460781 labels pending 0 https://github.com/boykopovar/AnyPS5/actions/runs/41249819990/job/124683721469 library-portability pass 1m15s https://github.com/boykopovar/AnyPS5/actions/runs/41249807923/job/124680763937 report pass 37s https://github.com/boykopovar/AnyPS5/actions/runs/41249807787/job/124680763189 linux skipping 0 https://github.com/boykopovar/AnyPS5/actions/runs/41249807957/job/124682461864 register-context skipping 0 https://github.com/boykopovar/AnyPS5/actions/runs/41249807957/job/124682461939 relinker skipping 0 https://github.com/boykopovar/AnyPS5/actions/runs/41249807957/job/124682461681 relinker-asan skipping 0 https://github.com/boykopovar/AnyPS5/actions/runs/41249807957/job/124682462129 write-watch skipping 0 https://github.com/boykopovar/AnyPS5/actions/runs/41249807957/job/124682462015 eligibility pass 6s https://github.com/boykopovar/AnyPS5/actions/runs/41249807957/job/124680781983 ``` </details> ## Negative control No negative control was run. The regression is timing-dependent and Windows-specific; I relied on the hosted Windows job and the new repeated-connect tests rather than altering the implementation locally. ## Sources - [Windows connect-state implementation at this PR head](https://github.com/boykopovar/AnyPS5/blob/80c8ea873ee9adfc5f6e9861cc34ada1e0607b25/core/libs/prx/libSceNet/Export.cpp#L281-L284) - [Kernel socket connect-state implementation at this PR head](https://github.com/boykopovar/AnyPS5/blob/80c8ea873ee9adfc5f6e9861cc34ada1e0607b25/core/libs/prx/libkernel/Socket/src/Udp.cpp#L1369-L1435) - [New regression cases at this PR head](https://github.com/boykopovar/AnyPS5/blob/80c8ea873ee9adfc5f6e9861cc34ada1e0607b25/core/libs/tests/GuestRefusedConnect.cpp#L92-L104) - [Existing socket-option regression case at this PR head](https://github.com/boykopovar/AnyPS5/blob/80c8ea873ee9adfc5f6e9861cc34ada1e0607b25/core/libs/tests/GuestSocketOptions.cpp#L125-L147) - [Updated behavior note at this PR head](https://github.com/boykopovar/AnyPS5/blob/80c8ea873ee9adfc5f6e9861cc34ada1e0607b25/docs/dev/debt/libkernel.md#L275-L276) ## Not verified - No local build, CTest, Linux/macOS execution, or console behavior. - The hosted Windows check passed, but I did not inspect its full log in this review. ## To get this to a good state No blocking change is needed based on the reviewed head and available checks. The only outstanding hosted result I saw was the labels check.
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!14183
No description provided.