symfony: [HttpClient] Failed to open stream: Too many open files
Symfony version(s) affected
4.5.2 / 5.3.13
Description
When launching PHPUnit test suite, I have this error:
PHPUnit 9.5.11 by Sebastian Bergmann and contributors.
Testing
............................................................. 61 / 4733 ( 1%)
............................................................. 122 / 4733 ( 2%)
............................................................. 183 / 4733 ( 3%)
............................................................. 244 / 4733 ( 5%)
............................................................. 305 / 4733 ( 6%)
............................................................. 366 / 4733 ( 7%)
............................................................. 427 / 4733 ( 9%)
............................................................. 488 / 4733 ( 10%)
............................................................. 549 / 4733 ( 11%)
............................................................. 610 / 4733 ( 12%)
............................................................. 671 / 4733 ( 14%)
............................................................. 732 / 4733 ( 15%)
................PHP Warning: include(/var/www/project/symfony/vendor/phpunit/phpunit/src/Framework/Error/Warning.php): failed to open stream: Too many open files in /var/www/project/symfony/vendor/symfony/error-handler/DebugClassLoader.php on line 349
PHP Warning: include(): Failed opening '/var/www/project/symfony/vendor/composer/../phpunit/phpunit/src/Framework/Error/Warning.php' for inclusion (include_path='.:/usr/share/php:/var/www/project/symfony/vendor/deployer/recipes') in /var/www/project/symfony/vendor/symfony/error-handler/DebugClassLoader.php on line 349
PHP Warning: include(/var/www/project/symfony/vendor/phpunit/phpunit/src/Framework/Error/Warning.php): failed to open stream: Too many open files in /var/www/project/symfony/vendor/composer/ClassLoader.php on line 571
PHP Warning: include(): Failed opening '/var/www/project/symfony/vendor/composer/../phpunit/phpunit/src/Framework/Error/Warning.php' for inclusion (include_path='.:/usr/share/php:/var/www/project/symfony/vendor/deployer/recipes') in /var/www/project/symfony/vendor/composer/ClassLoader.php on line 571
Class 'PHPUnit\Framework\Error\Warning' not found
The issue is related to symfony/http-client, no more issue when reverting these 3 files to v5.3.12 :
- https://github.com/symfony/symfony/blob/v5.3.12/src/Symfony/Component/HttpClient/CurlHttpClient.php
- https://github.com/symfony/symfony/blob/v5.3.12/src/Symfony/Component/HttpClient/Internal/CurlClientState.php
- https://github.com/symfony/symfony/blob/v5.3.12/src/Symfony/Component/HttpClient/Response/CurlResponse.php
I tried to set up a reproducer but it seems the issue occurs only with projects having thousands of tests (4733 tests, 31650 assertions for my project).
How to reproduce
Upgrade to Symfony 5.3.13 and launch a large PHPUnit test suite with symfony/http-client use.
Possible Solution
No response
Additional Context
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 6
- Comments: 33 (27 by maintainers)
Commits related to this issue
- bug #45015 [HttpClient] fix resetting DNS/etc when calling CurlHttpClient::reset() (nicolas-grekas) This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] fix resetting DNS/etc ... — committed to symfony/symfony by nicolas-grekas 2 years ago
- Reproducer for https://github.com/symfony/symfony/issues/44900 — committed to javer/symfony-issue-44900 by javer 2 years ago
- bug #45073 [HttpClient] Fix Failed to open stream: Too many open files (adrienfr) This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] Fix Failed to open stream: Too many open... — committed to symfony/symfony by nicolas-grekas 2 years ago
Same as @javer I can confirm that any of those commits are the culprit.
I tested it on PHP 8.0.13 and PHP 8.0.14.
Can confirm the issue with PHP 8.1 also. Downgrading
symfony/http-clientto5.4.1fixes the issue.Git bisect between 5.4.1 and 5.4.2 shows that the first bad commit could be any of:
More precise bisecting (with fixing incompatible types in HttpClientDataCollector in intermediate commits) led me to the single first bad commit: https://github.com/symfony/http-client/commit/d05b20ee7e0214f50a4d7a5ba132dc56dd60547b
@nicolas-grekas Unfortunately the patch didn’t help. But my reproducer shows that we even don’t need to make any curl requests, because
CurlClientState::reset()is called byServicesResetter, so just having any HttpClient in the container is enough to get an error after many calls toServicesResetter::reset(), see backtrace:For example, in my application (test run)
ServicesResetter::reset()is called 502 times before getting the errorFailed to open stream: Too many open files, at the same time there weren’t any calls toCurlResponseconstructor, so it seems we cannot rely on the response to free the resources.@nicolas-grekas Here is a small isolated script to reproduce the issue (need
lsofutil andpcntlextension for verbose output):results in:
Changing
$verbosetotrueexposes the stream handles leak 2 per eachresetcall:@jtojnar ok, so you’re experiencing something else. Please open an issue with as many details as possible when you have some hints.