From 0e7d4e9534fdc00a18d6dd93ab221359f2bd5bc2 Mon Sep 17 00:00:00 2001 From: Rick Newton-Rogers Date: Tue, 17 Mar 2026 19:19:37 +0000 Subject: [PATCH] Fix flakey `testHalfCloseOwnOutputWithPopulatedBuffer` on iOS sim (#3551) ### Motivation * `testHalfCloseOwnOutputWithPopulatedBuffer` asserts that writing to a half-closed channel produces `ChannelError.outputClosed`. However, after flushing the buffered write and the FIN, the remote peer can process both and fully close the connection before the subsequent write is processed. When this happens the channel is fully closed and the write fails with `.ioOnClosedChannel` instead. * This race seems to be more likely on iOS simulators in CI. ### Modifications * Accept either `.outputClosed` or `.ioOnClosedChannel` as valid errors from the post-half-close write. ### Result * The test no longer flakes on iOS simulator CI runs. --- Tests/NIOPosixTests/StreamChannelsTest.swift | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Tests/NIOPosixTests/StreamChannelsTest.swift b/Tests/NIOPosixTests/StreamChannelsTest.swift index 6b4f21c34..3d9c9f2cd 100644 --- a/Tests/NIOPosixTests/StreamChannelsTest.swift +++ b/Tests/NIOPosixTests/StreamChannelsTest.swift @@ -282,9 +282,16 @@ class StreamChannelTest: XCTestCase { // flush chan1's output chan1.flush() - // Attempt to write a byte from chan1 to chan2 which should be refused after the close + // Attempt to write a byte from chan1 to chan2 which should be refused after the close. + // We expect `.outputClosed` from the half-close, but if the remote peer processes + // the FIN and closes before this write is processed, the channel may fully close + // and we get `.ioOnClosedChannel` instead. XCTAssertThrowsError(try chan1.write(self.buffer).wait()) { error in - XCTAssertEqual(ChannelError.outputClosed, error as? ChannelError, "\(chan1)") + let channelError = error as? ChannelError + XCTAssertTrue( + channelError == .outputClosed || channelError == .ioOnClosedChannel, + "Unexpected error \(error) on \(chan1)" + ) } // wait for the write to complete