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) + } }