Motivation:
We have a few other file system modules with a small amount of code, and
a lot of tests. Most of these require no changes for strict concurrency,
so let's tackle them all.
Modifications:
Strict concurrency for filesystem targets enabled. Fixed a few incorrect
test case captures.
Result:
Strict concurrency clean filesystem bits.
Adds a new `ChannelPipeline.SynchronousOperations.Position` type to fill
a hole in the API that prevented non-sendable Channel handlers being
added at a position to the pipeline via the synchronous operations.
---------
Co-authored-by: Cory Benfield <lukasa@apple.com>
Motivation:
We're continuing out Strict Concurrency journey, making sure users of
NIO can write data-race-free code.
Modifications:
- Added some missing Sendable annotations in NIOAsyncSequenceProducer
- Made BufferedStream unconditionally Sendable, and required its Element
type to also be Sendable.
The prior constraint wasn't actually correct. We always
behaved as though the element types were Sendable, by passing
them into continuations. This cleans things up.
- Made AnyAsyncSequence Sendable, which it needs to be.
- Made BufferedOrAnyStream Sendable, which it needs to be.
- Made DirectoryEntries explicitly Sendable, which it was.
- Made DirectoryEntries.Batched explicitly Sendable.
Result:
Better concurrency-safety
Motivation:
NIOTests was already warning free, now we're just going to make sure of
it.
Modifications:
Enable strict concurrency for NIOTests
Result:
Our relentless safety march continues.
Motivation:
A few more low-hanging-fruit test targets.
Modifications:
Enable strict concurrency for:
- NIOFoundationCompatTests
- NIOSingletonsTests
Result:
We're knocking down those targets, one by one.
Motivation:
Our next suite of examples for strict concurrency are the chat ones.
These are very similar to the rest: either no violation at all, or just
a need to use the sync operations on pipeline setup.
Modifications:
- Use sync operations
- Lock in strict concurrency checking
Result:
We continue our march.
Co-authored-by: Rick Newton-Rogers <rnro@apple.com>
Motivation:
Two more examples with no changes required.
Modifications:
Lock in strict concurrency for NIOTCPEchoClient and NIOTCPEchoServer.
Result:
Even more stricter concurrencier.
Motivation:
NIOUDPEchoClient should be strict concurrency clean.
Modifications:
Annotate one closure @Sendable to convince Swift it's ok.
Result:
Strict concurrency clean in NIOUDPEchoClient.
Motivation:
Prior patches actually removed all the issues in NIOEchoServer, so let's
bank the win.
Modifications:
Added strict concurrency checking to NIOEchoServer
Result:
More stricter.
Motivation:
Another strict concurrency change, another echo handler.
Modifications:
Move to sync operations.
Result:
Another step on strict concurrency.
Motivation:
We're getting to the examples now! This patch updates NIOEchoClient to
be clean under strict concurrency.
Modifications:
Use the sync operations to add echo handler.
Result:
Another strict concurrency module.
Co-authored-by: George Barnett <gbarnett@apple.com>
Motivation:
Honestly these little modules are pretty mechanical now.
Modifications:
Add the strict concurrency flags to a module with nothing in it.
Result:
Become even more automatic from way downtown.
Motivation:
I continue to grab the low-hanging fruit. This time I've opted a purely
data-processing module into strict concurrency. Truly, I am the hero
Gotham deserves.
Modifications:
Set some build flags.
Result:
Stricter concurrency.
Motivation:
We intend to have this package be completely strict-concurrency clean,
so every target needs to be opted in, even those that currently have no
code.
Modifications:
Opt in this empty target to strict concurrency.
Result:
One more database entry.
### Motivation:
Some repositories still use the older CI Swift matrix directly so we
need to update it to handle 6.1 nightlies.
### Modifications:
Replace 6.0 nightly with 6.1 nightly.
### Result:
External CI should use 6.1 nightly.
### Motivation:
Xcode 13 is really quite old (3 major releases and it was the GM release
anyway) and this workaround can cause other issues.
### Modifications:
- Remove the Xcode 13 GM workaround.
-
### Result:
Better, possibly faster code.
Motivation
NIOPosix should be clean under strict concurrency
Modifications
- Fix newly-added issue in GetaddrinfoResolver
- Fix newly-added issue in SEL
- Lock in the win in Package.swift
Result
We did it!
Motivation
It's tempting for us to use the FILE * for stderr whenever we want to
print to it. However, the Linux libc modules aren't appropriately
annotated to call that field constant, and indeed it isn't. This causes
Swift to throw a fit if we even touch it.
Modifications
Introduce an awkward function that issues direct writes to stderr
instead of using the FILE *.
Result
Swift is happier.
Motivation
Unfortunately there are a few places where we have to help the compiler
out to realise that our copies of the libc functions are also Sendable,
and -require-explicit-sendable can cause us some trouble if we don't
annotate our usableFromInline types.
Modifications
Annotate our usableFromInline types
Add some @Sendable
Results
The march continues
### Motivation:
Explicit Sendable requires a bunch of annotations in SelectorGeneric, as
many of these types are `@usableFromInline`. This patch applies those.
### Modifications:
- Make many types Sendable
- Make some explicitly not Sendable
### Result:
No missing Sendable warnings
Motivation
SelectableEventLoop and MTEL are our major entry points for execution.
These need to be strict concurrency clean. Unfortunately, changes in one
tend to ripple into the other, so we need to tackle both at once.
Modifications
- A few closures get annotated @Sendable
- Add a NIOLockedValueBox to convince the compiler that this use is
safe.
- Make a few types explicitly not Sendable
Result
No concurrency warnings in MTELG and SelectableEventLoop.
Motivation
RawSocketBootstrap isn't strict concurrency clean yet, and it should be.
Modifications
Add a few necessary Sendable constraints.
Result
No warnings here boss.
### Motivation:
NonBlockingFileIO is now mostly in a good state, but there are some
missed Sendable annotations. Let's clean that up.
### Modifications:
Mark an inner function as `@Sendable`
Add `Sendable` to `NIODirectoryEntry`.
### Result:
The road continues
Motivation
Next on my journey of discovery is NIOThread and NIOThreadPool. These
are basically fine, but they need some Sendability annotations and a bit
of a cleanup.
Modifications
Add some Sendability
Clean up some shutdown logic to avoid semaphores.
Result
Another step forward to a strict-concurrency safe world.
### Motivation:
The HappyEyeballsResolver, being an old part of our stack, has a lot of
code in it that fails to pass strict concurrency checking. That's deeply
suboptimal.
### Modifications:
- Clean up the happy eyeballs resolver under strict concurrency
- Further cleanups to the bootstraps
### Result:
Another step taken on the road to strict concurrency.
Motivation:
As pointed out in #3071, the `flatScheduleTask` implementations can be
improved.
Modifications:
- Refactor the `flatScheduleTask` implementations to skip `flatMap`
calls, which avoids creating an extra promise.
- As there is now a lower number of allocations, reduce the necessary
thresholds for the allocation tests.
Result:
Reduction in the number of allocations in the package.
---------
Co-authored-by: George Barnett <gbarnett@apple.com>
### Motivation:
We recently added support for nightly 6.1 Swift and old scripts making
use of `check-matrix-job.sh` do not set `COMMAND_OVERRIDE_NIGHTLY_6_1`
which causes the script to fail. We should tolerate this.
### Modifications:
Add a default case for `COMMAND_OVERRIDE_NIGHTLY_6_1`.
### Result:
No failures if a caller doesn't specify `COMMAND_OVERRIDE_NIGHTLY_6_1`.
```
❯ SWIFT_VERSION=nightly-6.1 COMMAND="echo foo" COMMAND_OVERRIDE_5_9="" COMMAND_OVERRIDE_5_10="" COMMAND_OVERRIDE_6_0="" COMMAND_OVERRIDE_NIGHTLY_MAIN="" ./scripts/check-matrix-job.sh
** Running default command
foo
❯ SWIFT_VERSION=nightly-6.1 COMMAND="echo foo" COMMAND_OVERRIDE_5_9="" COMMAND_OVERRIDE_5_10="" COMMAND_OVERRIDE_6_0="" COMMAND_OVERRIDE_NIGHTLY_MAIN="" COMMAND_OVERRIDE_NIGHTLY_6_1="echo bar" ./scripts/check-matrix-job.sh
** Running nightly 6.1 command override
bar
```
### Motivation:
Some changes were missed in #3076, passing through parameters for the
new 6.1 version.
### Modifications:
Pass through more parameters, clean up misleading comments.
### Result:
6.1 nightly runs will be more like 6.0 nightly runs were
### Motivation:
Active development has moved to 6.1 so the Swift 6.0 nightlies no longer
deliver much value.
### Modifications:
* Deprecate 6.0 nightly CI configuration parameters
* Introduce new 6.1 nightly CI configuration parameters
* Update the matrix generation script to handle 6.1
### Result:
* No more nightly 6.0 CI, 6.1 nightlies are introduced.
* Configuration for 6.0 nightlies no longer has any effect.
* All repos are automatically opted-in to 6.1 nightly linux pipelines
**even if they opted out of 6.0**.
Motivation:
Users of SelectableEventLoop deserve to have their code that uses the
isolated event loop view perform at least as well as the code that
doesn't.
Modifications:
- Adopt the extra protocol witnesses.
- Refactor out common code.
Result:
Better performance
---------
Co-authored-by: George Barnett <gbarnett@apple.com>
Motivation:
NIOAsyncTestingEL is another place where we can reduce the overhead of
the isolated EL views. It's nice for the tests to run faster.
Modifications:
- Implement the new protocol witnesses for AsyncTestingEL
- Refactor common code for scheduleTask out
Result:
AsyncTestingEL is faster.
Motivation:
This patch takes advantage of the fast-path for isolated views on
EmbeddedEventLoop, reducing overhead when using EmbeddedEL.
Modifications:
- Implement the new witnesses in EmbeddedEL
Result:
EmbeddedEL is faster when using isolated views.
### Motivation:
As outlined in this
[issue](https://github.com/apple/swift-nio/issues/2930) by @weissi, the
current performance of calling `description` on `HTTPHeaders` is
undermined by the dynamism of Array.
### Modifications:
The proposed solution replaces the implementation of `description` to
iterate over the items and print them out manually. This provides a
faster solution since we bypass the cost of calling into `description`
of an Array.
### Result:
A more performant implementation of `HTTPHeaders` description.
---------
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
Motivation
I'm about to change all our main event loops to use our new
customisation points. That's great, but it'll have the effect of
removing all the testing of our default implementations, which is not a
good idea. So let's add some more tests.
Modifications
Define a "fallback" event loop for testing the isolated methods.
Result
Reduced likelihood of regressions in future.
Motivation
Right now the isolated view on the EventLoop has to implement its
interface in terms of UnsafeTransfer, because there are no non-Sendable
operations it can use.
That hurts us because we have to allocate an extra closure for all these
operations, making them slower than they need to be. This is unavoidable
in some contexts, but most EventLoops would be able to offer a fast-path
that avoids this extra allocation.
To achieve that, we need to move this logic into a customisation point
on the EventLoop protocol. Our fallback default implementation can be
the same as we do today.
Modifications
- Add customisation points for EventLoop for each of its operations that
has a protocol witness already.
- Add default implementations of these, using the implementation from
EventLoop.Isolated.
Results
No behavioural changes, but the code has moved.
---------
Co-authored-by: George Barnett <gbarnett@apple.com>
### Motivation:
Makes it possible to make integers out of ByteBuffers directly with an
initializer. I.e. `UInt32(buffer: ByteBuffer(bytes: [0, 1, 2, 3]))`.
Closes#3012.
### Modifications:
- Adds the `Int(buffer:)` initializer in `ByteBuffer-int.swift`
- Adds tests in `ByteBufferTest.swift`. _Holy hell this file is huge._
- Note that `Int(buffer:)` will crash if the buffer does not have enough
bytes in it to represent the desired integer type.
- It also does _not_ expose endianness and uses `Endianness.host`, to
keep the public API clean and simple. It's a convenience initializer, so
I thought keeping it minimal is a good idea.
### Result:
Nicer direct initializer for integers from ByteBuffers.
---------
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
Motivation:
We were only testing the isolated views on SelectableEventLoop. That was
fine when all the implementation was common, but it isn't going to be
anymore, so we should start having tests across all our EL kinds.
Modifications:
Refactor the tests to take their EL as an argument
Add support for ELs that need to be explicitly run
Add tests for NIOAsyncTestingEL and EmbeddedEL
Result:
Better test coverage.
Motivation:
As with the ELF operations before them, the isolated EL operations
currently incur overhead above-and-beyond the overhead of their
non-isolated counterparts. That's not what we want to see. However,
before we "fix" them, we need to add regression testing to confirm our
fix actually worked.
Modifications:
- Add isolated variations of all current EL scheduling tests.
- Where isolated methods didn't have alloc counter tests, add new ones
for the non-isolated versions too.
Result:
More tests.
Adds `TimeAmount.init(string:)` and `TimeAmount.description` for parsing
time amounts from strings and pretty printing them.
Closes#2504.
### Motivation:
Had a minute, wanted to work on Swift-NIO a bit more, and saw @weissi
still wanted this.
### Modifications:
It's largely based on the snippet @weissi made in #2504 with a few
changes:
- Added unit tests.
- Added support for more unit aliases for convenience based on
@glbrntt's suggestion.
- Changed `.gitignore` to ignore `.build` everywhere, I've got a few of
them when testing locally.
### Open questions:
- I originally thought perhaps I should add support for multiple number
and unit pairs, i.e. `1h 31m`, but decided against it. Feels like an
edge case. Happy to add this if you think it's needed.
---------
Co-authored-by: Cory Benfield <lukasa@apple.com>
### Motivation:
Makes it possible to read contents of an NIOFileSystem.FilePath into a
String directly. Closes#3010.
### Modifications:
- Added a new `String+FileSystem.swift` into `NIOFileSystem` target.
- Added basic tests to the existing `ConvenienceTests.swift`.
- Added a test to cover `Array+FileSystem` initializer as well.
### Result:
- String initializer for `FilePath` is now available
- Array initializer for `FilePath` now has a test
---------
Co-authored-by: George Barnett <gbarnett@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
Enhance `WebSocketProtocolErrorHandler` to correctly add masking key for
client/server.
### Motivation:
In `NIOWebSocket`, the automatic error handling provided by
`WebSocketProtocolErrorHandler` offers a convenient way for both clients
and servers to handle protocol-level errors. However, the
`WebSocketFrame` used in `WebSocketProtocolErrorHandler` does not fully
adhere to the RFC 6455 standard. Specifically, as per [RFC 6455 Section
5.1](https://datatracker.ietf.org/doc/html/rfc6455#section-5.1), a
client *must* mask all frames it sends to the server, while a server
*must not* mask any frames it sends to the client. This PR addressed
this discrepancy to ensure compliance with the WebSocket protocol.
### Modifications:
In the `WebSocketProtocolErrorHandler` initializer, the user can specify
whether the handler is used for a WebSocket client or server. Within the
`errorCaught` method, the `WebSocketFrame` will include a maskingKey if
the handler is used by a client, or nil if it is used by a server.
Additionally, the `@unchecked` annotation is removed from
`NIOWebSocketServerUpgrader` since the project is now using swift 5.9
toolchain.
### Result:
The `WebSocketProtocolErrorHandler` is more robust and can correctly
close the connection is error occurs.
Motivation
We have a few types lying around that can trivially be Sendable, and
that `-require-explicit-sendable` needs to be `Sendable`. Let's add
those annotations.
Modifications
Made a few types Sendable
Result
One step closer to our strict concurrency goal
### Motivation:
The bootstraps are the next most obvious target for cleaning up
Sendability issues. These issues are mostly just missing annotations,
but there are a few places where we had actual latent threading bugs
that were missed. A lot more of the control flow is made more explicit
in this patch, and in general it should get a lot easier to be confident
that the code is correct.
### Modifications:
- Add necessary `@Sendable` annotations
- Clean up some incorrect `self` captures of bootstraps, which are not
`Sendable`, and which could lead to real threading bugs
- Make a few methods `static` to avoid needing to capture `self` at all.
- Make a few threading assumptions clear by using the isolated views or
the sync operations
- Add some missing `Sendable` constraints on interior generic functions.
### Result:
Better safety, better correctness in the bootstraps.
Motivation:
Swift 6.1 nightlies have added many new warnings and errors to NIO. This
patch resolves all of them.
Modifications:
- Clean up missing imports
- Move away from @_implementationOnly where necessary
- Fix a few things where Sendable mismatches were present.
Result:
Clean compile again
---------
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
This fixes hitting the following preconditionFailure in NIOAsyncWriter:
"This should have already been handled by `yield()`".
It doesn't expect a yield to be suspended when the state is
`.writerFinished`, but this can definitely happen. This seemed to be the
correct solution to me, but please check it carefully, because I'm
unfamiliar with this code.
I'm not happy about the test for this. It's blocking the `didYield` call
for a while, to make sure the correct state is reached. See also the
'FIXME' line. What would be a better way to do this?
---------
Co-authored-by: Cory Benfield <lukasa@apple.com>