Skip to content

cURL: Wait for activity instead of busy-looping in request_multiple() - #1116

Draft
swissspidy wants to merge 1 commit into
WordPress:developfrom
swissspidy:fix/curl-multi-busy-wait
Draft

swissspidy wants to merge 1 commit into
WordPress:developfrom
swissspidy:fix/curl-multi-busy-wait

Conversation

@swissspidy

Copy link
Copy Markdown
Member

Pull Request Type

This is a:

  • Bug fix
  • New feature
  • Documentation improvement
  • Code quality improvement

Context

Fixes #283. Transport\Curl::request_multiple() polls curl_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/1 requests 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.

If this PR is preferred, it could supersede both. Credit to @TysonAndre and @diegode for 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 with curl_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::testMultipleDoesNotBusyWait runs 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

  • This change does NOT contain a breaking change (fix or feature that would cause existing functionality to change).
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added unit tests to accompany this PR.
  • The (new/existing) tests cover this PR 100%.
    • Every changed line runs under the new test, except the usleep() fallback. That line only runs on older PHP versions where curl_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.
  • I have (manually) tested this code to the best of my abilities.
    • Full suite run locally on PHP 8.3 with libcurl 8.5 against the local test server. The only failures were ones that also fail on develop in that environment: it runs as root, and HTTPS goes through an intercepting proxy.
  • My code follows the style guidelines of this project.

Documentation

For new features:

  • I have added a code example showing how to use this feature to the examples directory.
  • I have added documentation about this feature to the docs directory.

AI disclosure: this change was prepared with Claude Code.

`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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

performance issue when using request_multiple

1 participant