Skip to content

fix(net): keep MSG_DONTWAIT transfers nonblocking on Windows - #8653

Merged
oneandonlydean merged 4 commits into
boykopovar:mainfrom
Ondra97:codex/fix-net-dontwait
Oct 21, 2026
Merged

oneandonlydean merged 4 commits into
boykopovar:mainfrom
Ondra97:codex/fix-net-dontwait

Conversation

@Ondra97

@Ondra97 Ondra97 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

What

On Windows, the readiness check before a MSG_DONTWAIT receive could succeed in several threads, then one reader consumed the available byte and another entered a blocking Winsock receive. The regression reproduces this for sceNetRecv, sceNetRecvfrom and sceNetRecvmsg on TCP loopback.

Native Net sockets now stay non-blocking. Transfers return immediately for MSG_DONTWAIT or guest SO_NBIO; ordinary operations retry after readiness until their existing timeout. Blocking stream sends advance through short host writes until the whole buffer is queued. A timeout or host error after progress returns the queued count; a send timeout without progress returns EAGAIN. The original deadline is shared across all partial writes. Blocking connect and accept keep their waiting behavior. Readiness waits retry at most every 10 ms so local shutdown is observed even when Winsock does not wake WSAPoll.

The existing native-socket owner carries an optional provider transfer callback. SSL uses it for Net sockets, retaining the socket's lifetime, current guest mode and timeouts without changing the lookup signature or the SSL session constructor. Kernel sockets retain their existing direct native transport.

The blocking stream completion follows FreeBSD 11.4 sosend_generic. The per-call contract follows FreeBSD recv(2); Winsock documents the socket-wide mode in ioctlsocket. This addresses a host defect identified in the PRAGMATA audit in #8005; it does not establish a game blocker or fix that whole issue.

Tested

Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 UCRT r7, Release, BUILD_TESTING=ON. AMD Ryzen 9 7900X, 32 GB RAM, NVIDIA GeForce RTX 4070 Ti SUPER; GPU unused.

Main base: 5b59fbc5539b6c6db7a26972f6069f1c6cc179ac. Tested code head: c415375041b70c7ea4c127b0b8016892903c7b91. Documentation-only follow-up: 401dee24b6dbfc4d3da865dac59c15ab9d2762af; only docs/dev/debt/libSceNet.md changed after validation. Runtime and test sources are identical.

  • The initial comparison used base 2185dbf92e8d9013a1620d2dfc95de50494e19f8 and original head f29c50766892775df90c689031975e3f432f35f4. The same eight synthetic net_dontwait_* cases have 4 failures on the base and 0 after the fix: the three competing-reader cases and the pending receive after local close fail on the base. Each reader case runs 32 races with eight readers competing for one byte. Send/sendto/sendmsg and duplex controls also check delivered bytes and unchanged guest blocking flags. A full send is permitted when Winsock accepts the entire buffer.

  • Three new net_dontwait_blocking_send, _sendto and _sendmsg tests send a patterned 4 MiB payload to a real loopback reader that drains slowly and verifies every byte. On Windows, test-only wrappers limit each real host send/sendto to 16 KiB to make short writes deterministic. Linux uses the native calls without this limit. Small Winsock buffers alone did not reliably force short writes on this host. The same tests also check a partial count at the original send deadline, EAGAIN without progress, preserved progress on local abort, and a single host result for guest SO_NBIO.

    The baseline is the original PR head f29c507668 composed with main 5b59fbc553, using the same new test source. Each API returns only 16 KiB there; all three tests fail. After the fix, each returns and delivers the full 4 MiB. These are correctness results with controlled host chunk sizes, not a throughput benchmark.

Metric Before (base) After (fix)
Returned bytes per blocking API for a 4,194,304-byte request 16,384 4,194,304
Complete payload verified at the slow reader No Yes
Failed tests among the same 3 blocking-send cases 3 0

Reproduction commands (with the supported toolchain on PATH):

cmake -S . -B ../build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DPython3_EXECUTABLE=C:/Python314/python.exe -DFFMPEG_PREBUILT_DIR=D:/Github/AnyPS5/build-runtime/externals/ffmpeg-9ac4cfd195f1
cmake --build ../build --parallel 6
ctest --test-dir ../build -R "^(net_dontwait_.*|guest_sce_net|guest_net_ctl|guest_datagram_.*|guest_refused_connect|guest_socket_host_write_.*|guest_ssl.*|guest_socket_.*|guest_udp.*|guest_poll.*|guest_select.*|patched_libraries)$" --output-on-failure

