Skip to content

Map POLLHUP to readEOF in the WSAPoll selector so remote half-closure works on Windows - #3715

Open
Budoman wants to merge 1 commit into
apple:mainfrom
Budoman:windows-half-closure-main
Open

Map POLLHUP to readEOF in the WSAPoll selector so remote half-closure works on Windows#3715
Budoman wants to merge 1 commit into
apple:mainfrom
Budoman:windows-half-closure-main

Conversation

@Budoman

@Budoman Budoman commented Aug 21, 2026

Copy link
Copy Markdown

Motivation

On Windows, allowRemoteHalfClosure is currently unimplementable: the WSAPoll selector can never produce .readEOF. WSAPoll has no POLLRDHUP equivalent, so POLLHUP is its only hangup signal and it fires for both an orderly peer FIN and an abortive RST. SelectorWSAPoll maps it to .reset, which SelectableEventLoop.handleEvent treats as terminal, so a peer's orderly FIN tears down a live channel that had deliberately half-closed its input.

Observed effect: the streamed-body failures in hummingbird-project/hummingbird#747 (tracked in #3697), and four NIOSSLIntegrationTest close-mode failures blocking Windows validation of apple/swift-nio-ssl#567. Instrumentation on a windows-2022 runner showed 8 POLLHUP deliveries of which 4 force-closed an open, active channel (socketOpen=true active=true); channels were registering interest in .readEOF (interestedRaw includes it) that the selector could never deliver. Not a duplicate of #3702: measured identical with and without the #3704 data-path fix.

Modifications

Map POLLHUP to .readEOF instead of .reset in SelectorEventSet.init(wsaPollEvents:), with a comment explaining why. This mirrors what epoll does with EPOLLRDHUP and makes recv the authority on what actually happened: 0 bytes = orderly FIN (half-closure honoured when the channel allows it); WSAECONNRESET/WSAECONNABORTED = genuine abort, closed with the real error. One functional line.

Result

Measured on windows-2022 / Swift 6.3.3:

  • swift-nio's own testHalfCloseOwnOutput goes from failing to passing; no new failures in the half-closure test runs.
  • The four close-mode NIOSSLIntegrationTest failures on Add support for Windows swift-nio-ssl#567 all pass; that PR's full suite is green with this change (plus one Windows path-separator fix in a test regex on the nio-ssl side).
  • The three timeout-dependent close-mode tests run ~5.0s, matching macOS, versus 0.018s when the channel was being torn down early.
  • macOS sanity: the same tests pass locally; non-Windows platforms are untouched (the changed line is inside the WSAPoll-only selector).

Also validated stacked on #3704 (the two changes are in different areas and compose).

One caveat worth stating: Windows half-closure has no upstream CI guard today because StreamChannelTest is skipped on Windows (NIOPosix has no PipeChannel there), which is how this could ship undetected. We patched the harness to TCP-only on a probe branch to run the half-closure tests; a durable Windows guard likely wants that or a PipeChannel implementation, which is beyond this PR.

WSAPoll has no POLLRDHUP equivalent, so POLLHUP is the only hangup signal
available and it covers both an orderly peer shutdown (FIN) and an abortive
teardown (RST). The WSAPoll selector mapped it to .reset, which
SelectableEventLoop.handleEvent treats as terminal: it tears the whole Channel
down and skips the .read/.readEOF handling entirely. A peer's FIN therefore
closed both directions of the Channel and allowRemoteHalfClosure could never be
honoured on Windows, unlike epoll and kqueue.

Report .readEOF instead, matching the event epoll synthesises from EPOLLRDHUP,
and let the following recv distinguish the two cases: 0 for an orderly FIN, which
readable0 turns into half-closure when the Channel allows it, and
WSAECONNRESET/WSAECONNABORTED for an abort, which closes the Channel with the
real error.

This remains one-shot even though WSAPoll reports POLLHUP regardless of the
requested events: readEOF0 drops .readEOF from the registration's interest and
whenReady0 intersects each event set with registration.interested, so subsequent
POLLHUP reports are masked and cannot spin the event loop.
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