Repository navigation
cURL: Wait for activity instead of busy-looping in request_multiple() - #1116
Draft
swissspidy wants to merge 1 commit into
Draft
swissspidy wants to merge 1 commit into
swissspidy wants to merge 1 commit into
Conversation
`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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5orPkSjWrCmXNcVAbH
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request Type
This is a:
Context
Fixes #283.
Transport\Curl::request_multiple()pollscurl_multi_exec()in a busy loop, keeping a CPU core fully busy for as long as the slowest transfer of a batch takes. In a local test, two/delay/1requests to the test server used 1.98 s of CPU time in 2.00 s of wall time. With this change, the same batch uses about 0.01 s.Found while reviewing concurrent requests for WordPress core: https://core.trac.wordpress.org/ticket/37459 and WordPress/wordpress-develop#13657.
About #284 and #493. Both have been open for several years, and neither has tests. Tests were what the 2021 triage asked for on #284, and #493 has the "needs tests" label.
$activeis set to0right before theif ($active)check, so itscurl_multi_select()call never runs.curl_multi_select()returning immediately while one connection is refused and another request is still slow. This PR has a test for exactly that case.If this PR is preferred, it could supersede both. Credit to
@TysonAndreand@diegodefor the earlier work.Changelog:
Transport\Curl::request_multiple()no longer keeps a CPU core busy while waiting for transfers.Detailed Description
After each
curl_multi_exec()round, if transfers are still active, the loop now waits for activity withcurl_multi_select(), for up to 1 second.Older PHP versions return -1 from
curl_multi_select()straight away when cURL has no file descriptors to wait on yet (see https://bugs.php.net/76480). In that case the loop sleeps for 1 ms instead of spinning.Tests. The new
CurlTest::testMultipleDoesNotBusyWaitruns a batch and compares the CPU time the process used (getrusage()) with the wall time. It asserts the CPU time stays below half of the wall time. A busy loop uses about as much CPU time as the batch takes, and waiting uses next to none, so the threshold leaves a wide margin either way. It has two data sets:Without the fix, both data sets fail locally ("The batch used 1.98 s of CPU time in 2.00 s."). With the fix, both pass. The test is skipped if
getrusage()is unavailable.Quality assurance
usleep()fallback. That line only runs on older PHP versions wherecurl_multi_select()returns -1 without an error. On current PHP, -1 only signals an error, which I couldn't trigger from a test. Coverage wasn't measured locally.developin that environment: it runs as root, and HTTPS goes through an intercepting proxy.Documentation
For new features:
examplesdirectory.docsdirectory.AI disclosure: this change was prepared with Claude Code.