Correctly handle ChannelError.eof when reading data from Socket

Motivation:

The code to filter out ChannelError.eof when reading from the socket and not call fireErrorCaught(...) was broken and so the error was propagated through the pipeline.

Modifications:

- Correctly filter out .eof
- Added testcase

Result:

Correct handling of .eof
This commit is contained in:
Norman Maurer committed 2018-02-12 16:50:34 +01:00
1 parent 5705ce104a
commit 5aff263ad9
3 files changed
+58 -10

No files matched your search

+11 -10
View File
@@ -486,16 +486,16 @@ class BaseSocketChannel<T : BaseSocket> : SelectableChannel, ChannelCore {
try readFromSocket()
} catch let err {
// ChannelError.eof is not something we want to fire through the pipeline as it just means the remote
// per closed / shutdown the connection.
if let channelErr = err as? ChannelError, channelErr != ChannelError.eof {
pipeline.fireErrorCaught0(error: err)
} else if try! getOption(option: ChannelOptions.allowRemoteHalfClosure) {
// If we want to allow half closure we will just mark the input side of the Channel
// as closed.
pipeline.fireChannelReadComplete0()
close0(error: err, mode: .input, promise: nil)
readPending = false
return
// peer closed / shutdown the connection.
if let channelErr = err as? ChannelError, channelErr == ChannelError.eof {
if try! getOption(option: ChannelOptions.allowRemoteHalfClosure) {
// If we want to allow half closure we will just mark the input side of the Channel
// as closed.
pipeline.fireChannelReadComplete0()
close0(error: err, mode: .input, promise: nil)
readPending = false
return
}
} else {
pipeline.fireErrorCaught0(error: err)
}
@@ -503,6 +503,7 @@ class BaseSocketChannel<T : BaseSocket> : SelectableChannel, ChannelCore {
// Call before triggering the close of the Channel.
pipeline.fireChannelReadComplete0()
close0(error: err, mode: .all, promise: nil)
return
}
pipeline.fireChannelReadComplete0()
@@ -38,6 +38,7 @@ extension EchoServerClientTest {
("testWriteOnAccept", testWriteOnAccept),
("testWriteAfterChannelIsDead", testWriteAfterChannelIsDead),
("testPendingReadProcessedAfterWriteError", testPendingReadProcessedAfterWriteError),
("testChannelErrorEOFNotFiredThroughPipeline", testChannelErrorEOFNotFiredThroughPipeline),
]
}
}
+46
View File
@@ -697,4 +697,50 @@ class EchoServerClientTest : XCTestCase {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}
func testChannelErrorEOFNotFiredThroughPipeline() throws {
class ErrorHandler : ChannelInboundHandler {
typealias InboundIn = ByteBuffer
private let promise: EventLoopPromise<Void>
init(_ promise: EventLoopPromise<Void>) {
self.promise = promise
}
public func errorCaught(ctx: ChannelHandlerContext, error: Error) {
if let err = error as? ChannelError {
XCTAssertNotEqual(ChannelError.eof, err)
}
}
public func channelInactive(ctx: ChannelHandlerContext) {
self.promise.succeed(result: ())
}
}
let group = MultiThreadedEventLoopGroup(numThreads: 1)
defer {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}
let promise: EventLoopPromise<Void> = group.next().newPromise()
let serverChannel = try ServerBootstrap(group: group)
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
.childChannelInitializer { channel in
channel.pipeline.add(handler: ErrorHandler(promise))
}.bind(to: "127.0.0.1", on: 0).wait()
defer {
XCTAssertNoThrow(try serverChannel.close().wait())
}
let clientChannel = try ClientBootstrap(group: group)
.connect(to: serverChannel.localAddress!).wait()
XCTAssertNoThrow(try clientChannel.close().wait())
XCTAssertNoThrow(try promise.futureResult.wait())
}
}