8000 [HttpClient] Fix CurlHttpClient memory leak by HypeMC · Pull Request #38493 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpClient] Fix CurlHttpClient memory leak #38493

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 1 commit into from
Oct 10, 2020
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
[HttpClient] Fix CurlHttpClient memory leak
  • Loading branch information
HypeMC authored and fabpot committed Oct 10, 2020
commit aeb4ddf31662e052bf74991e6e47f5dab179a8c5
10 changes: 3 additions & 7 deletions src/Symfony/Component/HttpClient/Response/CurlResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\Internal\ClientState;
use Symfony\Component\HttpClient\Internal\CurlClientState;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

/**
Expand Down Expand Up @@ -205,14 +204,9 @@ public function __destruct()
return; // Unused pushed response
}

$e = null;
$this->doDestruct();
} catch (HttpExceptionInterface $e) {
throw $e;
} finally {
if ($e ?? false) {
throw $e;
}
$multi = clone $this->multi;

$this->close();

Expand All @@ -221,6 +215,8 @@ public function __destruct()
$this->multi->dnsCache->evictions = $this->multi->dnsCache->evictions ?: $this->multi->dnsCache->removals;
$this->multi->dnsCache->removals = $this->multi->dnsCache->hostnames = [];
}

$this->multi = $multi;
}
}

Expand Down
10 changes: 3 additions & 7 deletions src/Symfony/Component/HttpClient/Response/NativeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\Internal\ClientState;
use Symfony\Component\HttpClient\Internal\NativeClientState;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

/**
Expand Down Expand Up @@ -87,14 +86,9 @@ public function getInfo(string $type = null)
public function __destruct()
{
try {
$e = null;
$this->doDestruct();
} catch (HttpExceptionInterface $e) {
throw $e;
} finally {
if ($e ?? false) {
throw $e;
}
$multi = clone $this->multi;

$this->close();

Expand All @@ -103,6 +97,8 @@ public function __destruct()
$this->multi->responseCount = 0;
$this->multi->dnsCache = [];
}

$this->multi = $multi;
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\Internal\ClientState;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Test\HttpClientTestCase as BaseHttpClientTestCase;

abstract class HttpClientTestCase extends BaseHttpClientTestCase
Expand Down Expand Up @@ -120,4 +122,26 @@ public function testTimeoutIsNotAFatalError()
throw $e;
}
}

public function testHandleIsRemovedOnException()
{
$client = $this->getHttpClient(__FUNCTION__);

try {
$client->request('GET', 'http://localhost:8057/304');
$this->fail(RedirectionExceptionInterface::class.' expected');
} catch (RedirectionExceptionInterface $e) {
// The response content-type mustn't be json as that calls getContent
// @see src/Symfony/Component/HttpClient/Exception/HttpExceptionTrait.php:58
$this->assertStringNotContainsString('json', $e->getResponse()->getHeaders(false)['content-type'][0] ?? '');

$r = new \ReflectionProperty($client, 'multi');
$r->setAccessible(true);
/** @var ClientState $clientState */
$clientState = $r->getValue($client);

$this->assertCount(0, $clientState->handlesActivity);
$this->assertCount(0, $clientState->openHandles);
}
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ protected function getHttpClient(string $testCase): HttpClientInterface
$this->markTestSkipped("MockHttpClient doesn't timeout on destruct");
break;

case 'testHandleIsRemovedOnException':
$this->markTestSkipped("MockHttpClient doesn't cache handles");
break;

case 'testGetRequest':
array_unshift($headers, 'HTTP/1.1 200 OK');
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Contracts/HttpClient/Test/Fixtures/web/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
exit;

case '/json':
header("Content-Type: application/json");
header('Content-Type: application/json');
echo json_encode([
'documents' => [
['id' => '/json/1'],
Expand All @@ -170,7 +170,7 @@
case '/json/1':
case '/json/2':
case '/json/3':
header("Content-Type: application/json");
header('Content-Type: application/json');
echo json_encode([
'title' => $vars['REQUEST_URI'],
]);
Expand Down
0