Repository navigation
fix(libkernel): bind an unbound stream socket on listen on Windows - #16585
Conversation
|
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.
|
|
Linux check of the current head
AI-assisted: yes |
There was a problem hiding this comment.
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 newCheckUnboundListenfor 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_multicastfails on both sides withjoining 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 onmain, where the newlistenpath 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
left a comment
There was a problem hiding this comment.
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.
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>
49fb156 to
5baa8d2
Compare
|
Rebased onto main AI-assisted: yes. |
|
@gabichuuuuu and @Superseabas, thank you for the independent Windows checks, and for the red check with AI-assisted: yes. |
Superseabas
left a comment
There was a problem hiding this comment.
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.
|
Refreshed onto Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2.0 UCRT r7, Release.
AI-assisted: yes. |
#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.
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>
What
On Windows,
listenon a stream socket that was never bound failed withEINVAL. FreeBSD'stcp_usr_listenbinds 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 rejectslistenon an unbound socket withWSAEINVAL:(a standalone Winsock probe on this machine; listens on a bound socket succeed for every backlog, including
0, negatives,INT_MINandINT_MAX.)listen_nid_postfixnow handles this on Windows only. When::listenfails withEINVAL, andgetsocknameshows theAF_INETorAF_INET6stream socket is unbound, it binds the socket to the wildcard address with port 0 and retrieslisten. 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_backlogtest calls_listenon an unbound socket and fails here witherrno 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
main9b0fdbfb24.guest_socket_port_zerogets a newCheckUnboundListenforAF_INETandAF_INET6. It callslistenwith nobind, checks thatgetsocknamereports a nonzero port, then connects to that port on loopback and accepts. Passes in 3 of 3 runs.Udp.cppchange reverted (rebuilt,Udp.cpprecompiled), the same test fails atlisten on an unbound stream socket binds it to an ephemeral port (errno 22).guest_socket_*,guest_refused_connect,guest_poll*,guest_net_kernel_forwards*,guest_sce_net*andguest_kernel_socket*pass.check_conventions.pypasses.#ifdef _WIN32. The hosted Windows check does not link these guest tests on pull requests.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)