diff --git a/Sources/NIOSSL/NIOSSLHandler.swift b/Sources/NIOSSL/NIOSSLHandler.swift index c15c87ba..d5a831da 100644 --- a/Sources/NIOSSL/NIOSSLHandler.swift +++ b/Sources/NIOSSL/NIOSSLHandler.swift @@ -280,7 +280,9 @@ public class NIOSSLHandler: ChannelInboundHandler, ChannelOutboundHandler, Remov case .idle, .handshaking, .additionalVerification: // we should not flush immediately as we have not completed the handshake and instead buffer the flush self.bufferFlush() - case .active, .unwrapping, .closing, .unwrapped, .inputClosed, .outputClosed, .closed: + case .closed: + self.discardBufferedActions(reason: ChannelError.ioOnClosedChannel) + case .active, .unwrapping, .closing, .unwrapped, .inputClosed, .outputClosed: self.bufferFlush() self.doUnbufferActions(context: context) } diff --git a/Tests/NIOSSLTests/NIOSSLIntegrationTest.swift b/Tests/NIOSSLTests/NIOSSLIntegrationTest.swift index 2c1fe434..edc7bf3a 100644 --- a/Tests/NIOSSLTests/NIOSSLIntegrationTest.swift +++ b/Tests/NIOSSLTests/NIOSSLIntegrationTest.swift @@ -3884,4 +3884,73 @@ class NIOSSLIntegrationTest: XCTestCase { XCTAssertEqual(error as? ChannelError, .ioOnClosedChannel) } } + + func testFlushFromErrorCaughtAfterHandshakeEOFDoesNotSpin() throws { + try self.assertFlushAfterHandshakeEOFDoesNotSpin(halfClosure: false) + } + + func testFlushFromErrorCaughtAfterHandshakeHalfClosureDoesNotSpin() throws { + try self.assertFlushAfterHandshakeEOFDoesNotSpin(halfClosure: true) + } + + func testFlushBeforeCloseAfterHandshakeEOFDoesNotSpin() throws { + try self.assertFlushAfterHandshakeEOFDoesNotSpin(halfClosure: false, closeAfterFlush: true) + } + + private func assertFlushAfterHandshakeEOFDoesNotSpin( + halfClosure: Bool, + closeAfterFlush: Bool = false + ) throws { + final class FlushOnError: ChannelInboundHandler { + typealias InboundIn = ByteBuffer + typealias OutboundOut = ByteBuffer + + private let writePromise: EventLoopPromise + private let closeAfterFlush: Bool + + init(writePromise: EventLoopPromise, closeAfterFlush: Bool) { + self.writePromise = writePromise + self.closeAfterFlush = closeAfterFlush + } + + func channelActive(context: ChannelHandlerContext) { + context.writeAndFlush( + self.wrapOutboundOut(context.channel.allocator.buffer(string: "pending write")), + promise: self.writePromise + ) + context.fireChannelActive() + } + + func errorCaught(context: ChannelHandlerContext, error: Error) { + context.flush() + if self.closeAfterFlush { + context.close(promise: nil) + } + context.fireErrorCaught(error) + } + } + + let eventLoop = EmbeddedEventLoop() + let writePromise = eventLoop.makePromise(of: Void.self) + let channel = EmbeddedChannel( + handlers: [ + NIOSSLServerHandler(context: try self.configuredSSLContext()), + FlushOnError(writePromise: writePromise, closeAfterFlush: closeAfterFlush), + ], + loop: eventLoop + ) + try channel.connect(to: .init(ipAddress: "127.0.0.1", port: 443)).wait() + + if halfClosure { + channel.pipeline.fireUserInboundEventTriggered(ChannelEvent.inputClosed) + } else { + channel.pipeline.fireChannelInactive() + } + eventLoop.run() + + XCTAssertThrowsError(try writePromise.futureResult.wait()) { error in + XCTAssertEqual(error as? ChannelError, .ioOnClosedChannel) + } + XCTAssertNil(try channel.readOutbound(as: ByteBuffer.self)) + } }