Skip to content
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

[HttpClient] Memory leak in CurlHttpClient #38492

Closed
HypeMC opened this issue Oct 8, 2020 · 1 comment
Closed

[HttpClient] Memory leak in CurlHttpClient #38492

HypeMC opened this issue Oct 8, 2020 · 1 comment

Comments

@HypeMC
Copy link
Contributor

HypeMC commented Oct 8, 2020

Symfony version(s) affected: 4.4

Description
The CurlHttpClient has a memory leak which seems to have been caused by this PR #35674 .
When an exception occurs the CurlResponse::close method never gets called from __destruct cause of the PR & the handler never gets unset from the CurlClientState::$openHandles & CurlClientState::$handlesActivity arrays. Over time this leads to a lot of handlers piling up in the memory, causing a memory leak. I've notice this while using the HttpClient in a Messenger handler.

How to reproduce

class MyMessageHandler implements MessageHandlerInterface
{
    //...

    public function doSomething(MyMessage $myMessage): void
    {
        //...
        try {
            $this->httpClient->request(/*...*/);
        } catch (HttpExceptionInterface $e) {
            //...
        }
    }
}

Additional context
image

@Nyholm
Copy link
Member

Nyholm commented Oct 9, 2020

I can confirm that symfony/http-client: 4.4.15 and 5.x-dev has this issue.

<?php

use Symfony\Component\HttpClient\CurlHttpClient;

include __DIR__.'/vendor/autoload.php';

$client = new CurlHttpClient();

$start = 0;
for($i=0; $i<20; $i++) {
    try {
        $client->request('POST', 'https://example.com/foo');
    } catch (\Throwable $exception) {

    }

    if ($i === 5) {
        $start = memory_get_usage();
    }
}
$end = memory_get_usage();

if ($start < $end) {
    echo 'MEMORY LEAK!!'. PHP_EOL;
}

echo 'Start:', $start / 1024 / 1024, PHP_EOL;
echo 'End:', $end / 1024 / 1024, PHP_EOL;

@fabpot fabpot closed this as completed Oct 10, 2020
fabpot added a commit that referenced this issue Oct 10, 2020
This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[HttpClient] Fix CurlHttpClient memory leak

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #38492
| License       | MIT
| Doc PR        | -

Commits
-------

aeb4ddf [HttpClient] Fix CurlHttpClient memory leak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants