Test HTTP server - avoid writing .end to the wrong channel for responses using content-length (#3792)

### Motivation:

One of my project uses `NIOHTTP1TestServer` for testing and for
responses that contain the `content-length` header + `connection:
close`,
the test was flaky - failing intermittently, because the client closed
the connection before the test managed to submit `.end` to the test
server.

The cause was a race condition between the test and the client's and
server's threads hosting their respective event loops.

### Modifications:

Now `NIOHTTP1TestServer` captures the channel that sent the `.head`, and
even once `currentClientChannel` changes (but before the next `.head` is
sent),
when a call to send `.end` comes in, uses the channel associated with
the previous `.head`, rather than reading `currentClientChannel`.

This test is skipped on Darwin as it requires the platform to detect
early EOF.

### Result:

More reliable tests that involve two requests that come in at once (in a
deterministic order), when the second one is parked and ready right
after the first one finishes.
This commit is contained in:
Honza Dvorsky authored and GitHub committed 2026-11-18 08:44:43 +01:00
1 parent b440f7bae8
commit 93bd8f2e02
2 files changed
+89 -3

No files matched your search

+31 -3
View File
@@ -221,8 +221,23 @@ public final class NIOHTTP1TestServer {
// all protected by eventLoop
private let inboundBuffer: BlockingQueue<HTTPServerRequestPart> = .init()
private var currentClientChannel: Channel? = nil
private var outboundResponse: OutboundResponse = .idle
private var serverChannel: Channel! = nil
/// Ensures that after we `writeOutbound` a `.head` to a specific channel, we send
/// the `.body` and `.end` to the same channel, before `currentClientChannel` changes again.
///
/// Ensures that we don't send `.end` to a new channel, if we omitted `.end` to the previous
/// current channel.
private enum OutboundResponse {
/// No response is in progress.
case idle
/// The response's `.head` went to this connection, so send `.body` and `.end` to the same one.
case started(on: Channel?)
}
enum State {
case channelsAvailable(CircularBuffer<Channel>)
case waitingForChannel(EventLoopPromise<Void>)
@@ -357,11 +372,24 @@ extension NIOHTTP1TestServer {
let transformed = try SendableHTTPServerResponsePart(data)
try self.eventLoop.flatSubmit { () -> EventLoopFuture<Void> in
if let channel = self.currentClientChannel {
return channel.writeAndFlush(transformed)
} else {
let channel: Channel?
switch (transformed, self.outboundResponse) {
case (.head, _):
channel = self.currentClientChannel
self.outboundResponse = .started(on: channel)
case (.body, .started(let responseChannel)), (.end, .started(let responseChannel)):
channel = responseChannel
case (.body, .idle), (.end, .idle):
channel = self.currentClientChannel
}
if case .end = transformed {
self.outboundResponse = .idle
}
guard let channel else {
return self.eventLoop.makeFailedFuture(ChannelError.ioOnClosedChannel)
}
return channel.writeAndFlush(transformed)
}.wait()
}
@@ -519,6 +519,64 @@ class NIOHTTP1TestServerTest: XCTestCase {
// The promise for the second should error.
XCTAssertThrowsError(try secondResponsePromise.futureResult.wait())
}
func testResponsePartsAreNotWrittenToTheNextConnection() throws {
#if canImport(Darwin)
// This race needs the server to notice the client's close while a response is still in
// progress. `HTTPServerPipelineHandler` doesn't read during a response, and on Darwin NIO
// only learns about a remote close while reading (`isEarlyEOFDeliveryWorkingOnThisOS` in
// NIOPosix is `false`), so the server can't move on to the next connection before the
// response ends.
throw XCTSkip("Needs early EOF delivery, which NIO disables on Darwin")
#else
let testServer = NIOHTTP1TestServer(group: self.group)
defer {
XCTAssertNoThrow(try testServer.stop())
}
let response1Promise = self.group.next().makePromise(of: String.self)
let channel1 = try self.connect(serverPort: testServer.serverPort, responsePromise: response1Promise).wait()
let response2Promise = self.group.next().makePromise(of: String.self)
// `channel2` is accepted but waits until the server is done with `channel1`.
let channel2 = try self.connect(serverPort: testServer.serverPort, responsePromise: response2Promise).wait()
self.sendRequest(channel: channel1, uri: "/request1", message: "Request #1")
XCTAssertNoThrow(try testServer.readInbound().assertHead(expectedURI: "/request1"))
XCTAssertNoThrow(try testServer.readInbound().assertBody(expectedMessage: "Request #1"))
XCTAssertNoThrow(try testServer.readInbound().assertEnd())
// A `content-length` response is complete once its body has arrived, so the client closes
// the connection without waiting for the server to write the `.end`.
let response1Buffer = self.allocator.buffer(string: "Response #1")
let response1Head = HTTPResponseHead(
version: .http1_1,
status: .ok,
headers: ["content-length": "\(response1Buffer.readableBytes)"]
)
XCTAssertNoThrow(try testServer.writeOutbound(.head(response1Head)))
XCTAssertNoThrow(try testServer.writeOutbound(.body(.byteBuffer(response1Buffer))))
XCTAssertNoThrow(XCTAssertEqual("Response #1", try response1Promise.futureResult.wait()))
XCTAssertNoThrow(try channel1.closeFuture.wait())
// Once the server has read the request from `channel2`, `channel2` is the connection being served.
self.sendRequest(channel: channel2, uri: "/request2", message: "Request #2")
XCTAssertNoThrow(try testServer.readInbound().assertHead(expectedURI: "/request2"))
XCTAssertNoThrow(try testServer.readInbound().assertBody(expectedMessage: "Request #2"))
XCTAssertNoThrow(try testServer.readInbound().assertEnd())
// The first response's `.end` belongs to the closed `channel1`, not to `channel2`.
XCTAssertThrowsError(try testServer.writeOutbound(.end(nil))) { error in
XCTAssertEqual(error as? ChannelError, .ioOnClosedChannel)
}
// `channel2` still gets its own response, intact.
let response2Buffer = self.allocator.buffer(string: "Response #2")
XCTAssertNoThrow(try testServer.writeOutbound(.head(.init(version: .http1_1, status: .ok))))
XCTAssertNoThrow(try testServer.writeOutbound(.body(.byteBuffer(response2Buffer))))
XCTAssertNoThrow(try testServer.writeOutbound(.end(nil)))
XCTAssertNoThrow(XCTAssertEqual("Response #2", try response2Promise.futureResult.wait()))
#endif
}
}
private final class TestHTTPHandler: ChannelInboundHandler {