fix(libkernel): refuse a loopback TCP connect at once on Windows #5141
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5141"
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 0xRickx on 2026-10-13T12:46:37Z.
What
On FreeBSD, a TCP
connectfails withECONNREFUSEDas soon as the peer answers the SYN with a reset (POSIXconnect: "The target address was not listening for connections or refused the connection request"). Windows instead retransmits the SYN twice after a reset. On this machine a blocking connect to a loopback port with no listener takes 2.04 s, against 0.2 ms once the retransmissions are off.Homebrew that looks for a local service by connecting to
127.0.0.1waits that long on every attempt. PS5VR (PPSA99177) probes its helper service on port 17600 up to 21 times before its library screen (main.cpp,app_bridge.c). Onmainits log showsps5vr: sandbox openat 23.7 s andps5vr: dev service absentat 68.6 s: 45 s of refused connects. With this change the two lines are 2.0 s apart, which is PS5VR's own 20 × 100 ms sleep.Before a stream socket connects to a loopback address (127.0.0.0/8 or
::1), libkernel'sconnectand libSceNet'ssceNetConnectnow check whether any socket listens on the destination port (GetExtendedTcpTablewithTCP_TABLE_OWNER_PID_LISTENER, IPv4 and IPv6). When none does, they setSIO_TCP_INITIAL_RTOon the host socket withTCP_INITIAL_RTO_NO_SYN_RETRANSMISSIONS, which sets the SYN retransmissions to 0, so the reset fails the connect at once, as on FreeBSD.The check is
RefuseClosedLoopbackAtOnce_nid_no_patchin a newlibkernel/Socket/src/SocketConnect.cpp; libSceNet calls it from libkernel, and libkernel linksiphlpapion Windows.Tested
Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release,
mainff23657f(rebased onto0e6a5e7aafterwards, no conflicts):guest_refused_connect, with libkernel'sconnectand withsceNetConnect:127.0.0.1:0, reads its port, closes it, connects to that port and requiresECONNREFUSED(61 /0x8041013D) in under one second;It passes in 1.05 s. With
main's behaviour it aborts in the closed-port case (2.34 s), and without the listener check in the backlog case (0.40 s).A probe calling libkernel's
socket,listen,connectandaccept, 3 rounds per case:mainECONNREFUSEDafter 2047-2050 msECONNREFUSEDat oncePS5VR (PPSA99177, catalog 1.2.0), relinked with this
main, second run (warm shader cache):ps5vr: sandbox openat 7.11 s,ps5vr: dev service absentat 9.25 s, its home screen'slibrary: list Library -> ok, 7 itemsat 10.26 s. Onmainthe first two lines are about 45 s apart.All 27 socket tests pass:
guest_refused_connect,guest_sce_net, the fourguest_datagram_*, theguest_socket_*tests andpatched_libraries.tools/check_conventions.py --base upstream/mainpasses.Linux (Ubuntu 24.04 in WSL, GCC 13.3, Release, the CI's configure options, lavapipe), merged onto
main92e50418together with #5084:guest_refused_connect,guest_sce_net, the fourguest_datagram_*, theguest_socket_*tests andpatched_librariespass, exceptguest_socket_read_write. Full ctest: 1263/1273; the ten failures (nineagc_*tests andguest_socket_read_write) fail the same way onmain92e50418alone in this setup.git merge-tree: merges cleanly withmain, #4796, #5966, #6140 and #6142. #4910 conflicts only in files this PR does not touch. The call insceNetConnectsits before the address-family check, and libSceNet declares the helper instead of including a header, so the change stays off the lines those PRs edit.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T01:14:51Z.
Review: COMMENTED
The diagnosis is right: Winsock retries a SYN after a RST, and FreeBSD doesn't. One case this change makes worse, though: a loopback listener whose accept backlog is full.
Windows answers a SYN to a listener with a full backlog with a RST, the same as for a closed port. FreeBSD instead drops the SYN when the listen queue is full (or answers through its syncache), and the client's retransmission connects once the server has accepted the earlier connections. On
main, Windows' SYN retries happen to cover that case. WithTCP_INITIAL_RTO_NO_SYN_RETRANSMISSIONSthe connect fails withECONNREFUSEDat once. A title or homebrew that opens a burst of connections to a local server, for example several HTTP requests to an in-process service, or a server that callslistenwith a small backlog, would then see refusals that neither FreeBSD normainproduce.Ways to keep the fast refusal for closed ports without breaking busy ones:
TCP_INITIAL_RTO_PARAMETERS{Rtt = a few ms, MaxSynRetransmissions = 1}) rather than none. A closed port then fails in a few milliseconds, and a full backlog gets one more chance.connectitself after a short delay when it is refused on loopback.A test with a loopback listener of backlog 1 and two quick connects would pin the behaviour either way.
Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-14T09:15:44Z.
Windows/AMD check with PS5VR (PPSA99177 v1.2.0, catalog release): Windows 11 (26300), Radeon 860M (RDNA 3.5, driver 32.0.13058.8001), WinLibs GCC 15.2.0, Release, relinked with
--windows, same relinked executable for both builds, only the.prxfiles swapped.Time from launch until the title logs
library: list Library -> ok(right before it draws its home screen), polled from its log every 200 ms:main66f894761main66f894761+ this PR (f87099f6f)On
main, gdb attached at 25 s shows the main thread insideconnect_nid_postfix→ws2_32!connect, and a breakpoint onconnectprints 127.0.0.1:17600 every time (main thread and one more thread, 33 calls in about 37 s). A plain Pythonconnectto that port on this machine takes 2.04 s to be refused, the same figure as in the description.guest_refused_connectpasses.With the PR the title shows its home screen ("Put on your PlayStation VR2", the phone-link QR code for port 8090):
That's the only frame it presents without a headset: its main loop draws the home screen once and then only presents while a VR session is active (
main.cpp), so "FPS 0.00 (1)" in the window title is expected here.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T17:45:44Z.
Review: CHANGES_REQUESTED
Thanks, the diagnosis and the PS5VR numbers hold up. One point to resolve before merging, the one CnRJay raised and that is still open:
core/libs/prx/libkernel/Socket/include/SocketConnect.hpp:17:TCP_INITIAL_RTO_NO_SYN_RETRANSMISSIONSalso turns a loopback listener with a full accept backlog into an immediateECONNREFUSED, because Windows answers that SYN with a RST just like a closed port. FreeBSD drops the SYN (or answers through the syncache) and the client's retransmission connects once the server accepts, andmaincurrently gets the same result from Windows' retries. A burst of connections to a local server with a smalllistenbacklog would regress. Keeping one SYN retransmission with a short initial RTO (MaxSynRetransmissions = 1,Rttof a few ms) keeps the closed-port case fast and leaves the full-backlog case one more chance.core/libs/tests/GuestRefusedConnect.cpp: add a case with a loopback listener of backlog 1 and a second quick connect that must succeed once the first is accepted, so the behaviour is pinned either way, and keep the closed-port timing check.Please also report the closed-port time with whatever setting you choose (it should still be well under the 1 s limit).
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T23:14:25Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libkernel/CMakeLists.txtL83Some 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.
core/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/Socket/src/Udp.cppcore/libs/prx/libSceNet/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/Socket/src/Udp.cppcore/libs/prx/libSceNet/Export.cppImported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-15T01:53:42Z.
NVIDIA check with PS5VR (PPSA99177 v1.2.0, catalog release) as a second vendor next to the AMD run above, plus the backlog-full case from the review.
f87099f6fmerged ontomain2559e9623: clean, no conflicts (merge06a1d9dda). Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, NVIDIA GeForce RTX 4070 (driver 596.21).libspass, no new warnings.--repeat until-fail:3:guest_refused_connect,guest_sce_net, the fourguest_datagram_*and the nineteenguest_socket_*tests: 25/25, 3 of 3.guest_refused_connect_tests.exewithmain's libraries first onPATHaborts after 2.2-2.3 s (3 of 3).ctest -j8 --timeout 120 -E '^agc_driver_|^video_out_flip': 812/813. The one failure ishardware_oracle_compare(20 s limit), which also times out onmainunder-j8and passes 3 of 3 when run alone.relinker --windows --registry unused-filter=0. Both builds were seeded with the same warm shader cache, because a cold run spends about 95 s compiling beforesandbox open. Times are from launch, read from the log every 100 ms:ps5vr: sandbox openps5vr: dev service absentlibrary: list Library -> ok, 7 itemsmain2559e9623main(2nd run, machine under heavier load)main+ #5141main+ #5141Every run reaches the home screen ("Put on your PlayStation VR2") with
PS5VR | FPS: 0.00 (1), as expected without a headset. There is no stop line; the log ends withvr: headset setup failed.socket/listen/connect/poll/acceptagainstlisten(fd, 1)on127.0.0.1. One binary was built, then run withmain's or this PR'slibkernel.prxfirst onPATH. Each row has 3 rounds:mainmain+ #5141ECONNREFUSEDafter 2046-2056 msECONNREFUSEDafter 0.2-0.3 msECONNREFUSED(61) after 0.1-0.2 msSo the regression raised in review reproduces on Windows 11 26220. A SYN to a full backlog gets a RST. On
mainthe SYN retry about 500 ms later connects; withTCP_INITIAL_RTO_NO_SYN_RETRANSMISSIONSthe connect fails at once.Verdict: the PS5VR speed-up holds on NVIDIA: the library screen comes about 42 s sooner, with the gap falling from 44.9 s to 2.0 s. The backlog-full regression is real, though, so the requested change still applies (keep a retransmission or retry for loopback).
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by 0xRickx on 2026-10-15T15:16:45Z.
Thanks @CnRJay for the backlog case and @adriadam10 for the review; @screamm's probe confirmed the regression.
I changed the approach instead of keeping one retransmission: the fast refusal now applies only when nothing listens on the destination port. Before a loopback connect, libkernel reads the listening TCP sockets (
GetExtendedTcpTable, IPv4 and IPv6). If any listens on that port, the connect keeps Windows' retransmissions exactly as onmain, so a full backlog connects once the server accepts. Only a port with no listener getsTCP_INITIAL_RTO_NO_SYN_RETRANSMISSIONS; that is the case where FreeBSD also answers with a reset and the connect fails at once.One retransmission with a short initial RTO would still refuse whenever the server accepts later than that RTO. With a backlog of 1 and a server that accepts after 100 ms, a retry a few milliseconds later still finds the backlog full.
guest_refused_connectnow also listens with a backlog of 1, queues one connection that the server accepts after 100 ms, and requires a second connect to succeed, through libkernel'sconnectandsceNetConnect. The closed-port check stays.Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, head
831c5830onmain0e6a5e7a; a probe calling libkernel'ssocket,listen,connectandaccept, 3 rounds per case:mainECONNREFUSEDafter 2047-2050 msECONNREFUSEDat onceguest_refused_connectpasses in 1.05 s. It aborts in the closed-port case withmain's behaviour (2.34 s), and in the backlog case without the listener check (0.40 s).ps5vr: sandbox openat 7.11 s,ps5vr: dev service absentat 9.25 s,library: list Library -> ok, 7 itemsat 10.26 s.main92e50418together with #5084:guest_refused_connect,guest_sce_net, the fourguest_datagram_*, theguest_socket_*tests andpatched_librariespass, exceptguest_socket_read_write. Full ctest: 1263/1273; the ten failures (nineagc_*tests andguest_socket_read_write) fail the same way onmain92e50418alone in this setup.The call in
sceNetConnectnow sits before the address-family check, and libSceNet declares the helper instead of including the header, so the change merges cleanly with #5966, #6140 and #6142. The description is updated.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-15T21:06:49Z.
Re-ran my backlog probe on head
831c58300(merged ontomain44df21632, no conflicts). Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2.0 r7, Release, NVIDIA RTX 4070 (driver 596.21). The probe is the same guest code as before plussceNetConnectvariants, run againstmain's or this head's libraries, 6 samples per cell:127.0.0.1,listen(fd, 1))mainconnect/sceNetConnectECONNREFUSEDafter 2040-2057 msconnectandsceNetConnect)So @adriadam10's point is resolved: a full backlog behaves as on
main, and the previous head's refusals (2 of 8 and 24-26 of 32) are gone. The listener lookup adds about 0.2-0.6 ms to a loopback connect to a listening port (per-round minimum 0.53-0.98 ms vs 0.30-0.39 ms, 6 rounds of 50 connects each).guest_refused_connect, 26 more socket/net tests andpatched_librariespass 3 of 3.guest_sce_netaborted once in the 3x run. In 10 direct runs it aborts 3/10 with this head's libs and 2/10 withmain's under the same host load, so I don't attribute it to this PR.mainsandbox open4.9 s →dev service absent49.8 s; this head 2.6 s → 5.2 s,library: list Library -> ok, 7 itemsat 5.8 s. 1 frame each, no stop line.check_conventions.pypasses,git diff --checkis clean.Not checked: full build and non-GPU suite, IPv6
::1, Linux, the CI runner.Written with Claude Code assistance (validation by a subagent, reviewed in this session).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T18:18:19Z.
Review: APPROVED
Checked the new head against the open point: the fast refusal now applies only when
GetExtendedTcpTableshows no listener on the destination port (IPv4 and IPv6), so a full backlog keeps Windows' retransmissions;guest_refused_connectcovers both the closed port and backlog 1 throughconnectandsceNetConnect. Local build and ctest pass, and the Windows results from @screamm and @LeandroLP23 on this head cover the Windows behaviour; merging once the Windows build is green.