The Wayback Machine - https://web.archive.org/web/20241101141242/https://github.com/symfony/symfony/issues/46256
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 #46256

Closed
Gounlaf opened this issue May 4, 2022 · 4 comments
Closed

[HttpClient] memory leak #46256

Gounlaf opened this issue May 4, 2022 · 4 comments

Comments

@Gounlaf
Copy link

Gounlaf commented May 4, 2022

Symfony version(s) affected

6.0.8

Description

Hi,

I'm hunting a memory leak since few days and finally found where it might be.
Context: I've a loop that do some work. Some metrics are collected, and pushed via a http request to a prometheus pushgateway.

When the Psr18Client is handling the response body, the leak happens.

How to reproduce

Here you can find a MRE: https://github.com/Gounlaf/sf-http-client-memory-leak

It rely on docker & docker-compose; up the service debug and then check your processes. You will find that memory of php process is increasing with $psr18Client = new Psr18Client(new \Symfony\Component\HttpClient\NativeHttpClient());
or $psr18Client = new Psr18Client(new \Symfony\Component\HttpClient\CurlHttpClient());
It doesn't with another (not symfony) implementation $psr18Client = new \Http\Client\Curl\Client($psr17factory, $psr17factory);

If this MRE is not enough, please let me know.

Regards.

Possible Solution

No response

Additional Context

No response

@Gounlaf Gounlaf added the Bug label May 4, 2022
@Gounlaf Gounlaf changed the title http-client: memory leak HttpClient: memory leak May 5, 2022
@Gounlaf Gounlaf changed the title HttpClient: memory leak [HttpClient] memory leak May 5, 2022
@manueldimmler
Copy link

manueldimmler commented May 12, 2022

I was able to trace the memory leak to the following line:
https://github.com/symfony/symfony/blob/6.1/src/Symfony/Component/HttpClient/Response/StreamWrapper.php#L72

To me it looks like it's a problem with fopen in conjunction with a stream wrapper. The memory usage does not increase continuously, but jumps sometimes to a high value and then falls back again. The distances between the jumps are getting bigger each time:

@manueldimmler
Copy link

memory_usage.txt

@manueldimmler
Copy link

It doesn't look like an error in the HttpClient to me, but in the PHP source code. So I opened a ticket there.

@nicolas-grekas
Copy link
Member

This should be fixed by #47143

nicolas-grekas added a commit that referenced this issue Aug 1, 2022
…olas-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[HttpClient] Fix memory leak when using StreamWrapper

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

As hinted in php/php-src#8548

Commits
-------

16d0176 [HttpClient] Fix memory leak when using StreamWrapper
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