From 37457897d97c8efcb4d23dba75937401730eabea Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Mon, 7 Jan 2019 13:49:51 +0000 Subject: [PATCH] remove bogus 'is active' assertion in channelRead0 (#740) (cherry picked from commit 722ceb869dbd8d4cdcfa9e916de063a3e6f05254) Motivation: BaseSocketChannel used to assert that `channelRead0` is only called when the channel is active. That's bogus and it's trivial to hit this issue when calling `fireChannelRead` in the last `ChannelHandler` when the channel has gone inactive. Modifications: remove assertion, add comment Result: fewer crashes --- Sources/NIO/BaseSocketChannel.swift | 2 +- .../NIOTests/ChannelPipelineTest+XCTest.swift | 1 + Tests/NIOTests/ChannelPipelineTest.swift | 27 +++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Sources/NIO/BaseSocketChannel.swift b/Sources/NIO/BaseSocketChannel.swift index 64663ff1b..cec0b7b27 100644 --- a/Sources/NIO/BaseSocketChannel.swift +++ b/Sources/NIO/BaseSocketChannel.swift @@ -1076,8 +1076,8 @@ class BaseSocketChannel: SelectableChannel, ChannelCore { } public func channelRead0(_ data: NIOAny) { - assert(self.lifecycleManager.isActive) // Do nothing by default + // note: we can't assert that we're active here as TailChannelHandler will call this on channelRead } public func errorCaught0(error: Error) { diff --git a/Tests/NIOTests/ChannelPipelineTest+XCTest.swift b/Tests/NIOTests/ChannelPipelineTest+XCTest.swift index 66b43a55a..c09840865 100644 --- a/Tests/NIOTests/ChannelPipelineTest+XCTest.swift +++ b/Tests/NIOTests/ChannelPipelineTest+XCTest.swift @@ -51,6 +51,7 @@ extension ChannelPipelineTest { ("testRemovingByNameWithFutureNotInChannel", testRemovingByNameWithFutureNotInChannel), ("testRemovingByReferenceWithPromiseStillInChannel", testRemovingByReferenceWithPromiseStillInChannel), ("testRemovingByReferenceWithFutureNotInChannel", testRemovingByReferenceWithFutureNotInChannel), + ("testFireChannelReadInInactiveChannelDoesNotCrash", testFireChannelReadInInactiveChannelDoesNotCrash), ] } } diff --git a/Tests/NIOTests/ChannelPipelineTest.swift b/Tests/NIOTests/ChannelPipelineTest.swift index 56c116e6d..f5f36ca80 100644 --- a/Tests/NIOTests/ChannelPipelineTest.swift +++ b/Tests/NIOTests/ChannelPipelineTest.swift @@ -902,4 +902,31 @@ class ChannelPipelineTest: XCTestCase { XCTAssertNil(channel.readOutbound()) XCTAssertNoThrow(try channel.throwIfErrorCaught()) } + + func testFireChannelReadInInactiveChannelDoesNotCrash() throws { + class FireWhenInactiveHandler: ChannelInboundHandler { + typealias InboundIn = () + typealias InboundOut = () + + func channelInactive(ctx: ChannelHandlerContext) { + ctx.fireChannelRead(self.wrapInboundOut(())) + } + } + let handler = FireWhenInactiveHandler() + let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) + defer { + XCTAssertNoThrow(try group.syncShutdownGracefully()) + } + let server = try assertNoThrowWithValue(ServerBootstrap(group: group).bind(host: "127.0.0.1", port: 0).wait()) + defer { + XCTAssertNoThrow(try server.close().wait()) + } + let client = try assertNoThrowWithValue(ClientBootstrap(group: group) + .channelInitializer { channel in + channel.pipeline.add(handler: handler) + } + .connect(to: server.localAddress!) + .wait()) + XCTAssertNoThrow(try client.close().wait()) + } }