From 085af4c63b356f4e855bdd070583a8ad186e4615 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Wed, 7 Nov 2018 11:30:53 +0000 Subject: [PATCH] Prevent crashes when closing twice. (#52) Motivation: If a user called close on a channel without attaching a promise, and subsequently called close again with a promise, before the TLS shutdown completes, this would lead to a crash due to a force-unwrap. It has been my experience that crashes are less than ideal, especially when users have done nothing wrong. Modifications: Replaced the force-unwrap with code that correctly persists the promise. Added a test. Result: Less likely to crash. --- Sources/NIOOpenSSL/OpenSSLHandler.swift | 6 ++- .../OpenSSLIntegrationTest+XCTest.swift | 1 + .../OpenSSLIntegrationTest.swift | 38 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Sources/NIOOpenSSL/OpenSSLHandler.swift b/Sources/NIOOpenSSL/OpenSSLHandler.swift index b14a9a42..8debaecc 100644 --- a/Sources/NIOOpenSSL/OpenSSLHandler.swift +++ b/Sources/NIOOpenSSL/OpenSSLHandler.swift @@ -151,8 +151,10 @@ public class OpenSSLHandler : ChannelInboundHandler, ChannelOutboundHandler { case .closing: // We're in the process of TLS shutdown, so let's let that happen. However, // we want to cascade the result of the first request into this new one. - if let promise = promise { - closePromise!.futureResult.cascade(promise: promise) + if let promise = promise, let closePromise = self.closePromise { + closePromise.futureResult.cascade(promise: promise) + } else if let promise = promise { + self.closePromise = promise } case .idle: state = .closed diff --git a/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest+XCTest.swift b/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest+XCTest.swift index bc2a367a..69527c9b 100644 --- a/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest+XCTest.swift +++ b/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest+XCTest.swift @@ -46,6 +46,7 @@ extension OpenSSLIntegrationTest { ("testFlushPendingReadsOnCloseNotify", testFlushPendingReadsOnCloseNotify), ("testForcingVerificationFailure", testForcingVerificationFailure), ("testExtractingCertificates", testExtractingCertificates), + ("testRepeatedClosure", testRepeatedClosure), ] } } diff --git a/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift b/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift index 3527cf33..b57929b9 100644 --- a/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift +++ b/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift @@ -1245,4 +1245,42 @@ class OpenSSLIntegrationTest: XCTestCase { XCTAssertEqual(certificates.count, 1) } + + func testRepeatedClosure() throws { + let serverChannel = EmbeddedChannel() + let clientChannel = EmbeddedChannel() + + defer { + // We expect both cases to throw + XCTAssertThrowsError(try serverChannel.finish()) + XCTAssertThrowsError(try clientChannel.finish()) + } + + let ctx = try assertNoThrowWithValue(configuredSSLContext()) + + XCTAssertNoThrow(try serverChannel.pipeline.add(handler: OpenSSLServerHandler(context: ctx)).wait()) + XCTAssertNoThrow(try clientChannel.pipeline.add(handler: OpenSSLClientHandler(context: ctx)).wait()) + let handshakeHandler = HandshakeCompletedHandler() + XCTAssertNoThrow(try clientChannel.pipeline.add(handler: handshakeHandler).wait()) + + // Connect. This should lead to a completed handshake. + let addr: SocketAddress = try SocketAddress(unixDomainSocketPath: "/tmp/whatever") + let connectFuture = clientChannel.connect(to: addr) + serverChannel.pipeline.fireChannelActive() + try interactInMemory(clientChannel: clientChannel, serverChannel: serverChannel) + try connectFuture.wait() + XCTAssertTrue(handshakeHandler.handshakeSucceeded) + + // We're going to close twice: the first one without a promise, the second one with one. + var closed = false + clientChannel.close(promise: nil) + clientChannel.close().whenComplete { + closed = true + } + XCTAssertFalse(closed) + XCTAssertNoThrow(try interactInMemory(clientChannel: clientChannel, serverChannel: serverChannel)) + + // The closure should have happened. + XCTAssertTrue(closed) + } }