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
@@ -0,0 +1,7 @@
[
{
"type": "bugfix",
"category": "",
"description": "Fixed WrappedHttpHandler buffering the entire partial response body into memory when a 2xx response was attached to a rejected promise, which could exhaust memory on a transport failure during a large streamed download."
}
]
10 changes: 8 additions & 2 deletions src/WrappedHttpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,14 @@ private function parseError(

$serviceError = "AWS HTTP error:\n";

if (!isset($err['response'])) {
$parts = ['response' => null];
// A 2xx status on a rejected promise means the transport failed
// mid-body after a success response was received; there is no error
// document to parse, and parsing one would buffer the entire partial
// body into memory.
if (!isset($err['response'])
|| $err['response']->getStatusCode() < 300
) {
$parts = ['response' => $err['response'] ?? null];
$serviceError .= $err['exception']->getMessage();
} else {
try {
Expand Down
23 changes: 23 additions & 0 deletions tests/WrappedHttpHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,29 @@ public function testCanRejectWithoutResponse()
}
}

public function testDoesNotParseErrorWhenRejectedWithSuccessResponse()
{
$e = new \Exception('cURL error 18: transfer closed with outstanding read data remaining');
$cmd = new Command('foo');
$req = new Request('GET', 'http://foo.com');
$res = new Response(200, [], 'partial body');
$handler = function () use ($e, $res) {
return new RejectedPromise(['exception' => $e, 'response' => $res]);
};
$parser = $errorParser = [$this, 'fail'];
$wrapped = new WrappedHttpHandler($handler, $parser, $errorParser);
try {
$wrapped($cmd, $req)->wait();
$this->fail();
} catch (AwsException $e) {
$this->assertSame($cmd, $e->getCommand());
$this->assertSame($req, $e->getRequest());
$this->assertSame($res, $e->getResponse());
$this->assertNull($e->getResult());
$this->assertStringContainsString('cURL error 18', $e->getMessage());
}
}

#[DataProvider('responseAndParserProvider')]
public function testCanRejectWithAndParseResponse(
Response $res,
Expand Down