From 5aff263ad98b91cb53eae4daf8e8aad149c62263 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 12 Feb 2018 16:50:34 +0100 Subject: [PATCH] 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 --- Sources/NIO/SocketChannel.swift | 21 +++++---- .../EchoServerClientTest+XCTest.swift | 1 + Tests/NIOTests/EchoServerClientTest.swift | 46 +++++++++++++++++++ 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/Sources/NIO/SocketChannel.swift b/Sources/NIO/SocketChannel.swift index 160428d02..b972ba048 100644 --- a/Sources/NIO/SocketChannel.swift +++ b/Sources/NIO/SocketChannel.swift @@ -486,16 +486,16 @@ class BaseSocketChannel : 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 : SelectableChannel, ChannelCore { // Call before triggering the close of the Channel. pipeline.fireChannelReadComplete0() close0(error: err, mode: .all, promise: nil) + return } pipeline.fireChannelReadComplete0() diff --git a/Tests/NIOTests/EchoServerClientTest+XCTest.swift b/Tests/NIOTests/EchoServerClientTest+XCTest.swift index cf0cbff0e..dd6e2be90 100644 --- a/Tests/NIOTests/EchoServerClientTest+XCTest.swift +++ b/Tests/NIOTests/EchoServerClientTest+XCTest.swift @@ -38,6 +38,7 @@ extension EchoServerClientTest { ("testWriteOnAccept", testWriteOnAccept), ("testWriteAfterChannelIsDead", testWriteAfterChannelIsDead), ("testPendingReadProcessedAfterWriteError", testPendingReadProcessedAfterWriteError), + ("testChannelErrorEOFNotFiredThroughPipeline", testChannelErrorEOFNotFiredThroughPipeline), ] } } diff --git a/Tests/NIOTests/EchoServerClientTest.swift b/Tests/NIOTests/EchoServerClientTest.swift index f4243ba86..e75f833ab 100644 --- a/Tests/NIOTests/EchoServerClientTest.swift +++ b/Tests/NIOTests/EchoServerClientTest.swift @@ -697,4 +697,50 @@ class EchoServerClientTest : XCTestCase { XCTAssertNoThrow(try group.syncShutdownGracefully()) } + + func testChannelErrorEOFNotFiredThroughPipeline() throws { + + class ErrorHandler : ChannelInboundHandler { + typealias InboundIn = ByteBuffer + + private let promise: EventLoopPromise + + init(_ promise: EventLoopPromise) { + 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 = 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()) + } }