Skip to content

Correctly close the connection if sendEnd fails #599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ final class HTTP1ClientChannelHandler: ChannelDuplexHandler {

oldRequest.succeedRequest(buffer)
case .failure(let error):
context.close(promise: nil)
oldRequest.fail(error)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extension HTTP1ClientChannelHandlerTests {
("testIdleReadTimeoutIsCanceledIfRequestIsCanceled", testIdleReadTimeoutIsCanceledIfRequestIsCanceled),
("testFailHTTPRequestWithContentLengthBecauseOfChannelInactiveWaitingForDemand", testFailHTTPRequestWithContentLengthBecauseOfChannelInactiveWaitingForDemand),
("testWriteHTTPHeadFails", testWriteHTTPHeadFails),
("testHandlerClosesChannelIfLastActionIsSendEndAndItFails", testHandlerClosesChannelIfLastActionIsSendEndAndItFails),
]
}
}
85 changes: 85 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTP1ClientChannelHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,75 @@ class HTTP1ClientChannelHandlerTests: XCTestCase {
XCTAssertEqual(embedded.isActive, false)
}
}

func testHandlerClosesChannelIfLastActionIsSendEndAndItFails() {
let embedded = EmbeddedChannel()
let testWriter = TestBackpressureWriter(eventLoop: embedded.eventLoop, parts: 5)
var maybeTestUtils: HTTP1TestTools?
XCTAssertNoThrow(maybeTestUtils = try embedded.setupHTTP1Connection())
guard let testUtils = maybeTestUtils else { return XCTFail("Expected connection setup works") }

var maybeRequest: HTTPClient.Request?
XCTAssertNoThrow(maybeRequest = try HTTPClient.Request(url: "http://localhost/", method: .POST, body: .stream(length: 10) { writer in
testWriter.start(writer: writer)
}))
guard let request = maybeRequest else { return XCTFail("Expected to be able to create a request") }

let delegate = ResponseAccumulator(request: request)
var maybeRequestBag: RequestBag<ResponseAccumulator>?
XCTAssertNoThrow(maybeRequestBag = try RequestBag(
request: request,
eventLoopPreference: .delegate(on: embedded.eventLoop),
task: .init(eventLoop: embedded.eventLoop, logger: testUtils.logger),
redirectHandler: nil,
connectionDeadline: .now() + .seconds(30),
requestOptions: .forTests(idleReadTimeout: .milliseconds(200)),
delegate: delegate
))
guard let requestBag = maybeRequestBag else { return XCTFail("Expected to be able to create a request bag") }

XCTAssertNoThrow(try embedded.pipeline.addHandler(FailEndHandler(), position: .first).wait())

// Execute the request and we'll receive the head.
testWriter.writabilityChanged(true)
testUtils.connection.executeRequest(requestBag)
XCTAssertNoThrow(try embedded.receiveHeadAndVerify {
XCTAssertEqual($0.method, .POST)
XCTAssertEqual($0.uri, "/")
XCTAssertEqual($0.headers.first(name: "host"), "localhost")
XCTAssertEqual($0.headers.first(name: "content-length"), "10")
})
// We're going to immediately send the response head and end.
let responseHead = HTTPResponseHead(version: .http1_1, status: .ok)
XCTAssertNoThrow(try embedded.writeInbound(HTTPClientResponsePart.head(responseHead)))
embedded.read()

// Send the end and confirm the connection is still live.
XCTAssertNoThrow(try embedded.writeInbound(HTTPClientResponsePart.end(nil)))
XCTAssertEqual(testUtils.connectionDelegate.hitConnectionClosed, 0)
XCTAssertEqual(testUtils.connectionDelegate.hitConnectionReleased, 0)

// Ok, now we can process some reads. We expect 5 reads, but we do _not_ expect an .end, because
// the `FailEndHandler` is going to fail it.
embedded.embeddedEventLoop.run()
XCTAssertEqual(testWriter.written, 5)
for _ in 0..<5 {
XCTAssertNoThrow(try embedded.receiveBodyAndVerify {
XCTAssertEqual($0.readableBytes, 2)
})
}

embedded.embeddedEventLoop.run()
XCTAssertNil(try embedded.readOutbound(as: HTTPClientRequestPart.self))

// We should have seen the connection close, and the request is complete.
XCTAssertEqual(testUtils.connectionDelegate.hitConnectionClosed, 1)
XCTAssertEqual(testUtils.connectionDelegate.hitConnectionReleased, 0)

XCTAssertThrowsError(try requestBag.task.futureResult.wait()) { error in
XCTAssertTrue(error is FailEndHandler.Error)
}
}
}

class TestBackpressureWriter {
Expand Down Expand Up @@ -636,3 +705,19 @@ class ReadEventHitHandler: ChannelOutboundHandler {
context.read()
}
}

final class FailEndHandler: ChannelOutboundHandler {
typealias OutboundIn = HTTPClientRequestPart
typealias OutboundOut = HTTPClientRequestPart

struct Error: Swift.Error {}

func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
if case .end = self.unwrapOutboundIn(data) {
// We fail this.
promise?.fail(Self.Error())
} else {
context.write(data, promise: promise)
}
}
}