No game or original console module was run. Console ordering between competing operations remains unmeasured, and the existing Windows stream-receive timeout error mapping remains documented debt. The 10 ms readiness polling interval is also recorded in the library debt.

Checklist

@github-actions

github-actions Bot commented Oct 20, 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 5836 core/libs/prx/libSceNet/CMakeLists.txt
core/libs/prx/libSceNet/Export.cpp
docs/dev/debt/libSceNet.md
PR 6044 core/libs/prx/libSceNet/CMakeLists.txt
PR 6879 core/libs/prx/libSceNet/Export.cpp
PR 6977 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 7088 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 7201 core/libs/prx/libSceNet/Export.cpp
core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 8204 core/libs/prx/libSceNet/Export.cpp
docs/dev/debt/libSceNet.md
PR 8658 core/libs/prx/libSceNet/Export.cpp
core/libs/prx/libkernel/Socket/src/Udp.cpp

@cadenasynyster

Copy link
Copy Markdown
Contributor

Independent Windows run of this PR.

Environment: Windows 11 10.0.26300 (same build as the PR description), Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7 UCRT, Release, BUILD_TESTING=ON. I built only net_dontwait_tests and guest_poll_tests, so the #8310 workaround was not needed.

Results:

  • PR head: all 8 net_dontwait_* tests pass.
  • Baseline: I kept the PR's tests and CMake and replaced the four production files with their base versions (libSceNet/Export.cpp, libSceSsl/Export.cpp, libkernel/Socket/include/SocketPoll.hpp, libkernel/Socket/src/Udp.cpp). net_dontwait_recv, _recvfrom, _recvmsg and _blocking fail, and the other four pass. This matches the 4-failure count and the cases named in the description. I restored the PR's files afterwards.

guest_poll: the description reports an abort at GuestPoll.cpp:202, reproduced on main. On the PR head it passes here (0.37 s) on this OS build, so that abort looks specific to the author's host.

Not run: other platforms, the SSL provider tests, the full repository CTest, and any title or console run.

AI-assisted: yes

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@Ondra97 Thanks. The race is real, the per-call MSG_DONTWAIT contract is right, and keeping connect and accept blocking keeps the change contained.

One request, about blocking sends now that the native socket is always non-blocking. In native_transfer, any result other than -1/WSAEWOULDBLOCK is returned at once. A non-blocking Winsock send may accept fewer bytes than requested ("the number of bytes sent can be less than the number requested", per the send docs). So a blocking sceNetSend/sceNetSendto/sceNetSendmsg on a stream socket can now return a short count where main's blocking Winsock socket waited. FreeBSD's sosend blocks a blocking stream send until the whole buffer is queued, and returns short only on a timeout, a signal or an error. Code written for that (one send of a whole message, with the count checked only for < 0) would silently lose the tail.

Could the blocking stream path (no MSG_DONTWAIT, SO_NBIO off) keep sending the rest after readiness until it's all queued, the send timeout expires (then return what was sent, or EAGAIN if nothing was), or an error occurs? A test would show it: a blocking sceNetSend of a few MiB to a reader that drains slowly, with the full length returned. The current CheckSend uses MSG_DONTWAIT, where a short count is correct.

A smaller point, not a blocker: every thread blocked in a receive now wakes every 10 ms to notice a local close. A one-line debt note about the polling interval would help whoever looks at idle CPU later.

After that, the bar is a full CI run with the net_dontwait_* tests, guest_poll and the existing libSceNet/libSceSsl tests passing on Linux and Windows.

@Ondra97

Ondra97 commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

@oneandonlydean I fixed the blocking stream path in c415375041. It advances through short host writes with one deadline for the whole call, returns queued bytes on timeout or error, and returns EAGAIN when the send times out without progress. MSG_DONTWAIT, guest SO_NBIO and datagram sends keep their single-transfer behavior. I also added the debt note about the 10 ms readiness polling interval.

The three new send/sendto/sendmsg tests use a real TCP loopback reader and a 4 MiB patterned payload. Test-only Windows wrappers cap each actual host send at 16 KiB, since small socket buffers alone did not reliably force a short write here. The original-head/current-main composition returns only 16 KiB in all three cases; the fix returns and delivers all 4 MiB. Timeout with and without progress, local abort, and guest nonblocking mode also pass. All 11 net_dontwait_* cases pass.

The full Windows build completes with the already-declared #8310 test-only dependency, restored afterwards. The focused suite is 54/55 with no skips, including both SSL transport providers; guest_poll still fails on this host in the previously documented urgent-TCP case reproduced on main. I have kept that test enabled and documented the failure. This still needs the full Linux and Windows CI run you requested; I am not claiming that bar has passed locally.

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.

3 participants