Skip to content

NIOPosix: report WSAEWOULDBLOCK from Windows send/writev/recv as .wouldBlock - #3716

Open
onetamer wants to merge 1 commit into
apple:mainfrom
onetamer:windows-wouldblock
Open

NIOPosix: report WSAEWOULDBLOCK from Windows send/writev/recv as .wouldBlock#3716
onetamer wants to merge 1 commit into
apple:mainfrom
onetamer:windows-wouldblock

Conversation

@onetamer

Copy link
Copy Markdown

Motivation

On Windows, NIOBSDSocket.send, .writev (WSASend) and .recv throw an
IOError for every SOCKET_ERROR, including WSAEWOULDBLOCK. The POSIX
implementations map that to .wouldBlock(0) (via syscall(blocking: true)),
and the Windows accept / connect / sendmmsg wrappers already special-case
it — 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):

  • 25 × 100 KiB frames, one flush per frame → the peer sees EOF partway
    through (21/25 buffers on one run, 3/25 on another); 4 × 100 KiB passes.
  • Same bytes in a single flush → arrive byte-exact.
  • A raw ClientBootstrapServerBootstrap harness shows the same split:
    flush-per-write loses the stream, one flush does not.

Modifications

NIOPosix/BSDSocketAPIWindows.swift: send, writev and recv now return
.wouldBlock(0) when WSAGetLastError() is WSAEWOULDBLOCK, matching the
POSIX 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.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant