Files
swift-nio/Sources
a7d4e6b50c Enable early EOF delivery for TCP channels on macOS 27 (#3783)
Enable early EOF delivery (`EVFILT_EXCEPT` / `.readEOF`) for TCP
channels on macOS 27 and later.

### Motivation:

`isEarlyEOFDeliveryWorkingOnThisOS` is hard-coded to `false` on all
Darwin platforms because of rdar://53656794, with the note "once fixed
we need to do an OS version check here". Without early EOF, a TCP
channel with `autoRead = false`, or one that is busy and not reading,
doesn't learn that its peer closed until it next reads or writes. For
servers this means work for a request whose client already disconnected
can't be cancelled promptly.

On macOS 27 the kernel behaviour is fixed **for TCP**. A minimal kqueue
probe without NIO registers the accepted side of a loopback TCP pair
with `EVFILT_EXCEPT, EV_ADD|EV_ENABLE|EV_CLEAR, fflags = NOTE_LOWAT,
data = Int.max`. It then (1) optionally sends 0, 1 or 4096 bytes and
leaves them unread, (2) asserts that no event fires within 30 ms, and
(3) closes the client and waits up to 500 ms for exactly one
`EVFILT_EXCEPT` event with `EV_EOF`. Result on macOS 27.0 (arm64), 4
repetitions per size, 12/12:

```
PEER_EXCEPTION unread=0 count=1 flags=32805 fflags=0 eof=true      (x4)
PEER_EXCEPTION unread=1 count=1 flags=32805 fflags=0 eof=true      (x4)
PEER_EXCEPTION unread=4096 count=1 flags=32805 fflags=0 eof=true   (x4)
```

However, **UNIX domain sockets are still affected.** With only the OS
version check,
`StreamChannelTest.testLotsOfWritesWhilstOtherSideNotReading` fails on
macOS 27 for the UDS pair only (its TCP pairs pass). Once the receive
buffer fills, there is a spurious `EVFILT_EXCEPT` event:

```
kevent(ident: 36, filter: -15, flags: 1, fflags: 0, data: 8192, udata: nil) - after 1 sends, we got an unexpected selector event for SocketChannel { ... localAddress = Optional([UDS]...) }
```

### Modifications:

- `isEarlyEOFDeliveryWorkingOnThisOS` returns `true` on macOS when
`#available(macOS 27, *)`. Earlier macOS releases and the other Darwin
platforms (not verified) keep `false`.
- `BaseSocketChannel.registerForReadEOF()` registers `.readEOF` on
Darwin only for IPv4/IPv6 channels. UNIX domain sockets and pipes keep
the previous behaviour. Linux and other platforms are unchanged.

No new tests. Four existing tests guarded by
`isEarlyEOFDeliveryWorkingOnThisOS` returned early on Darwin so far and
now run on macOS 27. `testLotsOfWritesWhilstOtherSideNotReading` guards
the UDS case:

-
`SocketChannelTest.testWeAreInterestedInReadEOFWhenChannelIsConnectedOnTheServerSide`
-
`SocketChannelTest.testWeAreInterestedInReadEOFWhenChannelIsConnectedOnTheClientSide`
- `ChannelTests.testCloseInSameReadThatEOFGetsDelivered`
- `ChannelTests.testEOFReceivedWithoutReadRequests`

### Result:

On macOS 27+, TCP channels get early `readEOF` notifications as on
Linux.

Tested on macOS 27.0 (arm64), Xcode 27.2, Swift 6.4.2
(`swiftlang-6.4.2.1.9`), on top of `main` b440f7b:

| Run | Result |
|---|---|
| OS check only (first iteration) | `NIOPosixTests`: 762 tests, 10
failures, all in `testLotsOfWritesWhilstOtherSideNotReading` (UDS) |
| This PR, `swift test --filter NIOPosixTests` | 762 tests, 37 skipped,
**0 failures**; the four tests above and the UDS test pass |
| This PR, full `swift test` | 16 test bundles, 2,462 tests, **0
failures** |
| `swift-format lint` (repo config) | clean |

Questions for reviewers:
- Does gating on the channel's address family suit you, or would you
prefer passing the family into the selector registration?
- Can Apple confirm rdar://53656794 as fixed for TCP in macOS 27 (and
whether the UDS case is tracked separately)? The gate could also cover
the other Darwin 27 platforms once verified.
- Would a macOS 27 CI job be welcome, so these tests keep running on
Darwin?

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2026-11-23 16:30:29 +00:00
..
2026-10-12 13:25:18 +01:00
2024-09-12 13:18:25 +01:00
2026-07-07 15:46:28 +01:00
2025-05-07 12:24:44 +00:00
2025-05-07 12:24:44 +00:00