Skip to content

fix(libkernel): bind an unbound stream socket on listen on Windows - #16585

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
JJenkx:windows/libkernel-listen-autobind
Nov 10, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
JJenkx:windows/libkernel-listen-autobind

Conversation

@JJenkx

@JJenkx JJenkx commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

What

On Windows, listen on a stream socket that was never bound failed with EINVAL. FreeBSD's tcp_usr_listen binds a socket without a local port to an ephemeral port on the wildcard address (in_pcbbind / in6_pcbbind) before listening, and Linux does the same. Winsock instead rejects listen on an unbound socket with WSAEINVAL:

unbound listen(0) -> -1 wsa=10022 (WSAEINVAL=10022)

(a standalone Winsock probe on this machine; listens on a bound socket succeed for every backlog, including 0, negatives, INT_MIN and INT_MAX.)

listen_nid_postfix now handles this on Windows only. When ::listen fails with EINVAL, and getsockname shows the AF_INET or AF_INET6 stream socket is unbound, it binds the socket to the wildcard address with port 0 and retries listen. A bound socket, other families and every other error take the existing path unchanged, and nothing changes off Windows.

Found while checking #16577 on Windows, whose new guest_socket_backlog test calls _listen on an unbound socket and fails here with errno 22. This PR fixes the library behaviour that test depends on; it doesn't change #16577.

Tested

Windows 11 Enterprise 22621, AMD Ryzen 9 5950X, WinLibs GCC 15.2 UCRT, Release, on main 9b0fdbfb24.

  • guest_socket_port_zero gets a new CheckUnboundListen for AF_INET and AF_INET6. It calls listen with no bind, checks that getsockname reports a nonzero port, then connects to that port on loopback and accepts. Passes in 3 of 3 runs.
  • Negative control: with only the Udp.cpp change reverted (rebuilt, Udp.cpp recompiled), the same test fails at listen on an unbound stream socket binds it to an ephemeral port (errno 22).
  • All 34 tests matching guest_socket_*, guest_refused_connect, guest_poll*, guest_net_kernel_forwards*, guest_sce_net* and guest_kernel_socket* pass.
  • check_conventions.py passes.
  • Not tested on Linux; the change is inside #ifdef _WIN32. The hosted Windows check does not link these guest tests on pull requests.

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

@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

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 7088 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 11896 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12682 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12688 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12700 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12965 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 16826 core/libs/prx/libkernel/Socket/src/Udp.cpp

@JJenkx

JJenkx commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

On the overlap report: I checked the listed PRs.

AI-assisted: yes.

@JJenkx

JJenkx commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Linux check of the current head 49fb156488, built from that exact commit. The change is for Windows; this confirms the shared test still holds on Linux. Arch Linux 7.2.11, Ryzen 9 7950X, GCC 16.2.1, Release, BUILD_TESTING=ON.

ctest -R '^guest_(socket|refused_connect)' --repeat until-fail:3: 17 of 17 pass in all three repeats, none skipped. That includes guest_socket_port_zero, which holds this PR's new check. tools/check_conventions.py reports no errors; its two debt-length warnings are in libSceNKWebKit.md and libSceNpFriendListDialog.md, which this PR doesn't touch.

AI-assisted: yes

@gabichuuuuu gabichuuuuu left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An independent Windows check of head 49fb156488.

Setup: Windows 11 x64, WinLibs GCC 15.2.0 r7 UCRT, Release, full build. Head merged onto main 4e44ed59b2 (clean merge), with main 4e44ed59b2 from the same build directory as the control. Each switch rebuilt Udp.cpp and the changed test before running.

ctest -R '^guest_socket_', alternating main and this PR for 5 rounds:

all 29 pass other results
main 3 of 5 1 round with guest_socket_options_multicast, 1 round with two host_write_sceNetRecv* timeouts
this PR 4 of 5 1 round with guest_socket_options_multicast
  • guest_socket_port_zero, with the new CheckUnboundListen for IPv4 and IPv6, passes in every run with this PR, usually in about 0.03 s (5 s once, in a run where the machine stalled).
  • The failures are not from this change. guest_socket_options_multicast fails on both sides with joining 224.0.0.251 through INADDR_ANY failed (errno 65), which depends on this host's network, and the 20 s timeouts showed up on both sides in tests that pass on main, where the new listen path does not exist. Earlier non-alternating runs had more timeouts on the PR side only because they ran later, while the machine was stalling. Alternating the two removed that difference.

AI-assisted: yes.

@Superseabas Superseabas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows check of head 49fb156488, merged onto main 090f57166a (clean merge).

Setup: Windows 11 Home 10.0.26200, Ryzen 7 7700X, WinLibs GCC 16.2.0 (x86_64-ucrt-posix-seh, r3), Release, BUILD_TESTING=ON. Run from PowerShell.

