Commit Graph
2140 Commits
Author SHA1 Message Date
Johannes Weiss 072babe443 ByteBuffer: deal correctly with underreporting ContiguousCollections (#501)
Motivation:

ContiguousCollections shouldn't give access to more than `count` bytes
via `withUnsafeBytes` but if they do we should still handle this
correctly and only copy `count` bytes.

Modifications:

- copy an upper limit of `count` bytes
- fail hard if `withUnsafeBytes` offers fewer bytes as we can't do
  anything sensible then.

Result:

even illegal `ContiguousCollection` implementations don't cause issues.
2018-07-12 11:56:40 +01:00
Esteban Torres e9aa6926ef Make CircularBuffer conform to more protocols [Fixes #350] (#466)
Motivation:

During the `PR` apple/swift-nio#347 there was a discussion around which
protocols this autoexpanding buffer should conform; the following 3
being the winners:

1. BidirectionalCollection
2. RandomAccessCollection
3. RangeReplaceableCollection

Modifications:

Added conformance to the 3 protocolos and provided implementaiton for 4
methods:

1. replaceSubrange(:with:)
2. index(before:)
3. removeSubrange(:)
4. remove(at:)
5. removeLast(:)

Result:

With the conformance of this 3 protocols and the implementation of this
5 functions we get dropLast(), dropLast(:), removeFirst(), removeLast()
& popLast() for free.
2018-07-12 07:58:42 +01:00
Franz Busch c2c008e53f Repeated Scheduling (#488)
Provide a mechanism to repeatedly schedule a task in the EventLoop

Motivation:
In some cases a certain task should run repeatedly, like polling an API or querying a database. For these scenarios a repeated scheduling mechanism is beneficial.

Modifications:
Added a method to EventLoop to schedule repeated tasks. 

Result:
It is possible to schedule task with an initial delay and a fixed delay now. Furthermore, these task can be cancelled.
2018-07-10 14:59:35 +01:00
Johannes Weiss ee0c74e9e2 document EmbeddedChannel (#464)
Motivation:

We had no EmbeddedChannel documentation.

Modifications:

Add EmbeddedChannel documentation.

Result:

Less lacking documentation.
2018-07-05 19:18:00 +01:00
Johannes Weiss 6848e7b8d7 ByteBufferAllocator: switch to size_t where appropriate (#503)
Motivation:

In a few situations we used `Int` in places where malloc/realloc
actually use `size_t`. On 64-bit platforms this doesn't actually matter
but on 32-bit it does!

Modifications:

- replace `Int` with `size_t` where appropriate

Result:

- should be able to use more than 2GB on 32-bit platforms
2018-07-05 09:00:42 +01:00
Lev Walkin 5347ab3e9f use a standard way to determine processor count (#496) 2018-07-03 11:45:19 +01:00
Johannes Weiss 89d46464ec workaround for Swift 4.2 compatibility issue with 4.0 (#494)
Motivation:

In Swift 4.2 you'll get a warning if you use an internal typealias from an
`@inlinable`/`@_inlineable` function. To fix it, you usually make it
`@usableFromInline`/`@_versioned`. Swift
4.0 however doesn't support those attributes for typealiases. The workaround is
to just make the typealias public (and underscored).

Modifications:

make `ByteBuffer.Capacity` and `ByteBuffer.Index` underscored and public

Result:

compiles warning-free on Swift 4.0, 4.1 and 4.2
2018-06-26 16:29:33 +01:00
Esteban Torres 29d6bf029a Nil NIOHTTP1Server dynamic handlers (#468)
Motivation:

The server attaches a dynamic handler to requests and will keep passing
data until the handlers are removed; which never happens.

Modifications:

`nil` out the `handler` when `completeResponse` is called.

Result:

When connections are reused we now get the correct dynamic handler for
it instead of the previous one.
2018-06-12 10:23:51 -07:00
Norman Maurer a67e85677a Allow to create ByteBufferView of all bytes in a ByteBuffer (#485)
Motivation:

We used an incorrect precondition and so it would be hit if someone tried to create a ByteBufferView add all bytes in a ByteBuffer.

Modifications:

Allow upperBound up to capacity

Result:

Fixes https://github.com/apple/swift-nio/issues/482.
2018-06-12 08:12:27 -07:00
Johannes Weiss 8fd397b3d4 fix ASCII art in ChannelPipeline docs (#462)
Motivation:

The ASCII art in the ChannelPipeline docs was slightly broken, this
fixes it.

Modifications:

fix ASCII art

Result:

less broken ASCII art
2018-06-09 19:41:01 -07:00
Cory Benfield e6787d3714 Update http_parser (#471)
Motivation:

Our copy of http_parser has become almost a year old now, and there are
both some bugfixes that improve correctness and some performance
improvements in http_parser that we should pick up.

Modifications:

- Updated http_parser
- Added tests to confirm we don't suffer from
    https://github.com/nodejs/http-parser/pull/432
- Added tests that we didn't get broken by SOURCE verb addition.

Result:

Shiny new http_parser!
2018-06-09 18:18:24 -07:00
OleG 58c983c049 Fix receiving message from websocket client (#472)
Motivation:

The http handler was not removed after the websocket upgrade and so a crash could happen as the handler assumes it received HTTP data.


Modification:

Remove httpHandler from pipeline

Result:

Fix receiving message from websocket client
2018-06-06 09:57:13 -07:00
Johannes Weiss d78ae0beed improve BlockingIOThreadPool docs (#461)
Motivation:

The BlockingIOThreadPool were very sparse which is improved here.

Modifications:

The existing docs didn't really help much if you didn't already know why
you'd need an extra thread pool for blocking work.

Result:

docs more clear on when to use BlockingIOThreadPool
2018-06-03 10:58:27 +01:00
Johannes Weiss 3c35c076da add scheme to URLs in NonBlockingFileIO docs (#460)
Motivation:

In the NonBlockingFileIO, the docs contain links but the scheme was
missing so the links didn't work.
Note: Both URLs don't work with https:// so I had to use http://,
sorry...

Modifications:

added a scheme to the URLs so they actually work

Result:

dead links in NonBlockingFileIO fixed
2018-06-03 10:32:13 +01:00
Cory Benfield 695afc5205 Widen assertion on readEOF. (#453)
Motivation:

In Darwin it is possible for readEOF to be received before writable,
if we race connection teardown with registering for writable. See
https://github.com/apple/swift-nio/issues/452 for a more in-depth
explanation.

Modifications:

Widened the assertion.

Result:

People's code won't go bang.
2018-05-30 14:35:10 +01:00
Johannes Weiss 402883a603 introduce ByteBufferView (#411)
Motivation:

Sometimes it would be handy to have ByteBuffers conform to a
`Collection` where `Element == UInt8`. ByteBuffer however has multiple
modes how to operate so it would be wrong to have say the readable bytes
be the ByteBuffer's collection.
Therefore it would be nice to have a view into a `ByteBuffer` which is a
`Collection where Element == UInt8`. This introduces these.

Modifications:

- add `ByteBufferView`
- add `ByteBuffer.readableBytesView`
- add `ByteBuffer.viewBytes(range: ...)`

Result:

- easier to work with `ByteBuffer`s in the Swift ecosystem.
2018-05-30 14:10:20 +01:00
Johannes Weiss eb15a4950b ByteBuffer: simplify ensureAvailableCapacity (#454)
Motivation:

ByteBuffer._ensureAvailableCapacity is quite complicated and the code
can be simplified by re-using existing code.

Modifications:

reuse more existing code

Result:

ByteBuffer code easier to understand
2018-05-30 12:52:34 +01:00
Johannes Weiss 25c6cd5982 make _UInt24 and _UInt56 printable (#445)
Motivation:

_UInt24 and _UInt56 didn't have a CustomStringConvertible conformance
which this patch is adding.

Modifications:

add CustomStringConvertible instances.

Result:

easier to debug stuff
2018-05-29 08:04:45 +01:00
Norman Maurer 82a6e4d821 Rename numThreads to numberOfThreads parameter. (#443)
Motivation:

We should be consistent with naming and also choose descriptive names.

Modifications:

- Deprecate old init method that uses numThreads
- Add new init with numberOfThreads param name
- Everywhere use the new init

Result:

More consistent and descriptive naming. Fixes https://github.com/apple/swift-nio/issues/432.
2018-05-25 17:37:55 +02:00
Andrew Edwards bc6618e300 Updated the code comment to explain the correct type of Future result.
Motivation:

This is done because when using Xcode and option + Click on a function you get a help overview inline to see more information and the messaging was incorrect. This helps developers in the future who want to rely on docs via Xcode.

Modification:

Correct the docs

Result:

More correct docs.
2018-05-24 21:02:32 +02:00
Norman Maurer c566b64455 Change initializer to public for HTTPRequestDecoder (#438)
Motivation:

d53ee6dafc introduced a new constructor to HTTPRequestDecoder which allows to change the behaviour of handling left over bytes when an upgrade was detected and the decoder was removed. This was done as an internal init block as we wanted to to do as part of a patch release.

Modifications:

Change internal to public init

Result:

More flexible configuration possible.
2018-05-24 14:08:32 +01:00
Cory Benfield 77dc77b9b5 Don't provide promises when we don't care about results. (#437)
Motivation:

When we don't care about the result of an operation, we shouldn't provide
a promise.

While I'm here I also removed an unnecessary close() call in the
AcceptHandler: the close() would be called again almost immediately.

Modifications:

Removed a close() call.
Set `promise: nil` on a pair of close() calls.

Result:

Fewer promise allocations.
2018-05-24 12:53:51 +01:00
Norman Maurer d53ee6dafc Only forward bytes on removal when we know we can handle it (#430)
Motivation:

398b950eee introduced a change which forwarded bytes that were buffered in HTTPDecoder after it was removed when an upgrade was detected. This may be risky if we dont know that the pipeline can handle it.

Modifications:

- Add new initializer that allow us to configure if we should forward bytes or not.
- Automatically use this if we know that we added an upgrader
- Add unit tests.

Result:

Less risky implementation.
2018-05-23 20:26:24 +02:00
Cory Benfield 9e0eb8347a Correctly account for writes of all datagrams. (#431)
Motivation:

The Darwin gathering datagram writes shim incorrectly placed the
"bytes written" amount into the first slot in the array for every write.
This had the effect of incorrectly accounting for the amount of bytes
written for literally every element of a vector write. Not good.

Modifications:

Correctly account for each vector write element.

Result:

Lower risk of preconditions in incorrect situations, better accounting
of written bytes.
2018-05-23 17:48:10 +01:00
Norman Maurer 398b950eee Ensure HTTPDecoder will not run into re-entrance issues and correctly forward bytes left after removal. (#427)
Motivation:

We need to ensure we correctly guard against re-entrancy for all cases. Also we did not correctly ensure we forward pending data after removal which could lead to missing data after upgrades.

Modifications:

- pause the parser when we need to callout to the pipeline and so ensure we never fire events throught the pipeline while in callbacks of http_parser
- correctly forward any pending bytes if an upgrade happened when the decoder is removed
- Ensure HTTPUpgradeHandler only remove decoder after the full request is received
- Add testcase provided by @weissi

Result:

Correctly handle upgrades and pending data. Fixes https://github.com/apple/swift-nio/issues/422.
2018-05-23 12:38:53 +01:00
Johannes Weiß 80363d9d8c fix EventLoops that Bootstrap channel initialiser's call out on (#424)
Motivation:

Previously we were not running the (child/server)channelInitializers on the
event loop associated to the `Channel` we're initialising. Almost all
operations in there are pipeline modifications which are thread safe so
it presumably wasn't a massive correctness issue. However it's very
expensive to hop threads that often and it is also very unexpected. This
addresses this issue.

Modifications:

made all (child/server)channelInitializers run on the event loop that is
associated to their `Channel`

Result:

- more correctness
- less unexpected behaviour
2018-05-22 15:58:43 +01:00
Johannes Weiss ec880b0853 don't just drop Channels on the floor if registration fails
Motivation:

If registration of a Channel fails we need to close that Channel.
Previously we'd just drop it on the floor which triggers an assertion
that an open channel got leaked.

Modifications:

In cases where the Channel registration fails, we need to close that
channel.

Result:

- No crashes if registration fails.
- fixes #417
2018-05-22 11:29:20 +01:00
Johannes Weiß f2343bfd95 retain Channel from ChannelPipeline until destroyed (#415)
Motivation:

A long time ago, `ChannelPipeline.channel` was public and needed to be
thread-safe. That meant we could never mutate `ChannelPipeline.channel`.
Instead we stored it as an unowned reference that we only read when the
`Channel` was still alive.

It was the right call making `ChannelPipeline.channel` internal but we
forgot to then remove the `unowned` reference from `ChannelPipeline` to
`Channel`. This patch makes this reference strong but nils it out
whenever the `ChannelPipeline` is destroyed. That makes everything
easier because there is no danger anymore that the `Channel` gets
deallocated when the `ChannelPipeline` is still alive.

After destroying the `ChannelPipeline` we will still hand out a
`DeadChannel` if you call `ctx.channel` (as it's a non-optional
requirement).

This comes with an unfortunate drawback: Now if you just leak an open
Channel (which you shouldn't be able to when using the Bootstraps) we
can no longer detect that as the Channel and the ChannelPipeline will
have a retain cycle. Unfortunately I don't think there's a good way to
safely assert that.

Modifications:

- made `ChannelPipeline` hold a strong reference to `Channel` until the
  `ChannelPipeline` is destroyed

Result:

- don't crash if the user only retains the ChannelPipeline but not the
  Channel
2018-05-22 11:01:00 +02:00
Johannes Weiß 6e73790951 explain why typealias is complicated (#423)
Motivation:

We should explain why this typealias

    public typealias RangeType<Bound> = CountableRange<Bound> where Bound: Strideable, Bound.Stride: SignedInteger

isn't just

    public typealias RangeType = CountableRange

and that's because SR-6963.

Modifications:

added a comment explaining why

Result:

code easier to read
2018-05-21 19:44:01 +01:00
Johannes Weiss 5aa140ca96 websockets: connection closed frame must be flushed
Motivation:

Because EmbeddedChannel used to treat all writes as flushed the test
that tested that we do send out the WebSockets connection close frame
didn't actually test the right thing. Now that EmbeddedChannel behaves
more realisticly, this test actually fails.

This fixes the bug and the test that tests that a WebSockets connection
is successfully torn down after an error occured.

Modifications:

- use `writeAndFlush` to send out the connection close frame.

Result:

WebSockets connection actually gets closed on error.
2018-05-21 16:05:17 +01:00
Johannes Weiss c3d02b8d47 EmbeddedChannel: make write not auto-flush
Motivation:

EmbeddedChannel just treated all `write`s as flushed. That's not a good
idea as all real channels obviously only actually write the bytes when
flushed. This changes that behaviour for `EmbeddedChannel`.

Modifications:

- make `write` only write to a buffer
- make `flush` then actually pretend to write the bytes

Result:

- EmbeddedChannel behaves more realisticly
2018-05-21 16:05:17 +01:00
Johannes Weiß 78e28e7d9c workaround SR-6963 (#420)
Motivation:

SR-6963 is currently blocking builds with Swift 4.2 pre-releases, we
should work around it.

Modifications:

work around SR-6963.

Result:

builds with 4.2 pre-release
2018-05-21 09:22:47 +01:00
Cory Benfield bad7c29742 Add ChannelCore.removeHandlers. (#408)
Motivation:

If ChannelPipeline.removeHandlers is internal it is extremely difficult
to implement a correct ChannelCore. For that reason, we should make it
available as an extension on ChannelCore.

This is a minor layering violation, but that's ok: ChannelCore and
Channel are not actually intended to be separate objects in most cases,
they are just separate for code clarity reasons.

Modifications:

Add ChannelCore.removeHandlers as a public method.

Result:

Easier to implement ChannelCore.
2018-05-18 10:48:39 +02:00
Johannes Weiß cea84765de Channel quiescing support (#399)
Motivation:

In certain cases it's useful to quiesce a channel instead of just
closing them immediately for example when receiving a signal.
This lays the groundwork by introducing the
`ChannelShouldQuiesceUserEvent` user event that when received can be
interpreted by a ChannelHandler in a protocol & application specific
way. Some protocols support tear down and that would be a good place to
initiate the tear down.

Modifications:

- introduce `ChannelShouldQuiesceUserEvent`
- handle `ChannelShouldQuiesceUserEvent` in the `AcceptHandler` with
  closing the server socket
- handle `ChannelShouldQuiesceUserEvent` in the
  `HTTPServerPipelineHandler` by only handling a already in-flight
  request and then no longer accepting input
- added `CircularBuffer.removeAll` (& tests)
- added tests for `nextPowerOf2()`

Result:

- handlers can now support quiescing
2018-05-18 10:48:18 +02:00
Johannes Weiß 6020265f13 fewer warnings for 4.2 in 4.2 mode (#407)
Motivation:

Minus [SR-7578](https://bugs.swift.org/browse/SR-7578) which will be
fixed soon, we should then compile warning-free in `-swift-version 4`
mode which is good.
However, we should also be as warning-free as possible in
`-swift-version 4.2`. This is not entirely possible because of
`@_inlineable` vs `@inlinable` but we can address the other warnings.

Modifications:

- make `CountableRange` vs `Range` (which used to be separate types but
  are now the same) work warning-free Swift 4.[01] and Swift 4.2

Result:

Fewer warnings.
2018-05-17 13:53:56 +01:00
Norman Maurer e029edef2a Do not allocate String to check if HTTP message is keep alive (#402)
Motivation:

We can check if a HTTP message is using keep alive without any allocation.

Modifications:

Directly act on the internal storage of HTTPHeaders to find out if keep alive is used or not.

Result:

Less allocations
2018-05-16 11:42:22 +01:00
Norman Maurer a7614d9180 Reuse detected keepalive of http_parser as long as possible (#299)
Motivation:

To detect if keepalive is used we need to search the headers (if HTTP/1.1 is used). This may be expensive depending how many headers are present. http_parser itself detects already if keep alive should be used and so we can re-use this as long as the user did not modify the headers.

Modifications:

Reuse keepalive parsed by http_parser as long as the headers were not modified.

Result:

Less overhead as long as the headers are not modified.
2018-05-14 18:04:52 +02:00
Johannes Weiß ec79bf2b0b add performance tester (#396)
Motivation:

NIO wants to be a highly performant library and therefore we need
performance tests.

Modifications:

Add a couple of performance tests.

Result:

Easier to talk about perf improvements/regressions if we use similar
benchmarks.
2018-05-11 07:22:24 +02:00
Alex Blewitt 1fa9f15aca Explicitly define constants through Posix and import errno.h (#400)
Explicitly define constants through Posix and import errno.h

Motivation:

We implicitly depend on errno's ENOENT in Linux.swift, but don't explicitly import errno.h.

Similarly, in some cases the compiler can be confused as to where SHUT_* come from, so make them an exported value from Posix instead.

Result:

Compiles successfully on Linux and Darwin.
2018-05-10 20:12:33 +02:00
Norman Maurer 4cbafde292 Add better guards again re-entrance in ByteToMessageDecoder (#370)
Motivation:

When the ByteToMessageDecoder re-entranced in its decode(...) / decodeLast(...) methods it could be possible that the ordering of processing and so the seen bytes are mixed.

Modifications:

- Refresh the buffer (and take a slice)  on each loop iteration and update the cumulationBuffers indicies.

Result:

More robust code.
2018-05-08 18:29:55 +02:00
Cory Benfield e9285c6bc8 Make hopTo(eventLoop:) public. (#395)
Motivation:

When this method was originally introduced it was also intended as a
helper for external users, but we never made it public.

Modifications:

Make hopTo(eventLoop:) public.

Result:

Users will have an easier time managing their threads.
2018-05-08 16:36:30 +01:00
Ludovic Dewailly 6fcbfc9b75 Minor typo: becuase -> because - issue#393 (#394) 2018-05-08 08:42:59 +02:00
Johannes Weiß 12cae95c64 remove assertion that String.UTF8View doesn't underestimate count (#392)
Motivation:

I left an assertion that String.UTF8View's underestimatedCount is equal
to the actual count of UTF-8 code units. It's good to see that it never
failed so far so we can expect it to be really good which is important
for performance.
However, there's no problem if it does actually underestimate maybe for
some special kind of Strings. So the assertion really isn't meaningful.

Modifications:

Remove the assertion.

Result:

Most likely nothing but we will not crash on something that we
shouldn't crash on.
2018-05-06 20:34:28 +02:00
Johannes Weiß a7ed5fd4e9 let the stdlib enumerate the Sequence elements (#391)
Motivation:

Very unfortunately [SR-7602](https://bugs.swift.org/browse/SR-7602)
Swift's String doesn't work very well on UTF-8 encoded data. Fortunately
(but unfairly see point (3) in
[SR-7602](https://bugs.swift.org/browse/SR-7602)) however there is now a
contiguous String storage for ASCII-only Strings that _only the stdlib_
can access. To benefit from the stdlib's capabilities of enumerating the
bytes faster than we can, we should use UnsafeMutableBufferPointer#initialize(from: Bytes).
Thanks @airspeedswift for pointing this out.

The performance gains on a recent Swift version are noticeable:

after this patch:

```
no-net_http1_10k_reqs_1_conn: 0.225782990455627, 0.228317022323608, 0.225206971168518, 0.22549307346344, 0.223728060722351, 0.225827932357788, 0.222437977790833, 0.221745014190674, 0.220004916191101, 0.222965002059937
http1_10k_reqs_1_conn: 0.688782095909119, 0.682757973670959, 0.689703941345215, 0.708670020103455, 0.691922068595886, 0.687381982803345, 0.681567072868347, 0.684866905212402, 0.683283090591431, 0.699701905250549
http1_10k_reqs_100_conns: 0.782313942909241, 0.779283046722412, 0.773630023002625, 0.776782989501953, 0.774596929550171, 0.775121927261353, 0.773631930351257, 0.784304022789001, 0.795854926109314, 0.786051988601685
```

before this patch:

```
no-net_http1_10k_reqs_1_conn: 0.232797980308533, 0.236240983009338, 0.237086057662964, 0.236070990562439, 0.236560940742493, 0.236254930496216, 0.234392046928406, 0.235393047332764, 0.234088897705078, 0.245710015296936
http1_10k_reqs_1_conn: 0.76228404045105, 0.788464903831482, 0.747326016426086, 0.753327012062073, 0.735787987709045, 0.727090001106262, 0.720335006713867, 0.721843004226685, 0.719871044158936, 0.728644967079163
http1_10k_reqs_100_conns: 0.841314911842346, 0.884468078613281, 0.854345917701721, 0.847344040870667, 0.847607016563416, 0.841101050376892, 0.814589977264404, 0.818693995475769, 0.832610964775085, 0.828818082809448
```

Modifications:

use UnsafeMutableBufferPointer#initialize(from: Bytes) to enumerate
String's bytes.

Result:

faster but still not fast enough, we need SR-7602 to be addressed
2018-05-06 09:30:54 +01:00
Norman Maurer 62a9ff1955 Fix possible header / URI corruption when discardReadBytes() is called in HTTPDecoder (#385)
Motivation:

When discardReadBytes() was called and we still did not pass the Head to the user it was quite possible that the headers / URI could be corrupted as the stored readerIndex did not matchup anymore.

Modifications:

- Override most of the functionality of ByteToMessageDecoder in HTTPDecoder to better work with the provided state machine of http_parser
- Remove allocations by removing the pendingInOut Array as its not needed anymore.
- Add guards against re-entrance calls
- Add unit test that shows that everything works as expected now.

Result:

Fixed bug and reduced allocations (8% - 10% perf win).
2018-05-04 19:22:54 +02:00
Johannes Weiß 2f55baf537 implement lazy selector registration (#388)
Motivation:

Previously we would eagerly register any fd with the selector
(epoll/kqueue) which works well with kqueue. With epoll however, you'll
get `EPOLLHUP` immediately if you supplied a socket that hasn't had
connect/bind called on it.
We got away with this because we took quite a bit of care to always make
sure we call connect/bind pretty much immediately after we registered
it.  And crucially before we ever asked the selector to tell us about
new events.

Modifications:

made selector registration lazy (when we try activating the channel by
calling connect/bind)

Result:

the user can now register and connect whenever they feel like it.
2018-05-04 19:09:59 +02:00
Helge Heß 7fcbff9fce Add support for compiling and running NIO on Raspberry Pi 32-bit (#383)
* Make NIOHTTP1 compile on 32-bit platforms

Motivation:

When building a datacenter out of Apple Watches w/ shattered
glasses, you'll need the ability to build on 32 bit platforms.
This fixes building NIOHTTP1 on such platforms.

Modifications:

This was resetting the parser context pointer using a very
long deadly beef. Had to shorted this for 32bit cows.

Result:

NIOHTTP1 compiles on 32 bit ARM platforms.
2018-05-04 17:13:13 +01:00
Johannes Weiß a5db2a6751 fix event loop hop between registration and activation of accepted channels (#389)
Motivation:

Once again, we had an extra event loop hop between a channel
registration and its activation. Usually this shows up as `EPOLLHUP` but
not so for accepted channels. What happened instead is that we had a
small race window after we accepted a channel. It was in a state where
it was not marked active _yet_ and therefore we'd not read out its data
in case we received a `.readEOF`. That usually leads to a stale
connection. Fortunately it doesn't happen very often that the client
connects, immediately sends some data and then shuts the write end of
the socket.

Modifications:

prevent the event loop hop between registration and activation

Result:

will always read out the read buffer on .readEOF
2018-05-04 15:48:08 +01:00
Johannes Weiß 5bebbf5565 don't crash if close is called when close fails (#387)
Motivation:

@vlm hit an interesting situation which is very likely the sign of
another bug that we have yet to find:

During a close, @vlm hit an error in close which lead to a user callout
which then closed again.

The immediate fix is simple (this PR) is as always not to call out to
the user before reconciling our own state. But hitting this bug also
means that either a `socket.close` or a `selectableEventLoop.deregister`
failed as we only called out in those situations. That definitely hides
a condition that is hard to imagine without another bug that we still
need to find.

Modifications:

in `BaseSocketChannel.close0` follow rule 0 and don't call out before
reconciling state.

Result:

fewer crashes, less potential to hide other bugs.
2018-05-03 17:35:49 +02:00
Johannes Weiß 359f35930a fix Swift 4.2 compile error (#384)
Motivation:

We had a public typealias for an internal type, that's an error now in
Swift 4.2.

Modifications:

make the typealias internal.

Result:

compiles in Swift 4.2
2018-05-02 18:42:48 +01:00