From 6a3f101870e0b48566a9df840a0395459e03c0f2 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 2 Dec 2026 14:43:29 +0000 Subject: [PATCH] cURL: Wait for activity instead of busy-looping in request_multiple() `Curl::request_multiple()` polled `curl_multi_exec()` in a busy loop, keeping a CPU core fully busy for as long as the slowest transfer of the batch took. Wait for activity with `curl_multi_select()` instead. Older PHP versions return -1 from `curl_multi_select()` straight away when cURL has no file descriptors to wait on yet, so sleep briefly in that case. The new test compares the CPU time a batch uses with its wall time, with two slow requests, and with a refused connection next to a slow request. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5orPkSjWrCmXNcVAbH --- src/Transport/Curl.php | 11 ++++ tests/Transport/Curl/CurlTest.php | 89 +++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/src/Transport/Curl.php b/src/Transport/Curl.php index 215b77e97..fd57ffd4a 100644 --- a/src/Transport/Curl.php +++ b/src/Transport/Curl.php @@ -336,6 +336,17 @@ public function request_multiple($requests, $options) { ++$completed; } + + if ($active > 0 && $completed < $subrequestcount) { + // Wait for activity on any of the transfers instead of polling in a busy loop. + if (curl_multi_select($multihandle, 1.0) === -1) { + /* + * Older PHP versions return -1 straight away when cURL has no file descriptors + * to wait on yet, see https://bugs.php.net/76480. Sleep briefly instead of spinning. + */ + usleep(1000); + } + } } while ($active || $completed < $subrequestcount); $request['options']['hooks']->dispatch('curl.after_multi_exec', [&$multihandle]); diff --git a/tests/Transport/Curl/CurlTest.php b/tests/Transport/Curl/CurlTest.php index 8068302e4..e82fed0d4 100644 --- a/tests/Transport/Curl/CurlTest.php +++ b/tests/Transport/Curl/CurlTest.php @@ -4,6 +4,7 @@ use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; +use WpOrg\Requests\Response; use WpOrg\Requests\Tests\Transport\BaseTestCase; use WpOrg\Requests\Transport\Curl; @@ -137,4 +138,92 @@ public function testSetsEmptyExpectHeaderIfBodySmallerThan1Mb() { $this->assertFalse(isset($result['headers']['Expect'])); } + + /** + * Waiting for the transfers of a batch must not keep a CPU core busy. + * + * @dataProvider dataMultipleDoesNotBusyWait + * + * @param array $urls URLs to request, keyed by request ID. Paths are requested from the test server. + * @param array $expected Expected result per request ID: a status code, or `false` for a failed request. + */ + public function testMultipleDoesNotBusyWait($urls, $expected) { + if (!function_exists('getrusage')) { + $this->markTestSkipped('getrusage() is not available.'); + } + + $requests = []; + foreach ($urls as $id => $url) { + $requests[$id] = [ + 'url' => $url[0] === '/' ? $this->httpbin($url) : $url, + ]; + } + + $start_usage = getrusage(); + $start_time = microtime(true); + $responses = Requests::request_multiple($requests, $this->getOptions(['timeout' => 10])); + $wall_time = microtime(true) - $start_time; + $cpu_time = $this->getCpuTime(getrusage()) - $this->getCpuTime($start_usage); + + foreach ($expected as $id => $status_code) { + if ($status_code === false) { + $this->assertInstanceOf(Exception::class, $responses[$id], "Request {$id} should fail."); + } else { + $this->assertInstanceOf(Response::class, $responses[$id], "Request {$id} should succeed."); + $this->assertSame($status_code, $responses[$id]->status_code, "Unexpected status code for request {$id}."); + } + } + + /* + * A busy loop uses about as much CPU time as the batch takes, waiting for activity + * uses next to none, so half of the wall time clearly separates the two. + */ + $this->assertLessThan( + $wall_time / 2, + $cpu_time, + sprintf('The batch used %.2f s of CPU time in %.2f s.', $cpu_time, $wall_time) + ); + } + + /** + * Data provider. + * + * @return array + */ + public static function dataMultipleDoesNotBusyWait() { + return [ + 'slow requests' => [ + 'urls' => [ + 'first' => '/delay/1', + 'second' => '/delay/1', + ], + 'expected' => [ + 'first' => 200, + 'second' => 200, + ], + ], + // The connection to port 1 is refused immediately, the other transfer is still active. + 'refused connection next to slow request' => [ + 'urls' => [ + 'refused' => 'http://127.0.0.1:1/', + 'slow' => '/delay/2', + ], + 'expected' => [ + 'refused' => false, + 'slow' => 200, + ], + ], + ]; + } + + /** + * Get the CPU time used by the process. + * + * @param array $usage Resource usage, as returned by getrusage(). + * @return float CPU time in seconds. + */ + private function getCpuTime($usage) { + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1e6 + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1e6; + } }