Skip to content

Tollerate more data after request body is cancelled #617

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 7 commits into from
Aug 18, 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
18 changes: 11 additions & 7 deletions Sources/AsyncHTTPClient/AsyncAwait/Transaction+StateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -429,17 +429,20 @@ extension Transaction {
case .executing(_, _, .waitingForResponseHead):
preconditionFailure("If we receive a response body, we must have received a head before")

case .executing(let context, let requestState, .buffering(let streamID, var currentBuffer, next: let next)):
guard case .askExecutorForMore = next else {
preconditionFailure("If we have received an error or eof before, why did we get another body part? Next: \(next)")
}
case .executing(_, _, .buffering(_, _, next: .endOfFile)):
preconditionFailure("If we have received an eof before, why did we get another body part?")

case .executing(_, _, .buffering(_, _, next: .error)):
// we might still get pending buffers if the user has canceled the request
return .none

case .executing(let context, let requestState, .buffering(let streamID, var currentBuffer, next: .askExecutorForMore)):
if currentBuffer.isEmpty {
currentBuffer = buffer
} else {
currentBuffer.append(contentsOf: buffer)
}
self.state = .executing(context, requestState, .buffering(streamID, currentBuffer, next: next))
self.state = .executing(context, requestState, .buffering(streamID, currentBuffer, next: .askExecutorForMore))
return .none

case .executing(let executor, let requestState, .waitingForResponseIterator(var currentBuffer, next: let next)):
Expand Down Expand Up @@ -690,10 +693,11 @@ extension Transaction {
case .finished:
// the request failed or was cancelled before, we can ignore all events
return .none

case .executing(_, _, .buffering(_, _, next: .error)):
// we might still get pending buffers if the user has canceled the request
return .none
case .executing(_, _, .waitingForResponseIterator(_, next: .error)),
.executing(_, _, .waitingForResponseIterator(_, next: .endOfFile)),
.executing(_, _, .buffering(_, _, next: .error)),
.executing(_, _, .buffering(_, _, next: .endOfFile)),
.executing(_, _, .finished(_, _)):
preconditionFailure("Already received an eof or error before. Must not receive further events. Invalid state: \(self.state)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ extension AsyncAwaitEndToEndTests {
("testInvalidURL", testInvalidURL),
("testRedirectChangesHostHeader", testRedirectChangesHostHeader),
("testShutdown", testShutdown),
("testCancelingBodyDoesNotCrash", testCancelingBodyDoesNotCrash),
]
}
}
20 changes: 20 additions & 0 deletions Tests/AsyncHTTPClientTests/AsyncAwaitEndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,26 @@ final class AsyncAwaitEndToEndTests: XCTestCase {
}
#endif
}

/// Regression test for https://github.com/swift-server/async-http-client/issues/612
func testCancelingBodyDoesNotCrash() {
#if compiler(>=5.5.2) && canImport(_Concurrency)
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { return }
XCTAsyncTest {
let client = makeDefaultHTTPClient()
defer { XCTAssertNoThrow(try client.syncShutdown()) }
let bin = HTTPBin(.http2(compress: true))
defer { XCTAssertNoThrow(try bin.shutdown()) }

let request = HTTPClientRequest(url: "https://127.0.0.1:\(bin.port)/mega-chunked")
let response = try await client.execute(request, deadline: .now() + .seconds(10))

await XCTAssertThrowsError(try await response.body.collect(upTo: 100)) { error in
XCTAssert(error is NIOTooManyBytesError)
}
}
#endif
}
}

#if compiler(>=5.5.2) && canImport(_Concurrency)
Expand Down