Skip to content

Commit

Permalink
bug #45073 [HttpClient] Fix Failed to open stream: Too many open file…
Browse files Browse the repository at this point in the history
…s (adrienfr)

This PR was merged into the 4.4 branch.

Discussion
----------

[HttpClient] Fix Failed to open stream: Too many open files

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

With a large PHPUnit tests suite, I receive this error "Failed to open stream: Too many open files".
I found the original code from `@nicolas`-grekas [here](https://github.com/symfony/symfony/pull/30413/files#diff-bbfff9335ca4f0716566920d7f427036c2d6f9ac2a9cb33e2c83514423ab4b14R77) and with small adjustments, it resolves my issue.

Warning: tests for HttpClient are OK but would someone try with a real HTTP/2 client push? I was not able to test it.

Commits
-------

40d22b8 [HttpClient] Fix Failed to open stream: Too many open files
  • Loading branch information
nicolas-grekas committed Jan 19, 2022
2 parents 3838696 + 40d22b8 commit d0a8092
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/Symfony/Component/HttpClient/Internal/CurlClientState.php
Expand Up @@ -23,9 +23,9 @@
*/
final class CurlClientState extends ClientState
{
/** @var \CurlMultiHandle|resource */
/** @var \CurlMultiHandle|resource|null */
public $handle;
/** @var \CurlShareHandle|resource */
/** @var \CurlShareHandle|resource|null */
public $share;
/** @var PushedResponse[] */
public $pushedResponses = [];
Expand Down Expand Up @@ -65,8 +65,17 @@ public function __construct(int $maxHostConnections, int $maxPendingPushes)
return;
}

curl_multi_setopt($this->handle, \CURLMOPT_PUSHFUNCTION, function ($parent, $pushed, array $requestHeaders) use ($maxPendingPushes) {
return $this->handlePush($parent, $pushed, $requestHeaders, $maxPendingPushes);
// Clone to prevent a circular reference
$multi = clone $this;
$multi->handle = null;
$multi->share = null;
$multi->pushedResponses = &$this->pushedResponses;
$multi->logger = &$this->logger;
$multi->handlesActivity = &$this->handlesActivity;
$multi->openHandles = &$this->openHandles;

curl_multi_setopt($this->handle, \CURLMOPT_PUSHFUNCTION, static function ($parent, $pushed, array $requestHeaders) use ($multi, $maxPendingPushes) {
return $multi->handlePush($parent, $pushed, $requestHeaders, $maxPendingPushes);
});
}

Expand Down

0 comments on commit d0a8092

Please sign in to comment.