Repository: apple/swift-nio
File: Sources/NIOPosix/SelectorWSAPoll.swift (Swift)

Code before:
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

#if os(Windows)
import CNIOWindows
import NIOCore
import WinSDK

extension SelectorEventSet {
    var wsaPollEventSet: Int16 {
        var result: Int16 = 0
        if self.contains(.read) || self.contains(.readEOF) {
            result |= Int16(WinSDK.POLLRDNORM)
        }
        if self.contains(.write) || self.contains(.writeEOF) {
            result |= Int16(WinSDK.POLLWRNORM)
        }
        return result
    }

    @usableFromInline
    init(wsaEvent: Int16) {
        // Event Constant	Meaning	                What to do (Typical Socket API)
        // POLLRDNORM	    Normal data readable	Use recv, WSARecv, or ReadFile
        // POLLRDBAND	    Priority data readable	Use recv, WSARecv (with MSG_OOB for out-of-band)
        // POLLWRNORM	    Normal data writable	Use send, WSASend, or WriteFile
        // POLLWRBAND	    Priority data writable	Use send (with MSG_OOB for out-of-band data)
        // POLLERR	        Error condition	        Use getsockopt with SO_ERROR; may need closesocket
        // POLLHUP	        Hang up (closed)	    Usually just cleanup: closesocket
        // POLLNVAL 	    Invalid fd (not open)	Fix your code; close and remove fd
        self.rawValue = 0
        let mapped = Int32(wsaEvent)
        if mapped & WinSDK.POLLRDNORM != 0 {
            self.formUnion(.read)
        }
        if mapped & WinSDK.POLLWRNORM != 0 {
            self.formUnion(.write)

Review comment (suggestion):
We shouldn't map `readEOF` to `POLLRDNORM`: we'll almost never unset it, even when we don't want to read. WSAPoll can't send us the necessary signal so let's never handle it.

Code after:
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

#if os(Windows)
import CNIOWindows
import NIOCore
import WinSDK

extension SelectorEventSet {
    // Use this property to create pollfd's event field. Reset and errors are (hopefully) always included.
    // According to the docs we don't need to listen for them explicitly.
    // Source: https://learn.microsoft.com/en-us/windows/win32/api/winsock2/ns-winsock2-wsapollfd
    var wsaPollEvent: Int16 {
        var result: Int16 = 0
        if self.contains(.read) {
            result |= Int16(WinSDK.POLLRDNORM)
        }
        if self.contains(.write) || self.contains(.writeEOF) {
            result |= Int16(WinSDK.POLLWRNORM)
        }
        return result
    }

    // Use this initializer to create a EventSet from the wsa pollfd's revent field
    @usableFromInline
    init(revents: Int16) {
        // Event Constant   Meaning	                What to do (Typical Socket API)
        // POLLRDNORM       Normal data readable    Use recv, WSARecv, or ReadFile
        // POLLRDBAND       Priority data readable  Use recv, WSARecv (with MSG_OOB for out-of-band)
        // POLLWRNORM       Normal data writable    Use send, WSASend, or WriteFile
        // POLLWRBAND       Priority data writable  Use send (with MSG_OOB for out-of-band data)
        // POLLERR          Error condition         Use getsockopt with SO_ERROR; may need closesocket
        // POLLHUP          Closed                  Usually just cleanup: closesocket
        // POLLNVAL         Invalid fd (not open)   Fix your code; close and remove fd
        self.rawValue = 0
        let mapped = Int32(wsaEvent)
        if mapped & WinSDK.POLLRDNORM != 0 {