From ae213938e151964aa691f0e902462fbe06baeeb6 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Thu, 26 Mar 2020 15:04:20 +0000 Subject: [PATCH] Unwrapping timeout shouldn't close channel. (#206) Motivation: If the unwrapping timeout fires, rather than close the channel we should just fire an error down the pipeline. This is in-line with standard NIO behaviour, and allows users to opt-out of channel closure in the event that they need it. Modifications: - Internal state modification to have different behaviour when unwrapping and closing timeout fires. Result: Users can control what happens if unwrapping times out. --- Sources/NIOSSL/NIOSSLHandler.swift | 42 ++++++++++++++++++++++--- Tests/NIOSSLTests/UnwrappingTests.swift | 26 ++++++++++----- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/Sources/NIOSSL/NIOSSLHandler.swift b/Sources/NIOSSL/NIOSSLHandler.swift index 290c135b..1124b9aa 100644 --- a/Sources/NIOSSL/NIOSSLHandler.swift +++ b/Sources/NIOSSL/NIOSSLHandler.swift @@ -313,8 +313,24 @@ public class NIOSSLHandler : ChannelInboundHandler, ChannelOutboundHandler, Remo /// out. This task should be cancelled if the shutdown does not time out. private func scheduleTimedOutShutdown(context: ChannelHandlerContext) -> Scheduled { return context.eventLoop.scheduleTask(in: self.shutdownTimeout) { - self.state = .closed - self.channelClose(context: context, reason: NIOSSLCloseTimedOutError()) + switch self.state { + case .idle, .handshaking, .active: + preconditionFailure("Cannot schedule timed out shutdown on non-shutting down handler") + + case .closed, .unwrapped: + // This means we raced with the shutdown completing. We just let this one go: do nothing. + return + + case .closing: + // We're closing, the only thing we do here is exit. + self.state = .closed + self.channelClose(context: context, reason: NIOSSLCloseTimedOutError()) + + case .unwrapping: + // The user only wants us to error and unwrap, not to close. + self.state = .unwrapped + self.channelUnwrap(context: context, failedWithError: NIOSSLCloseTimedOutError()) + } } } @@ -451,7 +467,7 @@ public class NIOSSLHandler : ChannelInboundHandler, ChannelOutboundHandler, Remo context.close(promise: closePromise) } - private func channelUnwrap(context: ChannelHandlerContext) { + private func channelUnwrap(context: ChannelHandlerContext, failedWithError error: Error? = nil) { assert(self.closePromise == nil) self.state = .unwrapped @@ -468,9 +484,23 @@ public class NIOSSLHandler : ChannelInboundHandler, ChannelOutboundHandler, Remo if let unconsumedData = self.connection.extractUnconsumedData() { context.fireChannelRead(self.wrapInboundOut(unconsumedData)) } + + if let error = error { + context.fireErrorCaught(error) + } } if let promise = shutdownPromise { + removalFuture.whenComplete { result in + switch (result, error) { + case(.success, .none): + promise.succeed(()) + case (.success, .some(let error)): + promise.fail(error) + case (.failure(let failure), _): + promise.fail(failure) + } + } removalFuture.cascade(to: promise) } @@ -504,13 +534,15 @@ extension NIOSSLHandler { /// /// This will send a CLOSE_NOTIFY and wait for the corresponding CLOSE_NOTIFY. When that next /// CLOSE_NOTIFY is received, this handler will pass on all pending writes and remove itself - /// from the channel pipeline. + /// from the channel pipeline. If the shutdown times out then an error will fire down the + /// pipeline, this handler will remove itself from the pipeline, but the channel will not be + /// automatically closed. /// /// This function **is not thread-safe**: you **must** call it from the correct event /// loop thread. /// /// - parameters: - /// - promise: (optional) An `EventLoopPromise` that will be completed when the unwrapping has + /// - promise: An `EventLoopPromise` that will be completed when the unwrapping has /// completed. public func stopTLS(promise: EventLoopPromise?) { switch self.state { diff --git a/Tests/NIOSSLTests/UnwrappingTests.swift b/Tests/NIOSSLTests/UnwrappingTests.swift index 584e9ca7..f258de45 100644 --- a/Tests/NIOSSLTests/UnwrappingTests.swift +++ b/Tests/NIOSSLTests/UnwrappingTests.swift @@ -742,8 +742,8 @@ final class UnwrappingTests: XCTestCase { var unwrapped = false defer { - XCTAssertNoThrow(try serverChannel.finish(acceptAlreadyClosed: true)) - XCTAssertNoThrow(try clientChannel.finish(acceptAlreadyClosed: true)) + XCTAssertNoThrow(try serverChannel.finish(acceptAlreadyClosed: false)) + XCTAssertNoThrow(try clientChannel.finish(acceptAlreadyClosed: false)) } let context = try assertNoThrowWithValue(configuredSSLContext()) @@ -785,21 +785,28 @@ final class UnwrappingTests: XCTestCase { XCTAssertFalse(clientClosed) XCTAssertFalse(serverClosed) XCTAssertFalse(unwrapped) + XCTAssertNoThrow(try serverChannel.throwIfErrorCaught()) - // Advance time by 5 seconds. This should fire the timeout. We unwrap and close the connection. + // Advance time by 5 seconds. This should fire the timeout. We unwrap. The connection is not closed automatically. serverChannel.embeddedEventLoop.advanceTime(by: .seconds(5)) XCTAssertFalse(clientClosed) - XCTAssertTrue(serverClosed) + XCTAssertFalse(serverClosed) XCTAssertTrue(unwrapped) serverChannel.pipeline.assertDoesNotContain(handler: serverHandler) + XCTAssertThrowsError(try serverChannel.throwIfErrorCaught()) { error in + XCTAssertTrue(error is NIOSSLCloseTimedOutError, "Unexpected error: \(error)") + } // Now we do the same for the client to get it out of the pipeline too. Naturally, it'll time out. clientHandler.stopTLS(promise: nil) clientChannel.embeddedEventLoop.advanceTime(by: .seconds(5)) clientChannel.pipeline.assertDoesNotContain(handler: clientHandler) + XCTAssertThrowsError(try clientChannel.throwIfErrorCaught()) { error in + XCTAssertTrue(error is NIOSSLCloseTimedOutError, "Unexpected error: \(error)") + } - XCTAssertTrue(clientClosed) - XCTAssertTrue(serverClosed) + XCTAssertFalse(clientClosed) + XCTAssertFalse(serverClosed) XCTAssertTrue(unwrapped) } @@ -875,7 +882,7 @@ final class UnwrappingTests: XCTestCase { defer { XCTAssertNoThrow(try serverChannel.finish(acceptAlreadyClosed: true)) - XCTAssertNoThrow(try clientChannel.finish(acceptAlreadyClosed: true)) + XCTAssertNoThrow(try clientChannel.finish(acceptAlreadyClosed: false)) } let context = try assertNoThrowWithValue(configuredSSLContext()) @@ -948,8 +955,11 @@ final class UnwrappingTests: XCTestCase { clientHandler.stopTLS(promise: nil) clientChannel.embeddedEventLoop.advanceTime(by: .seconds(5)) clientChannel.pipeline.assertDoesNotContain(handler: clientHandler) + XCTAssertThrowsError(try clientChannel.throwIfErrorCaught()) { error in + XCTAssertTrue(error is NIOSSLCloseTimedOutError, "Unexpected error: \(error)") + } - XCTAssertTrue(clientClosed) + XCTAssertFalse(clientClosed) XCTAssertTrue(serverClosed) XCTAssertTrue(unwrapped) }