Skip to content

Commit

Permalink
Revert throwing error to allow Kitura tests to succeed (keep it comme…
Browse files Browse the repository at this point in the history
…nted, as it may make sense to have them in the end).

Call `performClientConnectionFailCallback` when an error happen: see issue Kitura#233
Cherry-pick PR#239 for the connectionCount failure.
Add missing test from the static list for Linux.
  • Loading branch information
mbarnach committed Apr 3, 2020
1 parent 0709eca commit 8ab5cb1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
5 changes: 4 additions & 1 deletion Sources/KituraNet/HTTP/HTTPRequestHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ internal class HTTPRequestHandler: ChannelInboundHandler, RemovableChannelHandle
}

func channelInactive(context: ChannelHandlerContext, httpServer: HTTPServer) {
httpServer.connectionCount.sub(1)
// Cherry picked from PR#239:
server.connectionCount.sub(1)
// see: https://apple.github.io/swift-nio/docs/current/NIO/Classes/ChannelHandlerContext.html#/s:3NIO21ChannelHandlerContextC04fireB8InactiveyyF
context.fireChannelInactive()
}
}
2 changes: 2 additions & 0 deletions Sources/KituraNet/HTTP/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ public class HTTPServer: Server {
} catch let error {
self.state = .failed
self.lifecycleListener.performFailCallbacks(with: error)
// This should address issue #233.
self.lifecycleListener.performClientConnectionFailCallbacks(with: error)
switch socket {
case .tcp(let port):
Log.error("Error trying to bind to \(port): \(error)")
Expand Down
18 changes: 12 additions & 6 deletions Sources/KituraNet/HTTP/HTTPServerResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public class HTTPServerResponse: ServerResponse {
public func write(from string: String) throws {
guard let channel = channel else {
// The connection was probably closed by the client, subsequently the Channel was closed, deregistered from the EventLoop and deallocated.
throw HTTPServerError.channelClosed
// throw HTTPServerError.channelClosed
return
}

channel.eventLoop.run {
Expand All @@ -138,7 +139,8 @@ public class HTTPServerResponse: ServerResponse {
public func write(from data: Data) throws {
guard let channel = channel else {
// The connection was probably closed by the client, subsequently the Channel was closed, deregistered from the EventLoop and deallocated.
throw HTTPServerError.channelClosed
// throw HTTPServerError.channelClosed
return
}

channel.eventLoop.run {
Expand Down Expand Up @@ -175,12 +177,14 @@ public class HTTPServerResponse: ServerResponse {
public func end() throws {
guard let channel = self.channel else {
// The connection was probably closed by the client, subsequently the Channel was closed, deregistered from the EventLoop and deallocated.
throw HTTPServerError.channelClosed
// throw HTTPServerError.channelClosed
return
}

guard let handler = self.handler else {
// A deallocated channel handler suggests the pipeline and the channel were also de-allocated. The connection was probably closed.
throw HTTPServerError.pipelineClosed
// throw HTTPServerError.pipelineClosed
return
}

let status = HTTPResponseStatus(statusCode: statusCode?.rawValue ?? 0)
Expand Down Expand Up @@ -210,12 +214,14 @@ public class HTTPServerResponse: ServerResponse {
private func end(with errorCode: HTTPStatusCode, withBody: Bool = false) throws {
guard let channel = self.channel else {
// The connection was probably closed by the client, subsequently the Channel was closed, deregistered from the EventLoop and deallocated.
throw HTTPServerError.channelClosed
// throw HTTPServerError.channelClosed
return
}

guard let handler = self.handler else {
// A deallocated channel handler suggests the pipeline and the channel were also de-allocated. The connection was probably closed.
throw HTTPServerError.pipelineClosed
// throw HTTPServerError.pipelineClosed
return
}

self.statusCode = errorCode
Expand Down
1 change: 1 addition & 0 deletions Tests/KituraNetTests/ClientE2ETests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ClientE2ETests: KituraNetTest {
("testErrorRequests", testErrorRequests),
("testHeadRequests", testHeadRequests),
("testKeepAlive", testKeepAlive),
("testKeepAliveDisabled", testKeepAliveDisabled),
("testPostRequests", testPostRequests),
("testPutRequests", testPutRequests),
("testPatchRequests", testPatchRequests),
Expand Down

0 comments on commit 8ab5cb1

Please sign in to comment.