Skip to content
Open
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 @@ -244,9 +244,9 @@ final class HTTP2ClientRequestHandler: ChannelDuplexHandler {
self.request!.receiveResponseBodyParts(parts)

case .failRequest(let error, let finalAction):
// We can force unwrap the request here, as we have just validated in the state machine,
// that the request object is still present.
self.request!.fail(error)
// The request may already have completed after receiving a response end while its body
// was still streaming. A subsequent stream error must not fail that request twice.
self.request?.fail(error)
self.request = nil
self.runTimeoutAction(.clearIdleReadTimeoutTimer, context: context)
self.runTimeoutAction(.clearIdleWriteTimeoutTimer, context: context)
Expand Down
39 changes: 39 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTP2ClientRequestHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -622,4 +622,43 @@ class HTTP2ClientRequestHandlerTests: XCTestCase {
)
}

func testErrorAfterResponseEndWhileRequestBodyIsStreamingDoesNotCrash() throws {
struct TestError: Error {}

let eventLoop = EmbeddedEventLoop()
let handler = HTTP2ClientRequestHandler(eventLoop: eventLoop)
let channel = EmbeddedChannel(handlers: [handler], loop: eventLoop)
try channel.connect(to: .init(ipAddress: "127.0.0.1", port: 80)).wait()

let request = MockHTTPExecutableRequest(
head: .init(version: .http1_1, method: .POST, uri: "http://localhost/"),
framingMetadata: .init(connectionClose: false, body: .stream),
raiseErrorIfUnimplementedMethodIsCalled: false
)

channel.write(request, promise: nil)
XCTAssertEqual(try channel.readOutbound(as: HTTPClientRequestPart.self), .head(request.requestHead))

channel.read()
try channel.writeInbound(
HTTPClientResponsePart.head(
.init(version: .http1_1, status: .ok, headers: HTTPHeaders([("content-length", "0")]))
)
)
channel.read()
try channel.writeInbound(HTTPClientResponsePart.end(nil))
XCTAssertEqual(request.events.last?.kind, .receiveResponseEnd)

channel.pipeline.fireErrorCaught(TestError())

XCTAssertFalse(channel.isActive)
XCTAssertEqual(
request.events.map(\.kind),
[
.willExecuteRequest, .requestHeadSent, .resumeRequestBodyStream, .receiveResponseHead,
.receiveResponseEnd,
]
)
}

}