From 95ff3a845fe59f9a31ba95e77fccd24a348775eb Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 2 Dec 2026 14:46:25 +0000 Subject: [PATCH] cURL: Defer redirects in request_multiple() until all transfers finished Responses were parsed as soon as their transfer finished, and parsing a redirect follows it with a new, blocking request. While that request ran, the other transfers of the batch were not serviced, but their timeouts kept running, so they could time out although their server had answered in time. Parse the responses that will be redirected once all transfers have finished instead. The redirects themselves are still followed one after the other, as before. Also stop instead of looping forever when `curl_multi_exec()` reports an error, and report the error for each unfinished transfer. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5orPkSjWrCmXNcVAbH --- src/Transport/Curl.php | 67 +++++++++++++++++++++++++++++++- tests/Transport/BaseTestCase.php | 26 +++++++++++++ 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/src/Transport/Curl.php b/src/Transport/Curl.php index 215b77e97..2c4c2a33f 100644 --- a/src/Transport/Curl.php +++ b/src/Transport/Curl.php @@ -287,6 +287,9 @@ public function request_multiple($requests, $options) { $request['options']['hooks']->dispatch('curl.before_multi_exec', [&$multihandle]); + // Responses that will be redirected, parsed once all transfers have finished. + $redirected = []; + do { $active = 0; @@ -294,6 +297,26 @@ public function request_multiple($requests, $options) { $status = curl_multi_exec($multihandle, $active); } while ($status === CURLM_CALL_MULTI_PERFORM); + if ($status !== CURLM_OK) { + // The transfers cannot progress anymore, so stop instead of looping forever. + $exception = new CurlException( + curl_multi_strerror($status), + CurlException::MULTI, + $multihandle, + $status + ); + + foreach ($subhandles as $key => $subhandle) { + if (!isset($responses[$key]) && !isset($redirected[$key])) { + $responses[$key] = $exception; + $requests[$key]['options']['hooks']->dispatch('transport.internal.parse_error', [&$responses[$key], $requests[$key]]); + $requests[$key]['options']['hooks']->dispatch('multiple.request.complete', [&$responses[$key], $key]); + } + } + + break; + } + $to_process = []; // Read the information as needed @@ -321,7 +344,17 @@ public function request_multiple($requests, $options) { } else { $responses[$key] = $subrequests[$key]->process_response($subrequests[$key]->response_data, $options); - $options['hooks']->dispatch('transport.internal.parse_response', [&$responses[$key], $requests[$key]]); + if ($this->will_follow_redirect($subrequests[$key]->info, $options)) { + /* + * Following a redirect sends a new request, which blocks. Doing that now + * would stall the other transfers, which can then time out, so do it once + * they have all finished. + */ + $redirected[$key] = $responses[$key]; + unset($responses[$key]); + } else { + $options['hooks']->dispatch('transport.internal.parse_response', [&$responses[$key], $requests[$key]]); + } } curl_multi_remove_handle($multihandle, $done['handle']); @@ -330,7 +363,7 @@ public function request_multiple($requests, $options) { curl_close($done['handle']); } - if (!is_string($responses[$key])) { + if (isset($responses[$key]) && !is_string($responses[$key])) { $options['hooks']->dispatch('multiple.request.complete', [&$responses[$key], $key]); } @@ -338,6 +371,16 @@ public function request_multiple($requests, $options) { } } while ($active || $completed < $subrequestcount); + foreach ($redirected as $key => $response) { + $options = $requests[$key]['options']; + $responses[$key] = $response; + $options['hooks']->dispatch('transport.internal.parse_response', [&$responses[$key], $requests[$key]]); + + if (!is_string($responses[$key])) { + $options['hooks']->dispatch('multiple.request.complete', [&$responses[$key], $key]); + } + } + $request['options']['hooks']->dispatch('curl.after_multi_exec', [&$multihandle]); curl_multi_close($multihandle); @@ -345,6 +388,26 @@ public function request_multiple($requests, $options) { return $responses; } + /** + * Determine whether a finished transfer is a redirect that will be followed. + * + * This mirrors the conditions under which {@see \WpOrg\Requests\Requests::parse_response()} + * follows a redirect, using the information cURL collected about the transfer. + * + * @param array|null $info Transfer information, as returned by curl_getinfo(). + * @param array $options Request options. + * @return bool + */ + private function will_follow_redirect($info, $options) { + if (!is_array($info) || empty($options['follow_redirects']) || $options['blocking'] === false) { + return false; + } + + $code = isset($info['http_code']) ? (int) $info['http_code'] : 0; + + return $code >= 300 && $code < 400 && $code !== 304 && $code !== 305 && $code !== 306; + } + /** * Get the cURL handle for use in a multi-request * diff --git a/tests/Transport/BaseTestCase.php b/tests/Transport/BaseTestCase.php index bbe6cf40e..9b3f531ca 100644 --- a/tests/Transport/BaseTestCase.php +++ b/tests/Transport/BaseTestCase.php @@ -994,6 +994,32 @@ public function testMultiple() { $this->assertEmpty($result['args']); } + /** + * Following a redirect in a batch must not stall the other transfers of the batch, + * which could then time out although their server answered in time. + */ + public function testMultipleRedirectDoesNotStallOtherRequests() { + $requests = [ + 'redirect' => [ + 'url' => $this->httpbin('/redirect-to?url=' . rawurlencode($this->httpbin('/delay/3'))), + ], + 'fast' => [ + 'url' => $this->httpbin('/delay/1'), + 'options' => [ + 'timeout' => 2, + ], + ], + ]; + $responses = Requests::request_multiple($requests, $this->getOptions(['timeout' => 10])); + + $this->assertInstanceOf(Response::class, $responses['fast'], 'The request that answers within its timeout should succeed.'); + $this->assertSame(200, $responses['fast']->status_code); + + $this->assertInstanceOf(Response::class, $responses['redirect'], 'The redirected request should succeed.'); + $this->assertSame(200, $responses['redirect']->status_code); + $this->assertSame(1, $responses['redirect']->redirects); + } + public function testMultipleWithDifferingMethods() { $requests = [ 'get' => [