Socket and Net tests (ctest -R '(^|_)(net|sce_net|socket|sock|reuse|ipv6only|setsockopt|getsockopt|linger|poll|refused)' -E '^agc' -j 8, 132 tests):

runs result
main 2 132 / 132
this PR 3 132 / 132 each time

Red check: with main's Udp.cpp and the PR's test, guest_socket_port_zero fails 3 of 3 at listen on an unbound stream socket binds it to an ephemeral port (errno 22).

Reading the code: skipping bind_nid_postfix for the implicit bind loses nothing for a stream socket. There it's a plain ::bind (localAddressCleared is only reset for datagram sockets), so binding the wildcard directly matches what an explicit bind to it would do.

A gap outside this PR's scope: libSceNet's sceNetListen calls Winsock's ::listen directly (Export.cpp), so it doesn't get this fix. I built a probe on this PR's head (local only) that creates an unbound stream socket through each library and calls listen with a backlog of 0:

probe sceNetListen family 2: result 0x80410116 errno 22, getsockname 0 port 0
probe listen (libkernel) family 2: result 0 errno 0
probe sceNetListen family 28: result 0x80410116 errno 22, getsockname 0 port 0
probe listen (libkernel) family 28: result 0 errno 0

So with this PR, libkernel listen binds and listens, and sceNetListen on the same kind of socket still fails with EINVAL on Windows. That doesn't block this PR, whose description scopes it to listen_nid_postfix. Titles that listen through libSceNet without binding first would need the same change in sceNetListen, as a follow-up.

tools/check_conventions.py --base main passes.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 8, 2026
FreeBSD and Linux give a stream socket that was never bound an ephemeral
port on the wildcard address when it starts listening. Winsock instead
fails listen on an unbound socket with WSAEINVAL, so a guest listen
without a prior bind returned EINVAL on Windows only.

When listen fails with EINVAL on an unbound AF_INET or AF_INET6 stream
socket, bind it to the wildcard address with port 0 and retry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JJenkx
JJenkx force-pushed the windows/libkernel-listen-autobind branch from 49fb156 to 5baa8d2 Compare November 8, 2026 05:40
@JJenkx

JJenkx commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main 8a493fc113 (new head 5baa8d229e). The conflict in GuestSocketPortZero.cpp was main's new CheckUnspecifiedInet next to this PR's CheckUnboundListen, and I kept both. guest_socket_port_zero passes 3 of 3 runs on Windows (WinLibs GCC 15.2 UCRT, Release), and check_conventions.py passes.

AI-assisted: yes.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 8, 2026
@JJenkx

JJenkx commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

@gabichuuuuu and @Superseabas, thank you for the independent Windows checks, and for the red check with main's Udp.cpp. The sceNetListen gap is now covered by #16852 (also from @Superseabas). On this machine guest_sce_net_listen_ipv4 and guest_sce_net_listen_ipv6 pass 3 of 3 with it (head 248e68fe48), so I'll leave this PR limited to listen_nid_postfix. Since the rebase onto 8a493fc113 it is head 5baa8d229e.

AI-assisted: yes.

@Superseabas Superseabas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approving after the rebase. The lines this PR changes at 5baa8d229e are identical to 49fb156488, which I approved after testing it on Windows. I compared the diffs and did not re-run the tests on this head.

AI-assisted: yes.

@oneandonlydean oneandonlydean self-assigned this Nov 9, 2026
@JJenkx

JJenkx commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Refreshed onto main 561fb7672f. The new head is 56dd0cf1ee, a merge of main with no other changes; its tree matches the merge I tested.

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2.0 UCRT r7, Release.

  • guest_socket_port_zero passes in 3 of 3 runs, none skipped.

AI-assisted: yes.

@oneandonlydean
oneandonlydean merged commit 828c974 into boykopovar:main Nov 10, 2026
12 of 14 checks passed
adriadam10 added a commit that referenced this pull request Nov 10, 2026
#16585 added CheckUnboundListen with listen(fd, 0). On Linux a backlog of 0 makes the
SYN queue count as full, so the SYN is dropped unless syncookies rescue it and
connect retries until the 20 s ctest timeout (run 42732003769, guest_socket_port_zero
Timeout; same in 42732130842). Every other socket test listens with 1 or more.
oneandonlydean pushed a commit that referenced this pull request Nov 11, 2026
Winsock refuses listen on a stream socket that was never bound
(WSAEINVAL), while FreeBSD's tcp_usr_listen binds it to an ephemeral
port on the wildcard address first. On Windows, sceNetListen now binds
such an IPv4 or IPv6 socket to the wildcard address with port 0 when
listen fails with EINVAL and the socket has no local address, then
listens again. This is the libSceNet counterpart of #16585, which does
the same in libkernel's listen.

guest_sce_net_listen_ipv4 and _ipv6 listen on an unbound socket, read
its port back, connect to it on loopback and accept, and check that a
later sceNetBind fails with EINVAL; a socket bound first keeps its port.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants