mirror of
https://github.com/apple/swift-nio-ssl
synced 2026-11-29 05:24:23 +00:00
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.
This commit is contained in:
1 parent
711564e9cc
commit
ae213938e1
2 files changed
+55
-13
No files matched your search
@@ -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<Void> {
|
||||
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<Void>?) {
|
||||
switch self.state {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user