Skip to content

Handle ResponseAccumulator not being able to buffer large response in memory #631

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

Closed
wants to merge 2 commits into from
Closed
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
10 changes: 10 additions & 0 deletions Sources/AsyncHTTPClient/HTTPHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,12 @@ public class ResponseAccumulator: HTTPClientResponseDelegate {
case error(Error)
}

public struct ResponseTooBigError: Error, CustomStringConvertible {
public var description: String {
return "ResponseTooBigError: writing response part would exceed internal storage capacity."
}
}

var state = State.idle
let request: HTTPClient.Request

Expand Down Expand Up @@ -397,6 +403,10 @@ public class ResponseAccumulator: HTTPClientResponseDelegate {
case .head(let head):
self.state = .body(head, part)
case .body(let head, var body):
if body.writerIndex + part.readableBytes > UInt32.max {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UInt32.max is a bit magic. I think the best solution would be to add a static property in swift-nio to ByteBuffer with the max size it can handle. But we can also just give this constant a descriptive name here and document it.

return task.eventLoop.makeFailedFuture(ResponseTooBigError())
}

// The compiler can't prove that `self.state` is dead here (and it kinda isn't, there's
// a cross-module call in the way) so we need to drop the original reference to `body` in
// `self.state` or we'll get a CoW. To fix that we temporarily set the state to `.end` (which
Expand Down