mirror of
https://github.com/apple/swift-nio-ssl
synced 2026-11-28 05:14:29 +00:00
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.
This commit is contained in:
1 parent
081068d62f
commit
085af4c63b
3 files changed
+43
-2
No files matched your search
@@ -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
|
||||
|
||||
@@ -46,6 +46,7 @@ extension OpenSSLIntegrationTest {
|
||||
("testFlushPendingReadsOnCloseNotify", testFlushPendingReadsOnCloseNotify),
|
||||
("testForcingVerificationFailure", testForcingVerificationFailure),
|
||||
("testExtractingCertificates", testExtractingCertificates),
|
||||
("testRepeatedClosure", testRepeatedClosure),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user