Commit Graph
2945 Commits
Author SHA1 Message Date
Peter Adams 496d697df7 Don't use deadbeef for dead pointers. (#1604)
Motivation:

It turns out using this overflows Int on "Any iOS Device"

Modifications:

Switch the first d to a 7.

Result:

Code will compile on "Any iOS Device".
2.20.1
2020-07-30 18:27:26 +01:00
Peter Adams 3cfb3f1d6e Don't install Jazzy on Xenial (#1597)
Motivation:

Ruby on Xenial is too old for the cocoapods downloader which jazzy uses.

Modifications:

Change Dockerfile to not install Jazzy on xenial.

Result:

Dockerfile will build again, but bionic or later is required for docs.
2.20.0
2020-07-21 17:52:23 +01:00
Peter Adams 86ce6aae8b Explicit Congestion Notification for UDP (#1596)
Motivation:

Network congestion can be controlled without packet loss if explicit congestion notification messages are understood.

Modifications:

Receive single and vector paths to get notification and surface.
Send paths for both single and vector sends.
Round trip tests.
Storage for control messages added in a reusable way.

I have tested allocations and they seem to be fine but the allocation tests for vector read and write were not stable enough to sensibly add to CI.

Result:

Explicit Congestion Notifications can be both sent and received.
2020-07-17 16:25:09 +01:00
Peter Adams 74878a0235 Setup allocation test to send and receive ECN data. (#1594)
Motivation:

It's possible relaying ECN information will cause allocations.
Having a test which uses them will ensure this doesn't happen.

Modifications:

Added metadata data to always send ECN state and set a channel
option requesting to recieve it.

Result:

Allocation test will attempt to send and receive ECN data.
2020-07-14 18:02:47 +01:00
Peter Adams 1e2d2b997b Plumb metadata through PendingDatagramWritesManager. (#1593)
Motivation:

ECN information is held in metadata.
As part of the path to writing ECN to the network the
required state needs to pass through PendingDatagramWritesManager.

Modifications:

Add an extra metadata field to PendingDatagramWrite and ensure
this data is passed to the correct write method.

Result:

Nothing visible - just preparation for actual ECN send.
2020-07-14 17:42:23 +01:00
Peter Adams 723ff6acf1 Allow Metadata to be constructed and set on AddressedEnvelope (#1591)
Motivation:

This needs to be possible so users can specify what
ECN status they wish to send.  The auto-generated
initialiser is internal.

Modifications:

Add public initialiser to AddressedEnvelope.Metadata
Add public initialiser to AddressedEnvelope taking Metadata

Result:

Users will be able to construct AddressedEnvelopes with Metadata set.
2020-07-14 15:51:07 +01:00
Peter Adams f12803dbf1 Add methods to create and parse cmsghdr structured data. (#1590)
Motivation:

cmsghdrs can be used to send an receive extra data on UDP packets.
For example ECN data.

Modifications:

Map in Linux and Darwin versions of cmsghdr macros as functions.
Create strctures for holding a received collection; parsing ecn
data from a received collection and building a collection suitable
for sending.

Result:

Functions to manipulate cmsghdr and data exist.
2020-07-14 12:29:31 +01:00
Peter Adams ee8acd9b3d Use sendmsg rather than sendto to send UDP data. (#1587)
Motivation:

sendmsg is more powerful - this is paving the way for
Explicit Congestion Notifications to be sent.

Modifications:

Replace the sendto system call with sendmsg up to DatagramChannel.

Result:

Sendmsg will be used to send UDP data.
2020-07-10 13:41:13 +01:00
Cory Benfield 97450139d5 Improve pointer hygeine in SocketAddresses.swift (#1588)
Motivation:

The socket address types are some of the worst types for handling in
Swift. They pervasively type-pun in a way that Swift strongly wants to
resist, and they expose a bunch of data through awkward fixed-length
arrays.

We've done a number of passes over this code to improve its pointer
hygeine. This is another one. Here, we remove some uses of the memory
binding APIs that were unnecessary, and that can instead take advantage
of the way Swift handles binding memory of homogeneous tuple types.

In Swift, whenever we have a homogeneous tuple (i.e. a tuple whose
elements are all the same type), that memory is actually bound to _two_
types: to the tuple type, and to the type of the elements. This allows
us to use `assumingMemoryBound(to:)` to convert between the pointer to
the tuple and the pointer to the first element of the tuple.

This knowledge lets us remove calls to `bindMemory` and
`withMemoryRebound`. Neither call is correct to use in this situation,
and while we were unlikely to encounter any damage, this is the best
possible use of the Swift pointer APIs for this work.

Modifications:

- Rewrite accesses to the UNIX domain socket path to use appropriately
  typed pointers without binding or rebinding memory.
- Added some assertions about the static types of the stored elements.

Result:

Better management of pointer type bindings.
2020-07-10 08:14:29 +01:00
Cory Benfield e3508b0d04 Move DatagramChannel over to using recvmsg. (#1473)
Motivation:

When attempting to obtain metadata about a read, we need to use recvmsg
in order to obtain that data. While we're using recvmmsg for vector
reads right now, our scalar reads use recvfrom, which does not provide
us with that metadata.

While we're not extracting metadata right now, we may well do so in
future, so we can apply it now.

Modifications:

- Move recvfrom usage to recvmsg.

Result:

We'll be able to extend recvmsg to extract metadata.
2020-07-08 09:10:32 +01:00
Saleem Abdulrasool 5de1e41310 NIO: Implement BSD sockets APIs for Windows (#1471)
Motivation:

Split out the internal implied BSD socket API code to allow alternative implementations of these interfaces on other platforms.

Modifications:

- Split the code apart.
- Provide POSIX and Windows implementations of the BSD socket API.

Result:

Should be easier to run NIO unmodified on Windows.
2020-07-03 10:14:36 +01:00
Luo Xiu 8aae654645 fix incorrect case when removing an element from a circular buffer (#1584) 2020-07-01 10:13:44 +01:00
Peter Adams 8a865bd15e Make UDP connection allocation test more like best practice code. (#1583)
Motivation:

There is at least a theoretical race to flush before close in prior version.
Having terrible code in the NIO repo is asking for someone to copy it.

Modifications:

flatMap the various parts of the client together which also ensures the
flush is complete before close is called.

Result:

Slightly nicer code, slightly fewer allocations.
2.19.0
2020-06-30 17:11:05 +01:00
Gwynne Raskind 25db3ea68b Correct the behavior comment for the Windows implementation of Lock.deinit. Add a Windows-specific note to Lock's documentation comment. Trim spurious semicolons appearing in Lock.init(). (#1582) 2020-06-30 08:36:05 +01:00
Peter Adams a035a1b154 UDP allocation tests should not use so_reuseaddr (#1581)
Motivation:

so_reuseaddr on linux allows multiple binding to the same port simultaneously - this has not been seen to happen in practice but it is best to be cautious.

Modifications:

Remove address reuse from the UDP allocation tests.

Result:

Reduced danger of binding the same port twice.
2020-06-29 16:51:42 +01:00
Peter Adams 16b9dd6a61 Stop udp connection allocation test hanging on linux. (#1578)
Motivation:

Hanging tests make doing work and asserting correctness very difficult.

Modifications:

Remove address reuse from the server side of the udp channel.

Result:

The tests no longer hang.

(The reason things we hanging was that the client side was occasionally getting assigned the same port number as the server side (extremely agressive port reuse)
2020-06-29 12:42:48 +01:00
Johannes WeissandCory Benfield 3a2fc0d39b alloc tests: implement malloc_zone_* (#1569)
Motivation:

The Swift runtime is now using malloc_zone_*, we need to implement
replacements for these too. This is just a first pass, eventually, we
should implement _all_ replacements as `malloc_zone_memalign` which is
powerful enough to implement all others.

Modifications:

Provide new replacements.

Result:

Alloc tests work again on macOS.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-06-26 11:04:37 +01:00
Gregor Milos (Grzegorz Miłoś) 3c5ea83a9a Fix RepeatedTask scheduling logic to run on the correct EL (#1567)
Motivation:

Fixes: #1334

Modifications:

ELF hops on the correct EL before executing.

Result:

Fixes precondition failure, caused by being on the wrong ELG.
2020-06-26 09:19:06 +01:00
David Evans 6bba46fb90 Fix non-public init (#1573) 2020-06-25 13:07:18 +01:00
Cory Benfield 30961d2f05 Properly allow ThreadPool to be started multiple times. (#1572)
Motivation:

Our API documentation claims that NIOThreadPool.start() will tolerate
being called multiple times. However, this wasn't actually true: in
debug mode it'd hit an assertion, and in release mode it would quietly
orphan all its threads and replace them with new ones, leaving there
with twice as many threads and some that wouldn't be closed down.

We should support the functionality we claim we do.

Modifications:

- Fixed the incorrect check for running thread pool state.
- Added a test to validate we actually allow multiple starts.

Result:

Thread pools really can be started multiple times.
2020-06-25 11:14:43 +01:00
669a19cb7b Set SO_LINGER in integration tests. (#1549)
Motivation:

We now have sufficiently many integration tests that the number of TCP
sockets in TIME_WAIT begins to cause resource pressure on the system.
This can lead to the integration tests failing due to being unable to
assign a socket.

We can force this to not happen by disabling linger. As we're running on
a lossless fabric (localhost), this is entirely safe.

Modifications:

- Disable TIME_WAIT for integration tests.

Result:

Integration tests will pass.

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
Co-authored-by: Peter Adams <pp_adams@apple.com>
2020-06-24 14:28:40 +01:00
George Barnett 8a25c0f5a0 Make TimeAmount Hashable (#1571)
Motivation:

TimeAmount wasn't `Hashable` and there's no clear reason it shouldn't
be.

Modifications:

Add synthesized `Hashable` conformance to `TimeAmount`

Result:

`TimeAmount` is hashable.
2020-06-24 12:31:05 +01:00
Johannes Weiss 01e0e0cef4 stablise 1000 TCP connections alloc test (#1566)
Motivation:

The 1000 TCP connections allocation counter test did way too many thread
crosses and other things that allocated for unrelated reasons.

Modifications:

Remove those.

Result:

Much more stable
2020-06-23 13:11:43 +01:00
Peter Adams c03dd9c5f1 Make allocation tests produce more stable results (#1565)
Motivation:

Allocation tests are very hard to use productively if they don't produce stable results.

Modifications:

Change TCP and UDP connection tests to monitor on the server side as well as the client side to make sure all data is completely send and received before stopping counting allocations.

Result:

Allocation tests are more stable than before.
2020-06-23 10:25:36 +01:00
George Barnett 7100980bf7 Update supported version in README (#1517)
* Update supported version in README

Motivation:

NIO can run on macOS 10.9+/iOS 7+, but the README states 10.12+.

Modifications:

Update readme.

Result:

More accurate docs.
2020-06-18 18:05:42 +01:00
Johannes Weiss d371bb5a10 fix SAL non-determinism (#1562)
Motivation:

The SAL tests suffered from a rare non-determinism: When we go onto the
EventLoop to get a value and then leave it, we previously just assumed
that the EventLoop would park itself before we did any futher operations
on it.

That was mostly true because the tests are doing one thing after the
other. There was however a narrow race: If the tests enqueued new work
onto the EventLoop before the EventLoop parked itself, then the new work
would be run _before_ the EL parks itself.

That would make the _next_ operation fail because the operations usually
start by checking that we're parked because that's a synchronisation
point between tests and EL.

Modifications:

Actually wait until the EL's parked. That proves we found our
synchronisation point.

Result:

- fixes #1514
- fixes #1545
- fixes #1548
- fixes #1550
2020-06-18 18:01:30 +01:00
Johannes Weiss 6046aaa629 forward #file to #filePath differently (#1560)
Motivation:

only works if done when _calling_ a function, not when defining one :|.

- https://github.com/apple/swift/pull/32445
- https://bugs.swift.org/browse/SR-12936
- https://bugs.swift.org/browse/SR-12934
- https://bugs.swift.org/browse/SR-13041

Modifications:

Silence #file to #filePath differently.

Result:

Hopefully at some point we get this working.
2020-06-18 15:12:11 +01:00
George Barnett 7d2e632da2 Execute tasks scheduled at the same time in order on EmbeddedEventLoop (#1540)
Motivation:

`EmbeddedEventLoop` executes scheduled tasks in their run-time order.
If multiple tasks are scheduled for the same time then the order in
which they are eventually executed is undefined.

Since `execute` defers to scheduling a task for "now" it is possible for
tasks to be executed out of order if `run()` is not called between each
execute.

In "real" event loops this isn't an issue because the system time must
advance between each call to `execute`.

Modifications:

- Add a 'insertionOrder' to `EmbeddedScheduledTask`; use it to break
  ties when comparing two tasks with the same run time
- Add a task counter to `EmbeddedEventLoop` which is used to provide an
  insertion order when scheduling tasks.

Result:

- EmbeddedEventLoop will `execute` tasks in order
2020-06-17 10:56:32 +01:00
George Barnett 5739d4e6f1 Allow EmbeddedEventLoop to advanceTime(to:) a deadline (#1557)
Motivation:

You can schedule tasks for a deadline on `EmbeddedEventLoop` but have no
way of knowing the current time on the `EventLoop` and how much to
advance the time by in order to meet that deadline. In most cases this
is okay because you know that time starts at zero and you can keep track
of time, however, it's generally useful to not have to keep track of
this yourself.

Modifications:

- Add `advanceTime(to:)` on `EmbeddedEventLoop`
- Document that time may be controlled on `EmbeddedEventLoop` and how to
  advance it
- Tests

Result:

- `EmbeddedEventLoop` is more usable with deadlines
- Better documentation
2020-06-16 18:00:02 +01:00
Peter Adams 9f732b2382 Shorthand way of setting options in universal bootstrap (#1547)
Motivation:

Guide novice users towards the options they are most likely to want to use.

Modifications:

New type to represent shorthand TCP client options containing only the options
which are sensible for novice users.
New type to represent a collection of these options.
A method to allow underlying bootstraps to override universal behaviour - default
implementation is to do nothing.

A test case to demonstrate that shorthand options match old style options.
A test case to demonstrate that overriding option behaviour is possible.

Result:

For universal botostrap there will be an new way of setting options involving
less typing which guides the user towards the common options.
2020-06-15 17:24:20 +01:00
Johannes Weiss 7b93c22836 HTTP: assert that user doesn't set CL & TE:chunked (#1556)
Motivation:

NIO users need to understand the semantic of the underlying protocol.
Therefore NIO does not (and in many case cannot) check all the
invariants. Generally however, we try to be helpful and assert a bunch
of things that a user has (hopefully) checked. That doesn't affect
performance and simplifies development for users.

Modifications:

- assert that request/response heads don't set content-length +
  transfer-encoding: chunked

Result:

Developers of libraries like AHC will have an easier time.
2020-06-15 12:36:19 +01:00
Johannes Weiss 5268726898 fix inappropriate lanugage in NIO (#1554)
Motivation:

It's 2020, we shouldn't use unnecessary offensive, insulting, or
outdated language.

Modifications:

Fix a few examples.

Result:

More inclusive.
2020-06-12 19:34:05 +01:00
Jake PrickettandPrickett c05eeb40ee Add synchronous option to trunk push (#1551)
Co-authored-by: Prickett <jpricke1@ford.com>
2020-06-09 14:51:21 +01:00
Johannes Weiss 120acb15c3 ClientBootstrap: allow binding sockets (#1490) 2.18.0 2020-06-08 10:46:20 +01:00
5fc487bf7e don't blow up if schedule happens during EL shutdown (#1530)
Motivation:

Previously, if we scheduled EventLoop executions from the promises we
failed during EventLoop shutdown, we'd blow up. Thanks to @fabianfett
for the bug report.

Modifications:

Set the EL state to 'exiting thread' only after we failed all
outstanding tasks.

Result:

Fewer crashes.

Co-authored-by: George Barnett <gbarnett@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-06-08 08:10:12 +01:00
Johannes Weiss f259e7d48f workaround SR-12939 (#1546) 2020-06-08 07:37:17 +01:00
Johannes WeissandGeorge Barnett adeafa9ce1 remove fullFilePath() hack (#1544)
Motivation:

The fullFilePath() hack doesn't actually work
(https://bugs.swift.org/browse/SR-12934). This is expected behaviour so
currently, the only workaround would be to `#if` whole function bodies
which is a lot of work. Given that in Swift 5.3 `#file` == `#filePath`
still, we can just silence the warning for now.

Modifications:

- Replace `fullFilePath()` by `(#file)`.

Result:

- file locations working properly again.

Co-authored-by: George Barnett <gbarnett@apple.com>
2020-06-05 11:13:09 +01:00
Johannes WeissandCory Benfield e57122bd89 ByteBuffer: add convenience initialisers (#1533)
Motivation:

There are multiple sub-optimal ByteBuffer creation patterns that occur
in the wild. Most often they happen when people don't actually have
access to a `Channel` just want to "convert" a `String` into a
`ByteBuffer`. To do this, they are forced to type

    var buffer = ByteBufferAllocator().buffer(capacity: string.utf8.count)
    buffer.writeString(string)

Sometimes, they don't get the capacity calculation right or just put a
`0`.

Similar problems happen if NIO users want to cache a ByteBuffer in their
`ChannelHandler`. You will then find this code:

```swift
if self.buffer == nil {
    self.buffer = receivedBuffer
} else {
    var receivedBuffer = receivedBuffer
    self.buffer!.writeBuffer(&receivedBuffer)
}
```

And lastly, sometimes people want to append one `ByteBuffer` to another
without mutating the appendee. That's also cumbersome because we only
support a mutable version of `writeBuffer`.

Modifications:

- add `ByteBuffer` convenience initialisers
- add convenience `writeBuffer` methods to `Optional<ByteBuffer>`
- add `writeBufferImmutable` which doesn't mutate the appendee.

Result:

More convenience.

Co-authored-by: Cory Benfield <lukasa@apple.com>

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-06-04 21:02:11 +01:00
Johannes Weiss de606a62f2 PipeBootstrap: fix file type change & accept 1 fd (#1543)
Motivation:

Sometimes it can be useful to use NIOPipeBootstrap with a single file
descriptor, like a serial line. Previously however we would only accept
two distinct fds (without checking they're distinct...).

Also, we didn't correctly check file types.

Modifications:

- also support NIOPipeChannel for streams that have just 1 fd
- fix file type check
- check that the 2 fds are distinct in withPipe(fds...)

Result:

- more functionality
- fewer bugs
2020-06-03 17:48:51 +01:00
Peter AdamsandCory Benfield 4851816b65 Channel option to enable ECN reporting (#1506)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-06-02 16:36:17 +01:00
Johannes WeissandCory Benfield def2f0efe9 remove XCTest expectations with timeouts (#1539)
Motivation:

The XCTest expectations caused problems when they time out. When they
time out, the execution continues which in the tests in question then
shut down the EventLoop. Shutting down the event loop meant that other
things don't happen which caused the test to hang. The XCTest
expectations however tell us about the failure at the end of the test
(which we never reach).

Modifications:

Remove the XCTest expectations with timeouts.

Result:

Even slow CI should pass this test correctly.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-06-02 14:48:09 +01:00
Johannes Weiss 8b05ba64b1 silence the #file used instead of #filepath warning (#1526)
Motivation:

Very recent versions of the Swift compiler complain if you pass `#file`
to a parameter that is defaulted to `#filePath`. You can silence the
warning by using `(#file)`. We cannot migrate to `#filepath` because we
support Swift 5.0 and 5.1 which don't have `#filepath` yet.

Modifications:

- replace all occurances of `#file` by a special (test `internal`)
  function `fullFilePath()` which gives you the full path and is defined
  differently depending on the compiler version.

Result:

No warnings even on the latest compiler versions.
2020-06-02 11:56:42 +01:00
Johannes Weiss 2450ba55e5 crash tester test suite (#1536)
Motivation:

It's often very important to test that in certain cases we actually
crash with a certain error message whilst having access to all of NIO.

Modifications:

Add a crash tester test suite.

Result:

We can test crashers.
2020-06-01 12:51:39 +01:00
Peter Adams 1d134ccdcc Fine grained UDP allocation tests (#1534)
Fine grained UDP allocation tests

Motivation:

The existing UDP allocation tests count allocations everywhere.

Split out 3 tests to cover:

Making connections.
Making bootstraps.
Transferring data.
Modifications:

Three new tests.

Result:

Easier to debug changes to allocation profiles in UDP.
2020-05-29 12:49:34 +01:00
Cory Benfield f72357e773 Make SocketAddress Hashable. (#1532)
Motivation:

In general there's no good justification for `SocketAddress` not to be
`Hashable`: it's a well-formed value type that can easily have a
clearly-defined version of hashability defined. In particular, we have
a clear definition of equatability for this type already present in the
code.

This makes SocketAddress more broadly useful in a range of
circumstances.

Modifications:

- Defined `Hashable` for `SocketAddress`.
- Fixed broken `Equatable` implementation for `SocketAddress`.

Result:

Users can obtain the hash value of `SocketAddress`.
2020-05-29 10:38:11 +01:00
Peter Adams 51370e4113 Finer grained allocation tests (#1531)
Motivation:

The existing tcp allocation tests cover everything from bootstrap creation through to sending data in one massive test. The parameters of how many iterations control the focus of the test. It would be better to be more explicit about what we're trying to test.

Modifications:

2 new tests:
   * client bootstrap creation.
   * connection establishment.
 (Data transfer is already covered by the ping_pong test)

Result:

Easier to assess the impact of changes to memory allocation patterns.
2020-05-28 16:17:20 +01:00
Johannes Weiss 1744c6b363 thread takeover: test it unsets current EL (#1529)
Motivation:

I thought I forgot to implement unsetting the current EventLoop in the
thread takeover (withCurrentThreadAsEventLoop). Turns out I didn't.

Modifications:

Add a test to prove it's okay.

Result:

More test coverage.
2020-05-27 15:35:54 +01:00
Johannes Weiss c1d86289ed HTTPEncoder: fix 0 length chunks (#1524)
Motivation:

Previously, if the user sent us a 0 length chunk, we would encode it as
`0\r\n\r\n` which means the _end_ of the body in chunked encoding. But
the end in NIO is send by sending `.end(...)`.

Modifications:

Don't write anything for 0 length writes.

Result:

More correct behaviour with 0 length body chunks.
2020-05-18 18:50:43 +01:00
Peter AdamsandCory Benfield b174051b81 Supply allocation limits for UDP allocation tests (#1523)
Motivation:

Values were previously missing (5.3) or incorrectly specified (5.1)
meaning that allocations were not checked in for these swift versions.

Modifications:

Correct variable name in 5.1, add missing variables in 5.3 docker files.

Result:

UDP allocations will be checked.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-05-18 08:34:56 +01:00
Peter Adams a3a8b3f799 Detail the best way to request a re-review (#1522)
Motivation:

It is unclear how to engage with the core team after changes have been requested.

Modifications:

Add a short note to the CONTRIBUTING guide.

Result:

Increased clarity on how to interact.
2020-05-18 08:04:01 +01:00