fix(libkernel): bind an unbound stream socket on listen on Windows #16585
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16585"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-11-07T07:02:12Z.
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)Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T15:01:08Z.
Review: COMMENTED
An independent Windows check of head
49fb156488.Setup: Windows 11 x64, WinLibs GCC 15.2.0 r7 UCRT, Release, full build. Head merged onto
main4e44ed59b2(clean merge), withmain4e44ed59b2from the same build directory as the control. Each switch rebuiltUdp.cppand the changed test before running.ctest -R '^guest_socket_', alternatingmainand this PR for 5 rounds:mainguest_socket_options_multicast, 1 round with twohost_write_sceNetRecv*timeoutsguest_socket_options_multicastguest_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).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.
Imported from boykopovar/AnyPS5. Originally posted by Superseabas on 2026-11-07T17:49:14Z.
Review: APPROVED
Windows check of head
49fb156488, merged ontomain090f57166a(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):mainRed check: with
main'sUdp.cppand the PR's test,guest_socket_port_zerofails 3 of 3 atlisten on an unbound stream socket binds it to an ephemeral port (errno 22).Reading the code: skipping
bind_nid_postfixfor the implicit bind loses nothing for a stream socket. There it's a plain::bind(localAddressClearedis only reset for datagram sockets), so binding the wildcard directly matches what an explicitbindto it would do.A gap outside this PR's scope: libSceNet's
sceNetListencalls Winsock's::listendirectly (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 callslistenwith a backlog of 0:So with this PR, libkernel
listenbinds and listens, andsceNetListenon the same kind of socket still fails withEINVALon Windows. That doesn't block this PR, whose description scopes it tolisten_nid_postfix. Titles that listen through libSceNet without binding first would need the same change insceNetListen, as a follow-up.tools/check_conventions.py --base mainpasses.Imported from boykopovar/AnyPS5. Originally posted by Superseabas on 2026-11-08T17:10:09Z.
Review: APPROVED
Re-approving after the rebase. The lines this PR changes at
5baa8d229eare identical to49fb156488, 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.