Skip to content

[HttpClient] Fix activity tracking leading to negative timeout errors #59727

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
Feb 10, 2025
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
20 changes: 9 additions & 11 deletions src/Symfony/Component/HttpClient/Response/AmpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,17 @@
/**
* @param AmpClientState $multi
*/
private static function perform(ClientState $multi, ?array &$responses = null): void
private static function perform(ClientState $multi, ?array $responses = null): void

Check failure on line 182 in src/Symfony/Component/HttpClient/Response/AmpResponse.php

View workflow job for this annotation

GitHub Actions / Psalm

OverriddenMethodAccess

src/Symfony/Component/HttpClient/Response/AmpResponse.php:182:5: OverriddenMethodAccess: Method Symfony\Component\HttpClient\Response\AmpResponse::perform has different access level than Symfony\Component\HttpClient\Response\TransportResponseTrait::perform (see https://psalm.dev/066)

Check failure on line 182 in src/Symfony/Component/HttpClient/Response/AmpResponse.php

View workflow job for this annotation

GitHub Actions / Psalm

OverriddenMethodAccess

src/Symfony/Component/HttpClient/Response/AmpResponse.php:182:5: OverriddenMethodAccess: Method Symfony\Component\HttpClient\Response\AmpResponse::perform has different access level than Symfony\Component\HttpClient\Response\TransportResponseTrait::perform (see https://psalm.dev/066)
{
if ($responses) {
foreach ($responses as $response) {
try {
if ($response->info['start_time']) {
$response->info['total_time'] = microtime(true) - $response->info['start_time'];
($response->onProgress)();
}
} catch (\Throwable $e) {
$multi->handlesActivity[$response->id][] = null;
$multi->handlesActivity[$response->id][] = $e;
foreach ($responses ?? [] as $response) {
try {
if ($response->info['start_time']) {
$response->info['total_time'] = microtime(true) - $response->info['start_time'];
($response->onProgress)();
}
} catch (\Throwable $e) {
$multi->handlesActivity[$response->id][] = null;
$multi->handlesActivity[$response->id][] = $e;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpClient/Response/CurlResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@
/**
* @param CurlClientState $multi
*/
private static function perform(ClientState $multi, ?array &$responses = null): void
private static function perform(ClientState $multi, ?array $responses = null): void

Check failure on line 268 in src/Symfony/Component/HttpClient/Response/CurlResponse.php

View workflow job for this annotation

GitHub Actions / Psalm

OverriddenMethodAccess

src/Symfony/Component/HttpClient/Response/CurlResponse.php:268:5: OverriddenMethodAccess: Method Symfony\Component\HttpClient\Response\CurlResponse::perform has different access level than Symfony\Component\HttpClient\Response\TransportResponseTrait::perform (see https://psalm.dev/066)

Check failure on line 268 in src/Symfony/Component/HttpClient/Response/CurlResponse.php

View workflow job for this annotation

GitHub Actions / Psalm

OverriddenMethodAccess

src/Symfony/Component/HttpClient/Response/CurlResponse.php:268:5: OverriddenMethodAccess: Method Symfony\Component\HttpClient\Response\CurlResponse::perform has different access level than Symfony\Component\HttpClient\Response\TransportResponseTrait::perform (see https://psalm.dev/066)
{
if ($multi->performing) {
if ($responses) {
$response = current($responses);
$response = $responses[array_key_first($responses)];
$multi->handlesActivity[(int) $response->handle][] = null;
$multi->handlesActivity[(int) $response->handle][] = new TransportException(sprintf('Userland callback cannot use the client nor the response while processing "%s".', curl_getinfo($response->handle, \CURLINFO_EFFECTIVE_URL)));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpClient/Response/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected static function schedule(self $response, array &$runningResponses): vo
$runningResponses[0][1][$response->id] = $response;
}

protected static function perform(ClientState $multi, array &$responses): void
protected static function perform(ClientState $multi, array $responses): void
{
foreach ($responses as $response) {
$id = $response->id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
/**
* @param NativeClientState $multi
*/
private static function perform(ClientState $multi, ?array &$responses = null): void
private static function perform(ClientState $multi, ?array $responses = null): void

Check failure on line 231 in src/Symfony/Component/HttpClient/Response/NativeResponse.php

View workflow job for this annotation

GitHub Actions / Psalm

OverriddenMethodAccess

src/Symfony/Component/HttpClient/Response/NativeResponse.php:231:5: OverriddenMethodAccess: Method Symfony\Component\HttpClient\Response\NativeResponse::perform has different access level than Symfony\Component\HttpClient\Response\TransportResponseTrait::perform (see https://psalm.dev/066)

Check failure on line 231 in src/Symfony/Component/HttpClient/Response/NativeResponse.php

View workflow job for this annotation

GitHub Actions / Psalm

OverriddenMethodAccess

src/Symfony/Component/HttpClient/Response/NativeResponse.php:231:5: OverriddenMethodAccess: Method Symfony\Component\HttpClient\Response\NativeResponse::perform has different access level than Symfony\Component\HttpClient\Response\TransportResponseTrait::perform (see https://psalm.dev/066)
{
foreach ($multi->openHandles as $i => [$pauseExpiry, $h, $buffer, $onProgress]) {
if ($pauseExpiry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
/**
* Performs all pending non-blocking operations.
*/
abstract protected static function perform(ClientState $multi, array &$responses): void;
abstract protected static function perform(ClientState $multi, array $responses): void;

Check failure on line 95 in src/Symfony/Component/HttpClient/Response/TransportResponseTrait.php

View workflow job for this annotation

GitHub Actions / Psalm

OverriddenMethodAccess

src/Symfony/Component/HttpClient/Response/TransportResponseTrait.php:95:5: OverriddenMethodAccess: Method Symfony\Component\HttpClient\Response\AmpResponse::perform has different access level than Symfony\Component\HttpClient\Response\TransportResponseTrait::perform (see https://psalm.dev/066)

Check failure on line 95 in src/Symfony/Component/HttpClient/Response/TransportResponseTrait.php

View workflow job for this annotation

GitHub Actions / Psalm

OverriddenMethodAccess

src/Symfony/Component/HttpClient/Response/TransportResponseTrait.php:95:5: OverriddenMethodAccess: Method Symfony\Component\HttpClient\Response\CurlResponse::perform has different access level than Symfony\Component\HttpClient\Response\TransportResponseTrait::perform (see https://psalm.dev/066)

Check failure on line 95 in src/Symfony/Component/HttpClient/Response/TransportResponseTrait.php

View workflow job for this annotation

GitHub Actions / Psalm

OverriddenMethodAccess

src/Symfony/Component/HttpClient/Response/TransportResponseTrait.php:95:5: OverriddenMethodAccess: Method Symfony\Component\HttpClient\Response\NativeResponse::perform has different access level than Symfony\Component\HttpClient\Response\TransportResponseTrait::perform (see https://psalm.dev/066)

Check failure on line 95 in src/Symfony/Component/HttpClient/Response/TransportResponseTrait.php

View workflow job for this annotation

GitHub Actions / Psalm

OverriddenMethodAccess

src/Symfony/Component/HttpClient/Response/TransportResponseTrait.php:95:5: OverriddenMethodAccess: Method Symfony\Component\HttpClient\Response\AmpResponse::perform has different access level than Symfony\Component\HttpClient\Response\TransportResponseTrait::perform (see https://psalm.dev/066)

/**
* Waits for network activity.
Expand Down Expand Up @@ -150,10 +150,15 @@
$lastActivity = hrtime(true) / 1E9;
$elapsedTimeout = 0;

if ($fromLastTimeout = 0.0 === $timeout && '-0' === (string) $timeout) {
$timeout = null;
} elseif ($fromLastTimeout = 0 > $timeout) {
$timeout = -$timeout;
if ((0.0 === $timeout && '-0' === (string) $timeout) || 0 > $timeout) {
$timeout = $timeout ? -$timeout : null;

/** @var ClientState $multi */
foreach ($runningResponses as [$multi]) {
if (null !== $multi->lastTimeout) {
$elapsedTimeout = max($elapsedTimeout, $lastActivity - $multi->lastTimeout);
}
}
}

while (true) {
Expand All @@ -162,35 +167,33 @@
$timeoutMin = $timeout ?? \INF;

/** @var ClientState $multi */
foreach ($runningResponses as $i => [$multi]) {
$responses = &$runningResponses[$i][1];
foreach ($runningResponses as $i => [$multi, &$responses]) {
self::perform($multi, $responses);

foreach ($responses as $j => $response) {
$timeoutMax = $timeout ?? max($timeoutMax, $response->timeout);
$timeoutMin = min($timeoutMin, $response->timeout, 1);
$chunk = false;

if ($fromLastTimeout && null !== $multi->lastTimeout) {
$elapsedTimeout = hrtime(true) / 1E9 - $multi->lastTimeout;
}

if (isset($multi->handlesActivity[$j])) {
$multi->lastTimeout = null;
$elapsedTimeout = 0;
} elseif (!isset($multi->openHandles[$j])) {
$hasActivity = true;
unset($responses[$j]);
continue;
} elseif ($elapsedTimeout >= $timeoutMax) {
$multi->handlesActivity[$j] = [new ErrorChunk($response->offset, sprintf('Idle timeout reached for "%s".', $response->getInfo('url')))];
$multi->lastTimeout ??= $lastActivity;
$elapsedTimeout = $timeoutMax;
} else {
continue;
}

while ($multi->handlesActivity[$j] ?? false) {
$hasActivity = true;
$elapsedTimeout = 0;
$lastActivity = null;
$hasActivity = true;

while ($multi->handlesActivity[$j] ?? false) {
if (\is_string($chunk = array_shift($multi->handlesActivity[$j]))) {
if (null !== $response->inflate && false === $chunk = @inflate_add($response->inflate, $chunk)) {
$multi->handlesActivity[$j] = [null, new TransportException(sprintf('Error while processing content unencoding for "%s".', $response->getInfo('url')))];
Expand Down Expand Up @@ -227,7 +230,6 @@
}
} elseif ($chunk instanceof ErrorChunk) {
unset($responses[$j]);
$elapsedTimeout = $timeoutMax;
} elseif ($chunk instanceof FirstChunk) {
if ($response->logger) {
$info = $response->getInfo();
Expand Down Expand Up @@ -274,10 +276,12 @@
if ($chunk instanceof ErrorChunk && !$chunk->didThrow()) {
// Ensure transport exceptions are always thrown
$chunk->getContent();
throw new \LogicException('A transport exception should have been thrown.');
}
}

if (!$responses) {
$hasActivity = true;
unset($runningResponses[$i]);
}

Expand All @@ -291,7 +295,7 @@
}

if ($hasActivity) {
$lastActivity = hrtime(true) / 1E9;
$lastActivity ??= hrtime(true) / 1E9;
continue;
}

Expand Down
Loading