From 15e0b9e495786807272dd4891fa4bac76aa43034 Mon Sep 17 00:00:00 2001 From: Goutam Adwant <8672451+goutamadwant@users.noreply.github.com> Date: Mon, 12 Oct 2026 02:33:56 -0700 Subject: [PATCH] Avoid flushing buffered writes on closed TLS channels (#610) ## Summary - Discard buffered actions when `NIOSSLHandler.flush` runs after the handler reaches `.closed`, failing pending write promises with `ChannelError.ioOnClosedChannel`. - Add regression coverage for handshake EOF, remote half closure, and flush-before-close behavior. ## Motivation During handshake failure, a downstream `errorCaught` callback can flush a write that TLS buffered before closing. The closed handler cannot encode that write, so `doUnbufferActions` repeats until its no-progress assertion aborts a debug build. The new closed-state path avoids entering that loop. ## Testing - Before the fix, the full-closure and half-closure regressions each reproduced the `doUnbufferActions(context:) looped too many times` assertion. - After the fix, `swift test --jobs 4` passed all 344 tests using Swift 6.4 on Linux arm64. - macOS execution has not been verified for this change. Fixes #609 --- Sources/NIOSSL/NIOSSLHandler.swift | 4 +- Tests/NIOSSLTests/NIOSSLIntegrationTest.swift | 69 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) 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)) + } }