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
This commit is contained in:
Goutam Adwant authored and GitHub committed 2026-10-12 10:33:56 +01:00
1 parent 86458a336b
commit 15e0b9e495
2 files changed
+72 -1

No files matched your search

+3 -1
View File
@@ -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)
}
@@ -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<Void>
private let closeAfterFlush: Bool
init(writePromise: EventLoopPromise<Void>, 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))
}
}