NIOPosix: report WSAEWOULDBLOCK from Windows send/writev/recv as .wouldBlock - #3716
Open
onetamer wants to merge 1 commit into
Open
NIOPosix: report WSAEWOULDBLOCK from Windows send/writev/recv as .wouldBlock#3716onetamer wants to merge 1 commit into
onetamer wants to merge 1 commit into
Conversation
…uldBlock On Windows, NIOBSDSocket.send, .writev (WSASend) and .recv threw an IOError for every SOCKET_ERROR, including WSAEWOULDBLOCK. The POSIX wrappers map EWOULDBLOCK to .wouldBlock(0) (syscall(blocking: true)), and the Windows accept/connect/sendmmsg wrappers already special-case it, but the three stream calls did not — so the first flush that outran a non-blocking socket's send buffer tore the channel down mid-stream. Reproduced with a loopback echo: 25 x 100 KiB WebSocket frames written with a flush per frame die after the send buffer fills (server sees EOF); a single flush of the same bytes survives. With the mapping the channel waits for writability, as on every other platform.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
On Windows,
NIOBSDSocket.send,.writev(WSASend) and.recvthrow anIOErrorfor everySOCKET_ERROR, includingWSAEWOULDBLOCK. The POSIXimplementations map that to
.wouldBlock(0)(viasyscall(blocking: true)),and the Windows
accept/connect/sendmmsgwrappers already special-caseit — these three did not.
The consequence is that the first flush that outruns a non-blocking socket's
send buffer fails the write and tears the channel down mid-stream, instead of
waiting for writability. It only shows up once a write is big enough or a
burst long enough to fill the send buffer, which is why small traffic looks
fine.
Reproduced with a loopback WebSocket echo server on Windows ARM64
(Swift 6.3.3, swift-nio 2.101.3):
through (21/25 buffers on one run, 3/25 on another); 4 × 100 KiB passes.
ClientBootstrap→ServerBootstrapharness shows the same split:flush-per-write loses the stream, one flush does not.
Modifications
NIOPosix/BSDSocketAPIWindows.swift:send,writevandrecvnow return.wouldBlock(0)whenWSAGetLastError()isWSAEWOULDBLOCK, matching thePOSIX wrappers and the other Windows wrappers in the same file.
Result
Writes that fill the send buffer suspend and resume on writability, as on
every other platform. The loopback repro above passes; a real client (a
WebSocket transport talking to a production server over TLS) goes from
"connection dies mid-burst" to clean.