From da905d17f8cdca6b6260d2f5dec32a19c4c2eb1f Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Mon, 18 Sep 2017 14:42:35 +0100 Subject: [PATCH] channelInactive should fire before channelUnregistered --- Sources/NIO/Channel.swift | 4 ++-- Sources/NIO/Embedded.swift | 2 +- Tests/NIOTests/EchoServerClientTest.swift | 4 ++++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Sources/NIO/Channel.swift b/Sources/NIO/Channel.swift index d12a53784..8f3523436 100644 --- a/Sources/NIO/Channel.swift +++ b/Sources/NIO/Channel.swift @@ -1145,11 +1145,11 @@ class BaseSocketChannel : SelectableChannel, ChannelCore { // Fail all pending writes and so ensure all pending promises are notified self.pendingWrites.failAll(error: error) + pipeline.fireChannelInactive0() + if !neverRegistered { pipeline.fireChannelUnregistered0() } - - pipeline.fireChannelInactive0() eventLoop.execute { // ensure this is executed in a delayed fashion as the users code may still traverse the pipeline diff --git a/Sources/NIO/Embedded.swift b/Sources/NIO/Embedded.swift index 75da29385..8c42cdf08 100644 --- a/Sources/NIO/Embedded.swift +++ b/Sources/NIO/Embedded.swift @@ -98,8 +98,8 @@ class EmbeddedChannelCore : ChannelCore { promise?.succeed(result: ()) // As we called register() in the constructor of EmbeddedChannel we also need to ensure we call unregistered here. - pipeline.fireChannelUnregistered0() pipeline.fireChannelInactive0() + pipeline.fireChannelUnregistered0() eventLoop.execute { // ensure this is executed in a delayed fashion as the users code may still traverse the pipeline diff --git a/Tests/NIOTests/EchoServerClientTest.swift b/Tests/NIOTests/EchoServerClientTest.swift index 2b97a8bc0..77f65b03e 100644 --- a/Tests/NIOTests/EchoServerClientTest.swift +++ b/Tests/NIOTests/EchoServerClientTest.swift @@ -208,6 +208,8 @@ class EchoServerClientTest : XCTestCase { public func channelInactive(ctx: ChannelHandlerContext) { if alreadyClosedInChannelInactive.compareAndExchange(expected: false, desired: true) { + XCTAssertFalse(self.channelUnregisteredPromise.futureResult.fulfilled, + "channelInactive should fire before channelUnregistered") ctx.close().whenComplete { val in switch val { case .failure(ChannelError.alreadyClosed): @@ -224,6 +226,8 @@ class EchoServerClientTest : XCTestCase { public func channelUnregistered(ctx: ChannelHandlerContext) { if alreadyClosedInChannelUnregistered.compareAndExchange(expected: false, desired: true) { + XCTAssertTrue(self.channelInactivePromise.futureResult.fulfilled, + "when channelUnregister fires, channelInactive should already have fired") ctx.close().whenComplete { val in switch val { case .failure(ChannelError.alreadyClosed):