1300 Commits
Author SHA1 Message Date
Honza Dvorsky 93bd8f2e02 Test HTTP server - avoid writing .end to the wrong channel for responses using content-length (#3792)
### Motivation:

One of my project uses `NIOHTTP1TestServer` for testing and for
responses that contain the `content-length` header + `connection:
close`,
the test was flaky - failing intermittently, because the client closed
the connection before the test managed to submit `.end` to the test
server.

The cause was a race condition between the test and the client's and
server's threads hosting their respective event loops.

### Modifications:

Now `NIOHTTP1TestServer` captures the channel that sent the `.head`, and
even once `currentClientChannel` changes (but before the next `.head` is
sent),
when a call to send `.end` comes in, uses the channel associated with
the previous `.head`, rather than reading `currentClientChannel`.

This test is skipped on Darwin as it requires the platform to detect
early EOF.

### Result:

More reliable tests that involve two requests that come in at once (in a
deterministic order), when the second one is parked and ready right
after the first one finishes.
2026-11-18 08:44:43 +01:00
Mohammed HassanandCory Benfield 02d6d9ada2 Create temporary directories with owner-only permissions (#3771)
Create temporary directories with owner-only permissions.

### Motivation:

`FileSystem.createTemporaryDirectory(template:)` (and
`withTemporaryDirectory`, which uses it) creates the directory with
`FilePermissions.defaultsForDirectory`, i.e. 0755. When the template
points into a shared directory such as `/tmp`, other local users can
list the temporary directory and read anything written into it with the
default 0644 file permissions. `mkdtemp(3)`, Go's `os.MkdirTemp` and
Python's `tempfile.mkdtemp` all use 0700 for this reason.

Repro with a umask of 022:

```swift
let dir = try await FileSystem.shared.createTemporaryDirectory(template: "/tmp/nio-repro-XXXXXX")
try await FileSystem.shared.withFileHandle(forWritingAt: "\(dir)/secret", options: .newFile(replaceExisting: false)) { _ in }
// before: dir is drwxr-xr-x, secret is -rw-r--r--, readable by every local user
// after:  dir is drwx------
```

### Modifications:

- Pass `.ownerReadWriteExecute` when `_createTemporaryDirectory` creates
the directory, in both `NIOFS` and `_NIOFileSystem`. Intermediary
directories created from the template get the same permissions, since
they are created by the same `_createDirectory` call.
- Document the permissions on `createTemporaryDirectory(template:)`.
- Add `testCreateTemporaryDirectoryIsOnlyAccessibleByOwner`.

### Result:

Temporary directories are only accessible by their owner.

I verified the before/after modes for both modules with a small
executable against this branch on macOS (`swift build` is clean). I
couldn't run the XCTest target locally because this machine only has the
Command Line Tools.

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2026-11-03 17:44:08 +00:00
Aryan Shah 2f58d8ef84 Revert configuration type for HTTP/1 server pipeline setup (#3775)
### Motivation:

We introduced a new configuration type in #3746 to make further
extensions to the server pipeline (#3745 for example) easier to
introduce.

The plan was to merge #3745 soon after #3746 was merged, because #3745
added a breaking change to the new config type.

However, #3745 needs some more consideration. We should therefore revert
the change in #3746 to unblock new releases from being made until we
come up with a better solution.
 
### Modifications:

A clean revert of PR #3746.

### Result:

Unblocks new releases.
2026-11-02 13:00:48 +00:00
Aryan Shah 108daca35d Add configuration type for HTTP/1 server pipeline setup (#3746)
### Motivation:

`NIOHTTP1.HTTPPipelineSetup` contains helper methods which add HTTP/1
client/server handlers to the provided channel pipeline.

Over time, we have had to duplicate these methods when introducing a new
configuration option in order to not break API.

As a result, there is now lots of duplication in
`NIOHTTP1.HTTPPipelineSetup` and introducing more configuration options
is very cumbersome. We should instead add a configuration type to solve
this problem.

### Modifications:

- Added new configuration types named
`NIOHTTPServerPipelineConfiguration`, and
`NIOHTTPServerPipelineSynchronousConfiguration`.
- Added a new pipeline setup helper method on `ChannelPipeline` and
`ChannelPipeline.SynchronousOperations` that requires the configuration
type as an argument.
- Deprecated all existing helper methods on `ChannelPipeline` and
`ChannelPipeline.SynchronousOperations`.

### Result:

Better API.
2026-10-19 13:23:19 +01:00
Nikhil GuptaandRaphael Hiesgen eabcc14f9d Don't trap when QUIC reservedCapacity isn't a valid integer length (#3747)
## Motivation

`QUICBinaryEncodingStrategy.writeInteger(_:reservedCapacity:to:)`
switches on `max(reservedCapacity, bytesNeededForInteger(integer))` and
handles only 1, 2, 4 and 8, falling through to
`fatalError("Unreachable")` otherwise.

`bytesNeededForInteger` only ever returns those four values, but
`reservedCapacity` is guarded solely against being greater than 8. A
reserved capacity of 3, 5, 6 or 7 therefore reaches the unreachable case
and traps:

```swift
var buffer = ByteBuffer()
let strategy = ByteBuffer.QUICBinaryEncodingStrategy.quic
strategy.writeInteger(1, reservedCapacity: 3, to: &buffer)
// NIOCore/ByteBuffer-quicBinaryEncodingStrategy.swift:126: Fatal error: Unreachable
```

This is reachable without calling that method directly.
`requiredBytesHint` is a public `var Int` with no validation — the
initialiser takes the 1/2/4/8 `IntegerLength` enum, but the property can
be assigned any value afterwards, and
`writeLengthPrefixed(strategy:writeData:)` passes it straight through as
the reserved capacity:

```swift
var strategy = ByteBuffer.QUICBinaryEncodingStrategy(requiredBytesHint: .one)
strategy.requiredBytesHint = 3          // public var, accepted silently
var buffer = ByteBuffer()
buffer.writeLengthPrefixed(strategy: strategy) { $0.writeString("hi") }
// Fatal error: Unreachable
```

## Modifications

Round the length up to the next valid QUIC integer length rather than
trapping, which is what the surrounding comment already intends by "use
more space than necessary in order to fill the reserved space":

```diff
-            case 4:
+            case 3, 4:
-            case 8:
+            case 5, 6, 7, 8:
```

RFC 9000 § 16 permits encoding a value in more bytes than it strictly
requires, and `writeLengthPrefixed` already shifts the payload when the
integer turns out longer than was reserved (`case reservedCapacity...`),
so writing 4 bytes for a reservation of 3 is handled by existing code.

## Result

No trap for any `reservedCapacity` in `0...8`, and the encoded value
still round-trips.

I checked every combination of `reservedCapacity` in `0...8` against the
boundary values `0, 1, 63, 64, 16383, 16384, 1073741823, 1073741824,
4611686018427387903`: all 81 encode, decode back to the original value,
and leave no bytes behind. On `main` the same check traps.

## Testing

The existing `testRoundtripWithReservedCapacity` iterates over `[0, 1,
2, 4, 8]` — skipping exactly the values that trap, which is presumably
why this went unnoticed. I've added a companion
`testRoundtripWithNonPowerOfTwoReservedCapacity` covering 3, 5, 6 and 7,
asserting both the encoded length and that the value round-trips. It
fails on `main` and passes with this change.

Full test suite: **2455 tests, 0 failures.**

## A note on the alternative

Rounding *down* to the largest valid length that still fits the
reservation would also avoid the trap and waste fewer bytes — for
`reservedCapacity: 3` that would write 2 bytes rather than 4. I went
with rounding up because the existing comment says to use more space
rather than less, and because it keeps the guarantee that at least the
reserved number of bytes is written. Happy to switch if you'd prefer the
other behaviour.

If you would rather fix this at the source by validating
`requiredBytesHint` or changing its type to `IntegerLength`, that would
be source-breaking, so I left it alone — but the public setter is what
makes this reachable in ordinary use.

---------

Co-authored-by: Raphael Hiesgen <r_hiesgen@apple.com>
2026-10-16 16:54:15 +01:00
hamzahrmalik fcc5b9c153 Allow sending values into EventLoopFuture (#3643)
### Motivation:

Users may want to move a non-Sendable value into an EventLoopFuture.
This is safe if we take the value as `sending`

### Modifications:

Change functions whcih currently require Sendable parameters to instead
take the parameter as `sending` where possible. This seems to not break
API

Some functions take closures whcih return Sendable values, those ones
now take closures which return sending values. This has been done as an
overload, because a straight replacement seems to break API
2026-10-15 13:20:39 +02:00
Raphael Hiesgen 6db9d9abd9 Add socket options related to fragmentation (#3757)
### Motivation

SwiftNIO provides convenience functions for a range of socket options.
This PR adds convenience options for fragmentation-related socket
options on Darwin and Linux. One particular use case is disabling
fragmentation when using QUIC, which disallows IP fragmentation of its
datagrams.

### Modifications

* Add `NIOBSDSocket.Option.ip_dontfrag` (Darwin).
* Add `NIOBSDSocket.Option.ipv6_dontfrag` (Darwin and Linux).
* Add `NIOBSDSocket.Option.ip_mtu_discover` and `.ipv6_mtu_discover`
(Linux). They set the path MTU discovery mode, which influences
fragmentation.

### Result

Adopters can easily configure IP fragmentation.
2026-10-05 09:05:01 +01:00
Fabian FettandCory Benfield f5b34b27cb Allow non-copyable ByteToMessageDecoders and typed throws (#3731)
### Motivation

Decoders are state machines and state machines are a natural fit for
non-copyable types. Today `ByteToMessageDecoder` and
`NIOSingleStepByteToMessageDecoder` require their conformers to be
copyable.

While we are here we also want `NIOSingleStepByteToMessageDecoder` to be
able to declare a concrete error type, so that callers can handle decode
errors without going through an existential.

### Changes
  
- Make `ByteToMessageDecoder` and `NIOSingleStepByteToMessageDecoder`
`~Copyable`.
- Add an `associatedtype DecodeError: Error = any Error` to
`NIOSingleStepByteToMessageDecoder` and spell decode and decodeLast as
`throws(DecodeError)`. Decoders that don't declare it keep behaving
exactly as before.
- Allow `~Copyable` `ByteToMessageDecoder` in `ByteToMessageHandler`.
- Since `~Copyable` types cannot be type erased into an existential
before macOS 15, we have to work around the existing `CanDequeueWrites`
dance: instead of casting `self` to `CanDequeueWrites` in
`handlerAdded`, ByteToMessageHandler gains an optional
`deliverQueuedWrite` closure. It is formed in the constrained
`ChannelOutboundHandler.write`, the only place where `Decoder`'s
`WriteObservingByteToMessageDecoder` conformance is statically known,
the first time a write arrives re-entrantly.
This has to be a closure rather than a protocol requirement on
`ByteToMessageDecoder`. `Decoders` may conform to
`WriteObservingByteToMessageDecoder` conditionally while conforming to
`ByteToMessageDecoder` unconditionally — `HTTPDecoder` does exactly this
— and all generic instantiations share one `ByteToMessageDecoder`
witness table, so such a requirement could only ever be witnessed by a
non-observing default and the write would be silently dropped.
- Split the delivery out of `tryDecodeWrites` into an `@inline(never)
dequeueWrites()`. `decodeLoop` calls `tryDecodeWrites` on every
iteration, so the empty-queue check has to stay cheap; keeping the
delivery inline tripled that function's size (50 → 141 instructions) and
showed up directly on the normal decode path.
- `ByteToMessageDecoderVerifier` allows `~Copyable`
`ByteToMessageDecoder`s.
- Added tests that use the newly lifted `~Copyable` restriction, plus
regression coverage for a decoder whose write-observing conformance is
conditional (the HTTPDecoder shape) and for constructing the handler in
a generic context.

### Result

Decoders may be non-copyable and may declare a concrete error type.
  
The normal decode path does not change performance. The re-entrant write
path costs one extra allocation per handler, but is meaningfully cheaper
in instructions. I'm happy to provide the benchmarks used to generate
those numbers in a separate pr – leaving them out here, as we would end
up with as many benchmarks as we have unit tests sooner or later.
  
Instructions, macOS arm64, per 1000 iterations, min of 5 interleaved
rounds:
  
```
  ┌──────────────────────────────┬────────────┬────────────┬───────────────────────────────────┐
  │                              │    main    │  this PR   │                                   │
  ├──────────────────────────────┼────────────┼────────────┼───────────────────────────────────┤
  │ decode, no write observation │ 9,404,415  │ 9,360,896  │ −0.46% (noise ~0.8%, i.e. parity) │
  ├──────────────────────────────┼────────────┼────────────┼───────────────────────────────────┤
  │ decode + re-entrant write    │ 13,656,063 │ 12,541,951 │ −8.16%                            │
  ├──────────────────────────────┼────────────┼────────────┼───────────────────────────────────┤
  │ handler add / remove         │ 85,983,231 │ 83,361,791 │ −3.05% (noise ~2.2%)              │
  └──────────────────────────────┴────────────┴────────────┴───────────────────────────────────┘
```

Replacing the CanDequeueWrites existential with the closure is what
improves the reentrant write path: delivering a queued write is now a
direct closure call rather than a witness-table dispatch, and
handlerAdded no longer performs a runtime conditional-conformance
lookup.
The saving grows with the number of writes queued per decode — around
−16% with 8 queued writes, though that benchmark is noisier.

Allocations (nio-alloc-counter-tests, Swift 6.2):
  
```
  ┌────────────────────────────┬─────────┬─────────┬────────┐
  │                            │  main   │ this PR │        │
  ├────────────────────────────┼─────────┼─────────┼────────┤
  │ 1000_reqs_1_conn           │ 27,341  │ 27,342  │ +1     │
  ├────────────────────────────┼─────────┼─────────┼────────┤
  │ 1_reqs_1000_conn           │ 368,000 │ 369,000 │ +1,000 │
  ├────────────────────────────┼─────────┼─────────┼────────┤
  │ ping_pong_1000_reqs_1_conn │ 296     │ 296     │ —      │
  ├────────────────────────────┼─────────┼─────────┼────────┤
  │ decode_1000_ws_frames      │ 2,000   │ 2,000   │ —      │
  └────────────────────────────┴─────────┴─────────┴────────┘
```

Forming the closure captures Decoder.OutboundIn's metadata, so it
allocates once per handler that actually queues a re-entrant write —
1_reqs_1000_conn builds 1000 such handlers, hence +1000. main's
existential cast did not allocate, so this is a new cost rather than
staying flat. The 1_reqs_1000_conn threshold is raised by 1000 for
6.1/6.2/6.3; the nightly toolchains are unchanged (10,000 on both — they
specialize the closure away entirely).

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2026-09-17 13:44:37 +01:00
Johannes Weiss 21de5f08c1 ELF.getAbandoningOnCancel() (#3722)
### Motivation:

`EventLoopFuture.get()` violates Structured Concurrency in two ways:
1. It doesn't automatically propagate cancellation
2. It doesn't return control back on cancellation

(1) is unfixable without user help but (2) we can address.

### Modifications:

- Add `EventLoopFuture.getAbandoningOnCancel()`

### Result:

- Users now have a way to at least get back control.
2026-09-17 11:51:12 +01:00
Victor DebrayandGeorge Barnett c1ccdbcc95 Add channel options for a vsock socket's local and remote addresses (#3689)
Add channel options to read a vsock socket's local and remote addresses

### Motivation:

NIOPosix supports vsock via `VsockAddress`, `ServerBootstrap.bind(to:)`
and `ClientBootstrap.connect(to:)`, but `Channel.localAddress` and
`Channel.remoteAddress` are always `nil` on vsock channels because
`NIOCore.SocketAddress` has no vsock representation.

Two things are unreachable as a result:

- A server accepting a vsock connection cannot tell which context
connected. The peer's context ID is the only identity a vsock connection
carries, and it is supplied by the kernel rather than the peer, which
makes it a natural basis for authorization.
- A listener bound to `VsockAddress/Port/any` cannot learn the port the
kernel assigned, so callers are forced to hard-code ports and race for
them.

`ChannelOptions.localVsockContextID` already exists for the first half
of this problem, but it reports only the context ID, not the port.

Adding a `.vsock` case to `SocketAddress` would fix both, but it is
source-breaking for every exhaustive switch over that enum, in NIO and
downstream.

### Modifications:

- Add `ChannelOptions.Types.LocalVsockAddress` and `RemoteVsockAddress`,
both get-only with `Value == VsockAddress`, alongside the existing
`LocalVsockContextID`.
- Add `BaseSocket.getLocalVsockAddress()` and `getRemoteVsockAddress()`,
sharing a helper that reads into a `sockaddr_storage` via
`withMutableSockAddr` and `convert()`, mirroring `BaseSocket.get_addr`.
On a non-vsock socket `getsockname`/`getpeername` fill a different
`sockaddr`, and reinterpreting those bytes would yield a context ID
derived from unrelated data such as a peer's IP address and port, so the
helper checks the address family and throws
`SocketAddressError.unsupported` on a mismatch.
- Service both options in `SocketChannel.getOption0`, and
`LocalVsockAddress` in `ServerSocketChannel.getOption0`.
`RemoteVsockAddress` is not serviced on `ServerSocketChannel`: a
listener has no peer.
- Tests: both helpers reject non-vsock sockets; over vsock loopback the
client's peer port matches the port the listener bound and the accepted
channel reports the same CID the client saw; a `Port/any` listener
reports a concrete port and a CID of `.any`, matching the channel
option. The loopback tests are gated on `System.supportsVsockLoopback`
in the same way as `testGetLocalCID` and `testEchoVsock`, so they run in
the `vsock-tests` CI job and skip elsewhere.

This is purely additive: two new option types, four new static
accessors, and new arms in two internal switches. No existing
declaration changes.

### Result:

A vsock server can identify the connecting context, and a wildcard-bound
listener can report its port:

```swift
let peer = try await channel.getOption(.remoteVsockAddress).get()
peer.cid  // the connecting context's CID

let local = try await listener.getOption(.localVsockAddress).get()
local.port  // the port a `Port/any` bind was given
```

Reading either option on a channel that isn't backed by a vsock socket
throws `SocketAddressError.unsupported`.

Co-authored-by: George Barnett <gbarnett@apple.com>
2026-09-15 07:01:23 +00:00
Si Beaumont 03f13fb86c Add socket option API for SO_REUSEPORT (#3741)
### Motivation:

`NIOBSDSocket.Option` provides shorthand API for socket options, e.g.
`.so_reuseaddr` for `SO_REUSEADDR`. We don't have API for
`SO_REUSEPORT`.

`SO_REUSEPORT` is used to bind more than one socket to the same address
and port to spread accepts or datagrams over multiple sockets and
support hot restarts.

Users can already use it today, but must use `init(rawValue:)`, and
import the platform C lib, etc:

```swift
.channelOption(.socketOption(.init(rawValue: SO_REUSEPORT)), value: 1)
```

There's no reason not to provide a convenience for this option, like we
do for many others.

### Modifications:

This PR adds `NIOBSDSocket.Option.so_reuseport`, wrapping
`SO_REUSEPORT`, alongside the other options in `BSDSocketAPI.swift`.

It's not provided on Windows or WASI, which don't have `SO_REUSEPORT`,
but there was already precedent for other options like this, e.g.
`SO_TIMESTAMP`.

### Result:

Users can pass `.socketOption(.so_reuseport)` like other supported
socket options:

```swift
DatagramBootstrap(group: group)
    .channelOption(.socketOption(.so_reuseport), value: 1)
    .bind(host: "127.0.0.1", port: 12345)
```
2026-09-14 15:19:06 +00:00
Fabian Fett ae1b32d0a9 Fix new NoUseUnstructuredThrowingTask warnings (#3733)
### Motivation

The new `NoUseUnstructuredThrowingTask` diagnostic on nightly-main flags
every `Task { try await ... }`
whose result is dropped. We have a few of those.

### Changes

- Use `async let _` instead of a fire-and-forget `Task` in
`NIOAsyncWriterTests` and
`NIOThrowingAsyncSequenceTests`.
- `NIOPerformanceTester`: rethrow the error from the `Task` in
`measureAndPrint` instead of
swallowing it.
- Name the `swift-nio` dependency explicitly in
`Benchmarks/Package.swift`; SwiftPM otherwise infers
it from the directory name, which breaks in git worktrees.

### Result

No warnings. Suspended yields in the tests are cancelled when the test
returns instead of leaking
past it, and async benchmarks fail loudly.
2026-09-04 09:28:30 +01:00
Fabian Fett 082e059996 Translate ByteToMessageDecoderVerifierTest to Swift Testing (#3730)
### Motivation

We want to extend `ByteToMessageDecoderVerifierTest` with tests that
verify non-copyable decoders. Instead of adding more XCTests, lets first
move all existing tests to swift-testing.

### Changes

- Mechanical translation of `ByteToMessageDecoderVerifierTest` from
XCTest to Swift Testing.

### Result

No behaviour change. We can write the new tests in Swift Testing.
2026-09-03 14:49:22 +00:00
Fabian Fett c306e9f8a8 Translate SingleStepByteToMessageDecoderTest to Swift Testing (#3729)
### Motivation

We want to extend `SingleStepByteToMessageDecoderTest` with tests for
non-copyable decoders and typed throws. Instead of adding more XCTests,
lets first move all existing tests to swift-testing.

### Changes

- Mechanical translation of `SingleStepByteToMessageDecoderTest` from
XCTest to Swift Testing.

### Result

No behaviour change. We can write the new tests in Swift Testing.
2026-09-03 14:22:36 +00:00
Fabian Fett e8e0774c1a Fix warnings: private setters cannot be referenced from an '@inlinable' function (#3719)
Fix the following warnings:

> setter for property 'xxx' is private and cannot be referenced from an
'@inlinable' function; this will be an error in a future Swift language
mode
2026-09-02 22:45:28 +00:00
George Barnett 7800d84cba Add API to get the result of an isolated ELF, if available (#3721)
Motivation:

A number of NIO APIs are asynchronous and return futures but have
synchronous implementations. If a caller wants to operate on this they
need to register a callback with the future and then operate on the
result. However, now that we have an isolated view on the event loop
future we can provide a fast path: just check whether the result is
already present.

This is particularly helpful for channel initializers which are
typically synchronous. Using the fast path means calls can avoid an
escaping closure from registering a callback on the future.

Modifications:

Add three new APIs to the isolated event loop future:
- `result` which returns the underlying `Result<Value, Error>` or `nil`
if it hasn't been completed yet
- `value` which call `try result.get()`, and
- `isComplete`

Result:

APIs for accessing the result of an ELF from its isolated view if it's
already completed
2026-08-26 09:05:23 +01:00
Jake Petroules 08b497c937 [Windows] Stop skipping the event loop tick metrics tests (#3687)
### Motivation:

`EventLoopMetricsDelegateTests` is skipped in its entirety on Windows,
on the grounds that the WSAPoll selector wakes far more often than epoll
or kqueue and so the tick counts are unreliable and flaky.

Measuring it does not bear that out.

### Modifications:

Remove the skip. No source changes are needed.

Evidence gathered on a Windows ARM64 VM (Swift 6.3.2):

- `testMetricsDelegateTickInfo` observes `ticks=2`, `numberOfTasks` per
tick `[2, 1]`. That is **identical** to what the same instrumentation
reports on macOS, and it is the low end of the `2...5` range the test
allows, so there are three spare ticks of headroom rather than a count
pressed up against the limit.
- Both tests pass in isolation across four consecutive runs, and in
three separate full unfiltered runs of the entire test suite (2354
tests, 0 failures each).

For completeness, while investigating I did find one real asymmetry,
which turns out not to affect these tests: `whenReady0` converts its
timeout with `Int32(clamping: timeAmount.nanoseconds / 1_000_000)`, so a
sub-millisecond timeout truncates to `0` where epoll arms a
nanosecond-precision `timerfd` and kqueue uses a `timespec`. Probing it
showed the cost is at most one extra poll (a 500µs sleep scheduled from
inside an idle loop produced 1 extra tick, not a spin), and these tests
use 100ms and 1s delays so they never encounter it. Worth knowing about,
but not a correctness problem and out of scope here.

### Result:

Two more tests run on Windows.
2026-08-11 14:16:16 +00:00
Jake Petroules 0be2d1a0af [Windows] Fix buffer sizing in System.enumerateDevices() (#3684)
### Motivation:

`System.enumerateDevices()` crashes on Windows, which in turn makes
`UtilitiesTest.testEnumeratingDevices` and the whole of
`SocketOptionProviderTest` (whose `setUp` enumerates devices) unrunnable
there; both are currently skipped.

`GetAdaptersAddresses` reports the size its output requires in *bytes*,
but the buffer was allocated as `Int(ulSize) /
MemoryLayout<IP_ADAPTER_ADDRESSES>.stride` *elements*. That integer
division truncates, so the buffer was always smaller than what the OS
asked for, and the OS then wrote its linked list of variably sized
records (adapter names, sockaddrs, unicast address entries) past the end
of it, corrupting the heap.

### Modifications:

- Size the allocation in bytes, starting from the 15KB initial
allocation the
[documentation](https://learn.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getadaptersaddresses)
recommends, and retry on `ERROR_BUFFER_OVERFLOW` — the set of adapters
can change between the sizing call and the filling call.
- Remove the corresponding skips in
`UtilitiesTest.testEnumeratingDevices` and `SocketOptionProviderTest`.
- Running `SocketOptionProviderTest` for the first time on Windows
showed that `testPassingInvalidSizeToSetComplexSocketOptionFails` needs
a Windows-specific expectation. Winsock's `SO_RCVTIMEO` takes a `DWORD`
rather than a `timeval`, so the `Int` the test passes is in fact large
enough and `setsockopt` correctly accepts it. On Windows the test now
passes a single byte, which is too small for any option, and asserts the
`WSAEFAULT` that Winsock reports.

No behaviour change on any other platform; everything is behind `#if
os(Windows)`.

### Result:

`System.enumerateDevices()` works on Windows, and 13 previously skipped
tests run there.

Verified on a Windows ARM64 VM (Swift 6.3.2): a full unfiltered `swift
test` passes — 2354 tests, 0 failures.
2026-08-11 10:43:35 +00:00
Jake PetroulesandFabian Fett 18670a7e97 [Windows] Deregister channels while the event loop is closing (#3685)
### Motivation:

The WSAPoll selector traps with `preconditionFailure("Invalid fd
supplied.")` whenever `WSAPoll` reports `POLLNVAL`, and that is
reachable from ordinary shutdown, so
`MultiThreadedEventLoopGroupTests.testShuttingDownFailsRegistration` is
currently skipped on Windows.

The root cause is not in the selector. `SelectableEventLoop.deregister`
guards on `isOpen`, which is false both when the loop is closed *and*
when it has merely stopped accepting **new** registrations. The latter
is precisely the state the loop is in during `Selector.closeGently`,
which closes each remaining channel — so those channels skip
deregistration entirely and then close their sockets anyway.

The epoll and kqueue backends tolerate that, because there a
registration lives in the kernel and is dropped along with the
descriptor. The WSAPoll backend keeps its registrations in an array of
its own, so it is left holding a stale entry for a closed socket, and
the next `WSAPoll` correctly reports `POLLNVAL` for it.

Traced on a Windows ARM64 VM by instrumenting the selector and the event
loop:

```
register0 fd=704 idx=1
EL.deregister SKIPPED state=runningButNotAcceptingNewRegistrations
EL.deregister SKIPPED state=runningButNotAcceptingNewRegistrations
POLLNVAL fd=704 i=1 revents=4 events=256 dereg=[] indexes=[704: 1, 712: 2, 728: 3]
Fatal error: Invalid fd supplied.
```

### Modifications:

- Guard `deregister` on the *selector's* lifecycle rather than on
`isOpen`. Deregistering an existing channel while the loop is closing is
valid, because the selector itself is still open at that point.
- Remove the skip on
`MultiThreadedEventLoopGroupTests.testShuttingDownFailsRegistration`.

`POLLNVAL` deliberately keeps trapping. Like `EBADF`, which NIO already
treats as an unacceptable errno, it means we have lost track of a
descriptor; Windows recycles `SOCKET` handles, so continuing against a
possibly-reused handle would misroute I/O to the wrong channel. An
earlier version of this patch mapped `POLLNVAL` to `.error` instead —
that silently retained the stale entry and gave up that guarantee, so it
was dropped in favour of fixing the cause.

**Note for reviewers:** unlike most of the Windows porting patches this
touches shared code rather than sitting behind `#if os(Windows)`. The
observable behaviour on Linux/Darwin is unchanged, but they will now
issue one `epoll_ctl(EPOLL_CTL_DEL)`/kevent delete per channel during
event loop shutdown where previously they issued none.

### Result:

The WSAPoll selector no longer traps during event loop shutdown, and
`testShuttingDownFailsRegistration` runs on Windows.

Verified:
- Windows ARM64 (Swift 6.3.2), full unfiltered `swift test`: 2354 tests,
0 failures.
- macOS, full `swift test` with and without this change: identical
results (2451 tests, same 28 pre-existing failures caused by the network
mount used for this port, none in NIOPosix).

Co-authored-by: Fabian Fett <fabianfett@apple.com>
2026-08-07 14:41:31 +00:00
Aditya Singh 8f5cd5fa9c NIOFileSystem: saturate ByteCount unit factories instead of trapping on overflow (#3611)
Saturate the `ByteCount` unit factories so large counts no longer trap
on overflow.

### Motivation:

`ByteCount` exposes unit factories such as `kilobytes`, `megabytes`,
`gigabytes`, `kibibytes`, `mebibytes`, and `gibibytes`. Each one
computed the byte total with a plain multiplication, for example `1024 *
1024 * 1024 * count` in `gibibytes`. When a caller passes a very large
count, that multiplication overflows `Int64` and traps, crashing the
process. This is exactly what issue #2877 reports:
`ByteCount(contentsOf:maximumSizeAllowed: .gibibytes(.max))` crashes
with an arithmetic overflow inside `gibibytes`, even though passing a
huge value is a natural way to express "effectively no limit".

### Modifications:

I added a small `fileprivate` saturating multiplication helper on
`Int64` and routed every unit factory through it. The helper uses
`multipliedReportingOverflow(by:)` and, on overflow, clamps to
`Int64.max` for positive counts or `Int64.min` for negative counts.
Counts that fit continue to produce the exact same value as before, so
there is no behavior change for normal inputs. I extended
`ByteCountTests` with positive and negative overflow cases, and applied
the identical change to both copies of `ByteCount.swift` (`NIOFS` and
`_NIOFileSystem`) to keep them in sync, matching the convention used by
recent changes to those targets.

### Result:

Passing extreme values such as `.gibibytes(.max)` saturates to
`Int64.max` instead of trapping, so disabling a size limit via a large
value no longer crashes. I verified this by running `swift test --filter
ByteCountTests`, which passes after the change. To confirm the tests are
load bearing I reverted the source fix and re-ran the new tests, which
crashed with signal 5 (the overflow trap) exactly as the issue
describes, and they pass again with the fix in place. `swift format lint
--strict` reports no issues on the changed files.

Fixes #2877
2026-08-06 08:11:32 +00:00
Jake Petroules 72973283d7 [Windows] Report the WSAPoll selector's descriptor in its description (#3686)
### Motivation:

`Selector.description` prints `self.selectorFD`, which only the epoll,
kqueue and io_uring backends ever assign. On Windows it therefore always
reads `descriptor = -1` — the value that by convention means the
selector has been closed — even while the selector is open and perfectly
usable.

That made
`MultiThreadedEventLoopGroupTests.testStructuredConcurrencyMTELGStartStop`
unrunnable on Windows, since it asserts that a running event loop does
*not* describe itself as having descriptor -1, and that a shut down one
does.

### Modifications:

The WSAPoll backend has no single descriptor standing for the selector
itself, but the read end of its wakeup socket pair has exactly the same
lifetime: it is created in `initialiseState0` and reset to
`NIOBSDSocket.invalidHandle` in `close0`, under the same
`externalSelectorFDLock` that this description already takes. Report
that on Windows, mapping the invalid handle to -1 so the established
meaning of -1 is preserved.

Remove the corresponding skip.

The change is behind `#if os(Windows)`; other platforms keep printing
`selectorFD` exactly as before.

### Result:

A running event loop on Windows now describes its selector accurately,
and `testStructuredConcurrencyMTELGStartStop` runs there.

Verified on a Windows ARM64 VM (Swift 6.3.2): a full unfiltered `swift
test` passes — 2354 tests, 0 failures.
2026-07-31 10:03:16 +00:00
Jake PetroulesandGeorge Barnett b07752c676 [Windows] Skip unsupported/flaky tests and run the Windows unit tests in CI (#3680)
### Motivation:

All of the test targets now compile on Windows (#3663, #3665), but
`swift test` doesn't yet pass: a number of tests exercise functionality
that isn't supported or isn't yet functional on Windows, some crash or
hang, and a few are flaky on the WSAPoll-based selector. Before the
Windows unit tests can run in CI, those tests need to be skipped (with
reasons) so a full `swift test` exits cleanly.

### Modifications:

Skip the tests that don't pass on Windows, using `XCTSkip` / Swift
Testing's `.disabled` / a gated `setUp` where a fixture itself traps,
each with a reason. Broadly:

- **Unsupported transports/APIs:** Unix domain sockets, datagram/UDP
channels, pipe channels, raw sockets, vsock, pre-connected sockets, and
`FileRegion` sends.
- **Whole suites** for subsystems that aren't functional on Windows yet,
including the **NIOFileSystem family** (`NIOFSTests`,
`NIOFSIntegrationTests`, `NIOFSFoundationCompatTests`) whose Windows
syscall shims are still stubbed, plus `EchoServerClientTest`,
`ChannelTests`, `DatagramChannelTests`, `StreamChannelTest`,
`FileRegionTest`, `PipeChannelTest`, `RawSocketBootstrapTests`,
`ThreadTest`, and `SocketOptionProviderTest`.
- **Real crashes surfaced by a full run:** `System.enumerateDevices()`
(mis-sized `GetAdaptersAddresses` buffer), the WSAPoll selector trapping
on an invalid fd and on schedule-after-shutdown, and a connection-reset
promise leak.
- **Flaky, timing-sensitive tests:** the event-loop tick-count metrics
tests (the WSAPoll selector wakes far more often than epoll/kqueue).

Then enable the Windows 6.3 + nightly jobs in the reusable unit-tests
workflow (which runs `swift test`) in both the PR and push workflows,
and drop the separate build-only Windows job (which only ran `swift
build --build-tests`).

Windows uses the default `swift test` arguments rather than the Linux
`-Xswiftc -warnings-as-errors` override, because a `#warning` in the
(skipped) stream-channel tests would otherwise fail the build.

All skips are Windows-only; there is no behaviour change on other
platforms.

### Result:

`swift test` passes on Windows, and the Windows unit tests run in CI.

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
2026-07-29 09:41:53 +01:00
Jake PetroulesandGeorge Barnett 15b77e6400 [Windows] Compile the remaining test targets (NIOPosixTests, NIOFoundationCompatTests) (#3665)
## Motivation

Follow-up to #3663 (which got the "low-hanging" test targets compiling
on Windows). This finishes the job by getting the **last two** test
targets — `NIOPosixTests` and `NIOFoundationCompatTests` — compiling on
Windows, so **all 16 test targets now build**, and switches Windows CI
to `swift build --build-tests`.

These are **compile-only** changes: the goal is compilation, not runtime
pass. Verified by building on a Windows ARM64 VM with `swift build
--build-tests`.

## What's here

1. **Compile `NIOFoundationCompatTests`** — the target imports the `NIO`
umbrella (in `WaitSpinningRunLoopTests`, for
`MultiThreadedEventLoopGroup`) but only declared
`NIOCore`/`NIOFoundationCompat` as dependencies. `NIO` re-exports
`NIOPosix`, which imports the `CNIOPosix` **C module**; Linux/macOS
resolve that transitively, but on Windows the C-module map isn't in the
target's search path unless declared, giving the opaque `<unknown>:0:
error: missing required module 'CNIOPosix'`. Fix: declare the `NIO`
dependency it actually uses.

2. **Compile `NIOPosixTests`** — the deeply-POSIX target.
Gate-out-plus-minimal-shim:
- `import WinSDK` where only `sockaddr`/`timeval`/`in_addr` field or
type access was missing (`MemberImportVisibility`) — this alone cleared
the two largest files (`SocketAddressTest`, `HappyEyeballsTest`).
- Real fixes where cheap: `pipe` → `_pipe` (via `ucrt`), an
`STDOUT_FILENO` shim, `Thread.sleep` instead of `sleep`, and letting
`IOResult` type inference pick `ssize_t`.
- `#if !os(Windows)` gating (per-method where possible, to preserve
Windows-compilable coverage) for genuinely-unavailable APIs:
`fcntl`/`O_NONBLOCK`/`O_ASYNC`/`F_GETFL`/`F_SETFL`, `Posix.fcntl`,
`Posix.socketpair`, the integer socket-option API
(`ChannelOptions.socket`, `SOL_SOCKET`, `SO_REUSEADDR`, `so_timestamp`),
`poll`/`pollfd`, `cmsg`/`msghdr` construction,
`NonBlockingFileIO.lstat`/`symlink`/`listDirectory`/`createDirectory`,
vsock CID/connect tests, `/dev/null` syscall-wrapper perf tests, and
socket-handle (`NIOBSDSocket.Handle` `UInt64` vs `CInt`) mismatches.
- Whole-file gating only where a file is pervasively unsupported: the
SAL mock harness and its dependents (`SyscallAbstractionLayer[Context]`,
`SALChannelTests`, `SALEventLoopTests`) — it's built on `CInt` fds and
can't compile against Windows's `UInt64` socket handles without a larger
rework; `ControlMessageTests` (POSIX `msghdr`/control-message
throughout); and `MulticastTest` (deprecated
`enumerateInterfaces`/`NIONetworkInterface`).

3. **Build the test targets in CI** — change the Windows matrix command
from `swift build` to `swift build --build-tests` in `main.yml` and
`pull_request.yml`.

## Notes for reviewers

- No behavior change off Windows: every edit is behind `#if os(Windows)`
/ `#if !os(Windows)`; the few shared-line changes (`Thread.sleep`,
`IOResult` inference, an IUO force-unwrap of `msg_name`) are identical
on POSIX.
- The tests are **compiled, not run** on Windows yet. Running them needs
real Winsock work (socket handle types, etc.) and is the natural next
step.

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
2026-07-24 13:59:45 +00:00
George Barnett afc79dd083 Implement customisation point for scheduled callback on isolated EL (#3679)
Motivation:

The schedule callback mechanism on `EventLoop` has an equivalent on an
isolated `EventLoop`. The default isolated implementation puts the
hander in a loop bound box and calls the non-isolated version.

This is okay but introduces a number of additional "in event loop"
checks which are required for correctness in the general case (i.e. not
called from an isolated event loop).

Modifications:

- Implement the customisation point directly to avoid the additional
checks (the are unecessary by definition of being on the isolated event
loop).
- Add benchmarks

Result:

On the isolated event loop, in micro benchmarks:
- scheduleTask is ~1.6x faster
- scheduleCallback is ~2x faster
2026-07-23 11:26:58 +01:00
MOHAMMED WASIM KHAN 3777a70115 Fallback to non-positional read/write on ENXIO (#3671)
This PR addresses **#3613** where writing to `/dev/stdout` fails with
`ENXIO (6) Device not configured` when running in a terminal environment
instead of a pipe.

Currently, `SystemFileHandle` checks for `errno == .illegalSeek`
(ESPIPE) when deciding whether to fall back from positional
`pwrite`/`pread` to non-positional `write`/`read` for unseekable files
like pipes. However, macOS/Darwin returns `ENXIO`
(`Errno.noSuchDeviceOrAddress`) instead of `ESPIPE` when attempting a
positional write/read on a terminal device.

**Changes:**
- Added `.noSuchDeviceOrAddress` to the fallback conditions in
`SystemFileHandle.swift` for both `readChunk` and `write`.
- Updated `FileSystemError+Syscall.swift` to map
`.noSuchDeviceOrAddress` to `.unsupported` alongside `.illegalSeek`.
- Replicated these changes across both the `_NIOFileSystem` and `NIOFS`
modules.

This allows `FileSystem.shared.withFileHandle` and other APIs to work
transparently with terminal devices like `/dev/stdout`!
2026-07-22 17:55:38 +01:00
hamzahrmalikandCory Benfield 199ccb983c Add variants of flatMap which can return EventLoopFuture<NonSendable> (#3634)
### Motivation:

Currently, EventLoopFuture.flatMap requires the inner eventloop to have
a Sendable type. This makes sense, because the value might move between
eventloops, if the inner and outer future are not on the same EventLoop.
However, this makes it impossible for users to use non Sendable values
even when they _know_ they're on the right eventloop

### Modifications:

Add a new `flatMapIsolated` function which can be used in this case. It
requires users to return an EventLoopFuture.Isolated to prove that they
are on the same eventloop

### Example:

```swift
class T { // Not Sendable
    init() {

    }
}

func test(eventLoop: any EventLoop) -> EventLoopFuture<T> {
    let a = eventLoop.submit { T() }
    let b = eventLoop.submit { T() }
    return a.flatMap { _ in b }
}
```

The above example gives a warning on the final line, because flatMap
requires the inner future (`b`) to have a Sendable type. That is because
it doesn't know that a and b are on the same EventLoop.

```swift
func test(eventLoop: any EventLoop) -> EventLoopFuture<T> {
    let a = eventLoop.submit { T() }
    let b = eventLoop.submit { T() }
    return a.flatMapIsolated { _ in b.assumeIsolated() }
}
```

Here, it compiles without warnings. We can write assumeIsolated inside
the flatMapIsolated closure because we know that the eventloops are the
same. And therefore the new flatMapIsolated function does not have a
Sendable requirement on its value

Co-authored-by: Cory Benfield <lukasa@apple.com>
2026-07-20 11:15:50 +00:00
Jake PetroulesandGeorge Barnett 3bd6c86af3 [Windows] Compile the low-hanging test targets (#3663)
## Motivation

Follow-up to #3649 (all non-test targets now build on Windows). This
gets the bulk of the **test** targets compiling on Windows so we can
start moving toward running them in CI.

Of the 16 test targets, 6 already compiled unchanged. This brings **8
more** to a clean compile, leaving only two as dedicated follow-ups:
- `NIOPosixTests` — large, many distinct Winsock/POSIX issues; deserves
its own PR.
- `NIOFoundationCompatTests` — a `missing required module 'CNIOPosix'`
issue to investigate separately.

The changes are **compile-only** — they don't aim to make the tests
*pass* on Windows yet. Verified by building each target on a Windows
ARM64 VM; not yet wired into CI (CI still builds products only, since
the two deferred targets would fail `--build-tests`).

## What's here

Three commits:

1. **Make `Posix.close` available on Windows** — it was gated out only
because it sat inside the socket-syscalls `#if !os(Windows)` block (next
to `shutdown`), even though its body already handled Windows. Define
`sysClose = _close` and lift `close(descriptor:)` out of that block. No
behavior change off Windows.
2. **Compile the low-hanging test targets** — a
gate-out-plus-minimal-shim approach:
- A tiny `#if os(Windows)` `usleep` shim (backed by `Sleep`), once per
affected target, plus a Windows branch for the `#error` C-library check.
- Real Windows implementations for the temp-file/pipe test helpers
rather than stubs: `withPipe` → `_pipe`, `openTemporaryFile` →
`_mktemp_s` + `_sopen_s`, `createTemporaryDirectory` → a GUID +
`CreateDirectoryW`.
- Gate out genuinely-unavailable test logic behind `#if !os(Windows)`:
`System.enumerateInterfaces`, `ChannelOptions.socket(IPPROTO_IP,
IP_TTL)`, `mkfifo`-based tests, the POSIX-`stat`-shaped `FileInfoTests`,
the `S_IFSOCK`/`DT_*` conversion tests, and the `openat` oflag-bit test.
- Two Windows-only shim tweaks make the NIOFS `OpenOptions` gap values
and `Errno.noData` `@_spi(Testing) public`, matching how swift-system
exposes the equivalents elsewhere (the tests reach them via an SPI
import).
- The `NIOHTTP1Tests` and `NIOPosixTests` copies of `TestUtils.swift`
are intentional duplicates and get identical Windows helper
implementations to stay in sync.
3. **Document why `SocketOption`'s integer API is unavailable on
Windows** — comment-only: WinSDK imports the socket option level/name
constants as enumeration cases, not plain integers, so they don't fit
the integer `SocketOptionLevel`/`SocketOptionName` typealiases.

## Notes for reviewers

- No behavior change off Windows: every change is behind `#if
os(Windows)` / `#if !os(Windows)`; the one shared edit (a test buffer
typed `CChar` instead of `CInterop.PlatformChar`) is identical on POSIX.
- The `usleep` shim is duplicated per target rather than shared: there's
no test-only support target these all depend on (the shared deps,
`NIOCore`/`NIOTestUtils`, are shipping libraries). It can be centralised
later if desired.

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
2026-07-20 09:03:58 +01:00
Gus Cairo 0b18836bd8 Don't write any bytes for H1 responses that shouldn't include a body (#3651)
### Motivation:

On the server side, `HTTPResponseEncoder` chooses a response's body
framing only from the response status
(`HTTPResponseStatus.mayHaveResponseBody`): it has no knowledge of the
request method as it's an outbound-only handler. But HTTP requires that
a response to a HEAD request (and a 2xx response to CONNECT) carry no
message body, regardless of status or framing headers.

As a result, a response to a HEAD request with a status that allows a
body (like 200) and no `Content-Length` gets framed as
`Transfer-Encoding: chunked`, and the encoder emits the chunked
terminator (`0\r\n\r\n`). This is illegal.
A conformant client won't read those bytes when receiving a HEAD
response, so on a keep-alive connection they get misinterpreted as the
start of the next response, corrupting the connection. This surfaces as
intermittent, hard-to-diagnose failures (e.g. "I/O on closed channel")
on the following request.


### Modifications:

Made the server response framing method-aware without changing any
public API:
  
- The request decoder now hands each decoded request's method to the
response encoder.
- `HTTPResponseEncoder` consults the method and, for responses to
HEAD/CONNECT requests, omits the body and the terminator.
- This is wired up in `configureHTTPServerPipeline`. When the encoder is
constructed and used on its own, its behaviour is unchanged (framing is
still derived solely from the status), so there is no behavioural or API
change for users who may be creating this handler manually.


### Result:

Responses to `HEAD` and `CONNECT` requests are no longer emitted with a
message body, keeping keep-alive connections well-formed and
RFC-compliant. No public API changes.
2026-07-15 14:55:52 +01:00
Johannes Weiss b1f2dd18fc rewrite NIOCrashTester in Swift Testing (#3658)
### Motivation:

The old NIOCrashTester doesn't work on Windows and would be hard to
port. Also, Swift Testing has wonderful 'exit test' ability, let's use
that!

### Modifications:

- Rewrite all NIOCrashTester tests in Swift Testing

### Result:

- Less code to maintain
- (hopefully) works on Windows
- fixes #3657
2026-07-13 15:12:55 +01:00
Johannes Weiss eeb20e7f95 HTTPDecoder: restore old limit of 80 kB headers (#3621)
### Motivation:

NIO's historic limit was that the maximum header block was limited to 80
kB which is a sensible limit and originates from Nginx's http_parser.c.

The history of the changes:
- commit e67fc995: Introduces the 80 kB limit
- commit 69180342: Accidentally removes the limit entirely
- commit b24872d3: Introduced the 2 MB / 256 headers limit

I would argue that 2MB & 256 headers is a bad limit:
- 2MB is way too big, that allows pretty powerful slowloris-style
attacks forcing NIO to hold on to 2 MB per connection
- 256 headers is too little for software that may be a proxy
  - in HTTP, the number of headers is _non-semantic_
- A: `a: 1\r\n a: 2\r\n a: 3\r\n a: 4\r\n a: 5\r\n a: 6\r\n[...]\r\na:
257\r\n`
    - B: `a: 1, 2, 3, 4, 5, ..., 257\r\n`
- A & B are _equivalent_. Yet, A would be rejected and B accepted.
That's not right.
 
### Modifications:

Restore the old limits.

### Result:

- More compatible (because we allow more than 256 headers)
- More secure (because the default goes back to 80kB)
2026-06-30 09:22:19 +00:00
Cory Benfield cd3e115208 Avoid exit tests on iOS/tvOS/watchOS/visionOS (#3639)
Motivation:

The exit tests fail to compile on these platforms.

Modifications:

Commented out the exit tests on those platforms.

Result:

Better testing
2026-06-24 08:58:19 +00:00
Cory Benfield 74c924fd01 Apply an autoreleasepool around the task drop (#3638)
Motivation:

Users can write arbitrary code in their deinits, and that arbitrary code
_may_ end up hitting Objective-C autoreleases. We don't want to leak
those objects into the per-thread autoreleasepool, as that may not be
popped for a very long time, if ever, so we need to to ensure that
deiniting the task objects has an autoreleasepool.

Modifications:

Add an autoreleasepool around the task drop, and tests to validate that
we do things correctly.

Result:

Better memory management around deinit
2026-06-24 09:27:31 +01:00
Kumar TanayandGeorge Barnett a8e036cb86 Fix incorrect status lines in HTTPResponseEncoder fast paths (#3631)
### Motivation

`HTTPResponseEncoder` writes hand-optimised status lines for the
well-known `HTTPResponseStatus` values instead of going through the
general `default` path that formats `HTTP/<version> <code>
<reasonPhrase>`. Several of those fast paths had drifted out of sync
with `HTTPResponseStatus.reasonPhrase`, so the exact bytes on the wire
depended on whether a status happened to have a fast path:

* **HTTP/1.0 + 511** emitted the wrong HTTP version (`HTTP/1.1 511 ...`)
— a wire-format correctness bug, not just cosmetic.
* **HTTP/1.1 + 431** emitted the reason phrase `Range Not Satisfiable`
instead of `Request Header Fields Too Large`.
* **HTTP/1.1 + 416** emitted `Request Range Not Satisified` (also
misspelled) instead of `Range Not Satisfiable`.
* **HTTP/1.0 and HTTP/1.1 + 307** emitted `Tempory Redirect`
(misspelled) instead of `Temporary Redirect`.

Resolves #3630.

### Modifications

* Correct the five diverging status lines in `HTTPResponseEncoder` so
they match `HTTPResponseStatus.reasonPhrase` and carry the correct HTTP
version.
* Add a regression test that, for every well-known status at both
HTTP/1.0 and HTTP/1.1, asserts the encoded status line equals the
canonical `HTTP/<version> <code> <reasonPhrase>` form, keeping the fast
paths in lockstep with `reasonPhrase`.

### Result

The encoder now produces correct, consistent status lines for all
well-known statuses regardless of whether they use a fast path. The new
test fails on the pre-fix code (the 511 case fails on the version, the
431/416/307 cases on the reason phrase) and passes after the fix.

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
2026-06-23 12:33:49 +00:00
jmestwa-coderandCory Benfield 0b38af6217 fix off-by-one boundaries in QUIC bytesNeededForInteger (#3628)
Off-by-one in the QUIC variable-length integer length boundaries.

### Motivation:

`bytesNeededForInteger` classifies an integer into the 1/2/4/8 byte QUIC
encoding using half-open ranges (`0..<63`, `0..<16383`,
`0..<1_073_741_823`, `0..<4_611_686_018_427_387_903`). Those upper
bounds are the *largest* value each size can hold (2^6-1, 2^14-1, 2^30-1
and 2^62-1 per RFC 9000 § 16), so each maximum is wrongly excluded:

- 63, 16383 and 2^30-1 report one byte too many, so the encoder writes
them with a larger encoding than needed.
- 2^62-1, the largest value the encoding can represent, falls through to
the `fatalError` and crashes the encoder.

The existing one-byte write test only iterates `0..<63`, so the 63 case
was never exercised.

### Modifications:

- Switch the four ranges in `bytesNeededForInteger` to inclusive bounds.
- Extend the one-byte write test to include 63, and add boundary checks
plus a 2^62-1 round-trip test.

### Result:

Boundary values encode in the minimal number of bytes and 2^62-1
round-trips instead of crashing.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2026-06-22 12:45:20 +00:00
Gabriel Secula 3263350c88 Drop unused SystemPackage import from FileSystemTests (#3623)
Drop unused SystemPackage import from FileSystemTests

### Motivation:

swift-system 1.7.0 exposes a new type named `FileType`, which is
ambiguous with `NIOFS.FileType` when both modules are imported in the
same file. This causes a compile-time error when building
`FileSystemTests.swift` in the `NIOFSIntegrationTests` target.

Fixes #3622.

### Modifications:

Drop the unused `SystemPackage` import in `FileSystemTests.swift`.

### Result:

`NIOFSIntegrationTests` builds against swift-system 1.7.0 and later.
2026-06-13 02:25:51 +00:00
Mads Odgaard 77b84ac2cd [NIOFileSystem] Fallback to rename when renameat2 is not available (#3612)
We are running into some issues when doing:

```swift
        let writeOptions: OpenOptions.Write =
            byteOffset > 0 ? .modifyFile(createIfNecessary: true) : .newFile(replaceExisting: true)

        try await FileSystem.shared.withFileHandle(
            forWritingAt: FilePath(outputFilePath),
            options: writeOptions
        ) { fileHandle in
```

which results in crashes on some Android devices where `renameat2` is
not available:

```
('renameat2' system call failed with '(38) Function not implemented'.)
```

This PR adds functionality similar to what is already done for `EINVAL`
on Linux, such that both lead to the same recovery: stat the
destination, then plain rename() if absent.

And for non-exclusive renames where it also fails to function not
implemented, it falls back to plain `rename`.
2026-06-03 14:33:41 +01:00
Aditya Singh 03848cb0cc NonBlockingFileIO: error instead of silently truncating oversized reads (#3607)
Error instead of silently truncating oversized `NonBlockingFileIO`
reads.

### Motivation:

A `ByteBuffer` can hold at most `UInt32.max` bytes, so a read for more
than that can never be satisfied. Today the read functions clamp the
requested `byteCount` to `Int32.max` and then read at most that many
bytes, returning a short buffer with no error and no end-of-file. That
silently breaks the documented contract that the returned `ByteBuffer`
is only shorter than `byteCount` when end-of-file is reached. It also
means a request between 2 GiB and 4 GiB returns fewer bytes than the
caller asked for.

Resolves #2591.

### Modifications:

- `read0` (both the `EventLoopFuture` and the `async` overload) now
fails with an `IOError` (`EINVAL`) when `byteCount` is larger than
`UInt32.max` instead of clamping. The guard runs before any allocation.
On 32-bit platforms `Int.max` is smaller than `UInt32.max` so the guard
is never hit there, and behaviour is unchanged.
- `readSync` now caps the size of each individual `read`/`pread` syscall
at `Int32.max` rather than capping the total `byteCount`. The OS returns
`EINVAL` for a single read larger than `Int32.max`, and the surrounding
loop already reads repeatedly until the whole request is satisfied or
end-of-file is reached, so a request up to `UInt32.max` is now read in
full.
- Added tests covering the future and async read variants for a
`byteCount` larger than a `ByteBuffer` can hold.

I went with `IOError(errnoCode: EINVAL, ...)` rather than a new
`NonBlockingFileIO.Error` case to keep this non API-breaking, matching
the existing `IOError(errnoCode: ENOTDIR, ...)` use in the same file.
Happy to switch to a typed error case if you would prefer that.

### Result:

A read that cannot fit in a `ByteBuffer` fails clearly, and reads
between 2 GiB and 4 GiB are no longer truncated. No public API changes.
2026-05-29 13:53:15 +00:00
Aditya Singh 9ebe69d269 Add get/setMultipleIntegers on ByteBuffer (#3606)
`ByteBuffer` exposes `read`, `write`, and (since the peek work landed)
`peek`
variants of `MultipleIntegers`, but not the absolute-index `get`/`set`
pair that
every other integer accessor on `ByteBuffer` has. This fills that gap,
which is
what #2736 originally asked for.

The thread on #2736 settled on adding `peek*` first so that the common
"look without consuming" use case has a non-mutating, index-free
spelling, and
that is now in place. `get`/`set` remain useful for the niche cases that
genuinely need to address an arbitrary index, so this completes the set
of four
for `MultipleIntegers` to match `getInteger`/`setInteger`.

Both methods are generated from `dev/generate-bytebuffer-multi-int.sh`,
the same
script that produces the read/write/peek variants, so they stay in
lockstep with
the rest of the file across all arities (2 through 15):

- `getMultipleIntegers(at:endianness:as:)` reads at an explicit index
without
moving the reader index and returns `nil` when the requested bytes are
not
  readable, exactly like `getInteger(at:)`.
- `setMultipleIntegers(_:at:endianness:as:)` writes at an explicit index
without
moving the writer index and returns the number of bytes written, exactly
like
  `setInteger(_:at:)`.

The new tests mirror the existing read/write and peek tests: a
round-trip across
both endiannesses, a check that the bytes written by `set` are
byte-for-byte
identical to `writeMultipleIntegers`, coverage of every arity, a check
that
neither method moves the reader or writer index, and the out-of-bounds /
before-reader-index cases that must return `nil`.

Resolves #2736
2026-05-29 12:30:55 +00:00
4e370dc4d7 NIOWebSocket: reject 8-byte payload lengths that exceed Int.max (#3603)
RFC 6455 §5.2 specifies that when the payload length indicator byte is
127, the following 8 bytes hold a 64-bit unsigned integer and the most
significant bit of that integer must be 0. The maximum legal value is
therefore 0x7FFFFFFFFFFFFFFF, which equals Int.max on all 64-bit
platforms Swift supports.

WSParser.parseStep read the raw UInt64 and passed it directly to Int()
without a bounds check. Swift's Int initializer performs a checked
conversion and traps on overflow, so any remote peer that sends a single
WebSocket frame with the MSB of the 8-byte length set will cause a fatal
error in the receiving process. No authentication is required; the frame
is processed before any application handler runs.

The fix adds a guard that rejects the frame with
NIOWebSocketError.invalidFrameLength before the conversion, which is the
same error path used for frames that exceed maxFrameSize. The call site
in decode() is updated to propagate the throw.

A regression test covers the exact byte sequence that previously caused
the fatal trap.

Co-authored-by: g0w6y <g0w6y@users.noreply.github.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2026-05-26 15:35:59 +01:00
Aditya Singh d20fa05a64 Fix readInlineArray reading from the wrong index when readerIndex is non-zero (#3605)
ByteBuffer.readInlineArray reads each element through getInteger(at:).
The index parameter of getInteger(at:) is an absolute index into the
buffer, the same one readInteger passes self.readerIndex into. The loop
in readInlineArray instead passed stride * index, which starts at zero
and never accounts for the reader index.

As long as the buffer has a reader index of zero this absolute index
lines up with the bytes the caller expects, which is why the current
tests pass: they all read from a freshly written buffer. On any buffer
whose reader index has already moved (the normal case once some bytes
have been consumed, for example reading a length prefix before the
array), getInteger(at:) is given an index below the reader index.
rangeWithinReadableBytes rejects it, getInteger returns nil, and the
force-unwrap on the result traps. So a public read API crashes the
process on entirely valid input.

The fix snapshots the reader index before building the array and offsets
each element index by it, so getInteger(at:) gets the correct absolute
index. The multiplication stays bounded by bytesRequired as before, and
adding the reader index cannot overflow because the result stays within
the writer index.

I added a regression test that writes a short prefix, reads it off so
the reader index is non-zero, then reads an InlineArray and checks both
the element values and the resulting reader index. It traps before this
change and passes after it. The three existing InlineArray tests, the
full ByteBufferTest suite (275 tests), and ByteBufferSpanTests all still
pass.

Reopening the change from #3604, which I closed inadvertently. The
original branch and fork were deleted, so this is a fresh PR with the
identical commit.
2026-05-26 09:03:23 +00:00
Adam FowlerandCory Benfield 9670fe1d68 Don't fatalError if NIOTypedHTTPServerUpgraderStateMachine is in state finished when finding upgrader (#3597)
Don't fatalError in
NIOTypedHTTPServerUpgraderStateMachine.findingUpgraderCompleted` when
the state is `.finished`.

### Motivation:

There is a race where the connection can be closed and the
NIOTypedHTTPServerUpgraderHandler can be removed setting the state to
`.finished` while we are still finding upgraders. And then when
`findingUpgraderCompleted` gets called it would fatalError.

### Modifications:

- Return `nil` as an action if the state is `.finished` when
`findingUpgraderCompleted` is called.
- Added a new set of tests which test the
`NIOTypedHTTPServerUpgraderStateMachine` directly instead of relying on
`HTTPServerUpgraderTests`. I used Swift Testing for these, not sure what
NIO policy on new tests are?

### Result:

No more crashes and better testing for
NIOTypedHTTPServerUpgraderStateMachine

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2026-05-21 13:41:50 +00:00
Fabian Fett 57c0a08a33 Speed up the tests (#3601)
### Motivation

We want our tests to pass in reasonable time.

### Changes

- Disable test
`ByteBufferCrashTests.setBytesWithoutContigiousStorageMoreThanUInt32maxBytes`
- Do not initialize data in `ByteBufferCrashTests.
takingOwnershipOfPointerThatsToLarge`

### Result

Tests are faster again.
2026-05-20 10:12:53 +00:00
Fabian Fett b24872d3aa Merge commit from fork
Add configurable HTTP/1 decoder parsing limits

### Motivation:

The HTTP/1 decoder previously enforced a single hardcoded limit (80 KB per header field) and placed no cap on the total header-block size or the number of header fields per message. That made resource use unpredictable on requests with very many or very large headers, and there was no way for users to tune the limits for their workload.

### Modifications:

- Introduce a new public `NIOHTTPDecoderLimitConfiguration` exposing three parsing limits:
  - maxHeaderFieldSize — maximum size of a single header field (name + value). Default: 80 KB.
  - maxHeaderListSize — maximum total bytes of all header field names and values combined per message. Default: 2 MB.
  - maxHeaderFieldCount — maximum number of header fields per message, including trailers. Default: 256.
- Plumb the configuration through `HTTPDecoder` / `HTTPRequestDecoder` / `HTTPResponseDecoder` initializers. The existing initializers continue to work unchanged via convenience overloads that fall back to
  default limits.
- Expose the configuration on `NIOUpgradableHTTPServerPipelineConfiguration` and `NIOUpgradableHTTPClientPipelineConfiguration` so users of the typed pipeline setup can override the defaults.
- Track per-message totals in `BetterHTTPParser` and reject with `HTTPParserError.headerOverflow` when a limit is exceeded; counters reset at each new message so HTTP/1 pipelined connections behave
  correctly.
- Add tests covering the success and error paths for each limit, the default behaviors, and that trailer fields count toward the field-count cap.

### Result:

Users can configure the HTTP/1 decoder's per-field-size, total-list-size, and field-count limits. Existing call sites continue to compile and run unchanged, now with bounded defaults across all three dimensions instead of just one.
2026-05-20 10:00:56 +01:00
Fabian Fett 87f935b70c Merge commit from fork
This patch adds additional validation that we don't overflow the ByteBuffers capacity (which is capped at UInt32.max).
2026-05-20 09:58:46 +01:00
Fabian Fett dd16365724 Merge commit from fork
- Ensure HTTP Request Uri and Method only contain allowed bytes
- Ensure HTTP Response Reasons only contain allowed bytes
2026-05-20 09:56:46 +01:00
PAVANSAI KUMAR ALLADIandPavan sai kumar alladi 9f506606a9 Add dnsResolver configuration option for randomized DNS load balancing (#3577)
### Motivation:

When connecting to services with multiple DNS records (e.g. Kubernetes
headless services), getaddrinfo returns addresses in a deterministic
order, causing all connections to go to the same backend. SwiftNIO now
provides a RandomizedDNSResolver that shuffles results.

### Modifications:

Add a dnsResolver property to HTTPClient.Configuration with two options:
.system (default, current behavior) and randomized (uses NIO's
RandomizedDNSResolver to shuffle DNS results). Apply the resolver to
ClientBootstrap during connection creation.

### Result:

Users can opt into randomized DNS resolution by setting
config.dnsResolver = .randomized, enabling load distribution across
multiple backend IPs while preserving the default behavior.

---------

Co-authored-by: Pavan sai kumar alladi <p_alladi@apple.com>
2026-05-17 21:53:29 +00:00
David SternandDavid Stern c077074e9f Allow concurrent createDirectory calls with shared ancestor paths (#3598)
Allow concurrent calls of `_createDirectory()` for file paths with
shared ancestors.

### Motivation:

The current implementation of
`_createDirectory(withIntermediateDirectories: true)` does not catch and
continue during execution when the intermediate directory path it is
trying to create already exists.

The current loop looks like this: 

```swift
 // Successfully made a directory, construct its children.
        while let subdirectory = droppedComponents.popLast() {
            path.append(subdirectory)
            switch Syscall.mkdir(at: path, permissions: permissions) {
            case .success:
                continue
            case let .failure(errno):
                return .failure(.mkdir(errno: errno, path: path, location: .here()))
            }
        }
```
If we are concurrently trying to create directories `/foo/bar/dir1` and
`foo/bar/dir2`, we can fail with an error like `directory /foo already
exists` because one of the two task's `mkdir` syscall on line 928
errored with a `fileExists` error.
 
### Modifications:

The code adds the same logic already present on lines 894-907 of
`FileSystem.swift` to the failure case while iterating over the
subdirectory paths:

If the syscall errors because the directory already exists, (but didn't
exist during the loop on lines 886-923), we can safely continue. Another
task must have beaten our task to creating the directory, but that is
ok.

### Result:

Without this change, it would be difficult to actually call the
`FileSystem.createDirectory(withIntermediateDirectories: true)` function
in concurrent tasks, because if you were creating directories with
common ancestors, at least one of the 2 tasks would error due to the
race condition on directory creation. With this change, the
`createDirectory()` function can now be called concurrently without this
race issue potentially causing an error. I added the change to both the
`NIOFS` and `_NIOFileSystem` directories since they seem to be copies of
each other.

Co-authored-by: David Stern <d_stern@apple.com>
2026-05-15 08:11:37 +00:00
Francesco Paolo Severino 32f395bc91 Add helpers for reading length-prefixed things (#3589)
Add missing helpers for reading length-prefixed strings and bytes with
customizable encodings for the length.

### Motivation:

There are already some APIs for writing length-prefixed strings and
bytes with a custom strategy for encoding the length, but there weren't
helpers to read them.

### Modifications:

- Add missing helpers for reading length-prefixed strings and bytes with
customizable encodings for the length
- Add tests for the new APIs 
- Fix DocC comments and improve the related article 

### Result:

New APIs are added for reading length-prefixed strings and bytes with a
custom strategy for encoding the length.
2026-05-12 11:53:10 +01:00
Fabian Fett 37e191d35b Strip Transfer-Encoding when Content-Length is present in HTTPEncoder (#3583)
Strip Transfer-Encoding when Content-Length is present in HTTPEncoder

### Motivation

When `correctlyFrameTransportHeaders()` detected a `Content-Length`
header, it returned `.contentLength` without removing any existing
`Transfer-Encoding` header.

### Changes

- Remove `Transfer-Encoding` header, if a `Content-Length` header is
present

### Result

NIO sends either a `Content-Length` header or a `Transfer-Encoding`
header, but not both.
2026-05-06 10:14:09 +00:00
Fabian Fett a57cc530e7 Translate HTTPHeaderValidationTests to Swift testing (#3579)
Translate `HTTPHeaderValidationTests` to Swift testing

### Motivation:

We want to add tests that use parameterized testing to
`HTTPHeaderValidationTests`. In order to allow this we first need to
translate this file to use Swift Testing instead of XCTest.

### Modifications:

- Mechanical translation of `HTTPHeaderValidationTests` from XCTest to
Swift testing.

### Result:

We can add parameterized tests to HTTPHeaderValidationTests.
2026-04-21 16:37:06 +02:00